Unix Operating System and Shell Programming F-CSIT311
Unix Operating System and Shell Programming F-CSIT311
in
nl
O
ty
Unix Operating System
and Shell Programming
r si
ve
ni
U
ity
m
)A
(c
e
in
© Amity University Press
nl
No parts of this publication may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording or otherwise
without the prior permission of the publisher.
O
Advisory Committee
ty
Chairman : Ms. Monica Agarwal
Members : Prof. Arun Bisaria
si
Dr. Priya Mary Mathew
Prof. Aindril De
Mr. Alok Awtans
Dr. Coral J Barboza
r
ve
Dr. Monica Rose
Mr. Sachit Paliwal
ni
Published by Amity University Press for exclusive use of Amity Directorate of Distance and Online Education,
Amity University, Noida-201313
Contents
e
Page No.
in
Module -1: Introduction to Unix 01
1.1 Introductory Concept to OS
1.1.1 Introduction to OS
nl
1.1.2 History of Unix
1.1.3 UNIX Family
O
1.1.4 Unix System Layered Architecture
1.2 Unix File System
1.2.1 Unix File System
ty
1.2.2 Unix Paths
1.2.3 UNIX File System Structure
1.2.4 Inode
si
1.3 Process
1.3.1 Basic Concept of Process
1.3.2 Process State Diagram r
ve
1.3.3 Type of Shells
1.3.4 Introduction of Basic System Calls
e
2.3.3 Redirecting Output and Error
in
2.4 Unix Permissions
2.4.1 Concept of User and Group Permissions and Its Dependencies
2.4.2 Changing, Setting Permissions
nl
2.4.3 Changing Permissions
2.5 Unix Links
O
2.5.1 File Links
2.5.2 Types of Links
2.5.3 ln Command
ty
2.6 Process Management in Unix
2.6.1 Concept of Jobs and Process
2.6.2 Process ID; Foreground and Background Jobs
si
2.6.3 Suspend and Interrupt a Process
2.6.4 Killing Jobs
2.6.5 Changing Password, Exit r
ve
Module - 3: VI Editor 154
3.1 Vi Editor
3.1.1 Command Mode, Insert Mode and Last Line Mode
ni
e
4.2.4 Assignment Statements, Arithmetic, Logcal and Relational Operators, Read, Echo
in
4.2.5 Shift, Meta characters
4.3 Shell Scripting
4.3.1 Test Command: Numerical Test, File Test and String Test
nl
4.3.2 for Loop
4.3.3 while and until Loop
O
4.3.4 case and switch
ty
5.1.1 Adding and Removing Users, Changing Password
5.1.2 Disk Management, Starting and Shutting Down the System
si
5.1.3 File System Mounting and Unmounting
5.1.4 Monitoring System Usage
5.1.5 Ensuring System Security
r
ve
ni
U
ity
m
)A
(c
(c
)A
m
ity
U
ni
ve
r si
ty
O
nl
in
e
Unix Operating System and Shell Programming 1
e
Learning Objectives:
in
At the end of this module, you will be able to understand:
●● Introduction to OS
nl
●● History of Unix
●● UNIX Family
O
●● Unix System Layered Architecture
●● Unix File System
●● Unix Paths
ty
●● UNIX File System Structure
●● Inode
●● Basic Concept of Process
si
●● Process State Diagram
●● Type of Shells
●● Introduction of Basic System Calls
r
ve
Introduction
An operating system is a piece of software that controls how computers work. The
ni
hardware must include necessary measures to ensure the computer system’s correct
functioning and to prevent user programmes from interfering with the system’s proper
operation.
U
The UNIX file system allows you to organise and manage your data in a robust and
flexible way. In this unit we discuss the most significant commands for managing files
and directories and present the basic notions of the file system. The commands that
)A
perform basic file manipulation activities, such as reading files, changing directories,
deleting, and moving files, are among the most commonly used UNIX commands.
Types of Processes
(c
session is used to set up and control these devices. To put it another way, such
Notes
e
operations must be initiated by a user who is connected to the system; they do not
begin automatically as part of the system’s functions or services.
in
Background processes (also referred to as non-interactive/automatic processes):
Background processes are those that do not require human input and are not
connected to a terminal.
nl
1.1 Introductory Concept to OS
An operating system serves as a bridge between the computer’s user and its
O
hardware. The goal of an operating system is to offer a convenient and efficient
environment in which a user may run programmes.
ty
1.1.1 Introduction to OS
An operating system (OS) is a set of applications that serves as a conduit between
a computer’s user and its hardware. An operating system’s objective is to offer an
si
environment in which a user may run programmes. Operating systems are thought
of as resource supervisors. Computer hardware, which includes processors, storage,
input/output devices, communication devices, and data, is the most important resource.
r
Implementing the user interface, sharing hardware between users, allowing users
ve
to share data among themselves, preventing users from interfering with one another,
scheduling resources among users, facilitating input/output, recovering from errors,
accounting for resource usage, facilitating parallel operations, organising data for
secure and rapid access, and handling network communications are just a few of the
ni
the basic computational resources. The application programmes specify how these
resources are to be employed to solve the users’ computing problems. The operating
system manages and coordinates the utilisation of hardware by the many system and
application applications for different users.
ity
and users as needed for their duties. The operating system must decide which requests
for resources are allocated resources to operate the computer system fairly and
effectively because there may be several, possibly conflicting, requests for resources.
)A
While there is no commonly accepted definition of the term “operating system,” the
following is a good place to start:
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 3
e
primary functions:
in
among multiple users and workloads, and
●● To create an interface between computer hardware and programmers that
makes the creation, coding, debugging, and maintenance of application
nl
programmes easier and more feasible.
The following are the functions that a good operating system should perform:
O
translator.
●● It should make it easier to communicate with other users.
●● Facilitate the creation of directories and files, as well as the security option.
ty
●● Provide methods to deal with the nitty gritty of I/O programming.
●● Make compilers available so that programmes can be translated from high-
level languages to machine language.
si
●● Provide a loader programme to move the built programme code from the hard
drive to the computer’s memory for execution.
●●
r
Assure that when the computer has multiple active processes, each receives
fair and non-interfering access to the central processor unit for execution.
ve
●● Make sure you have enough storage and that the right devices are assigned
to you.
●● Allow users to save information in the form of files for long periods of time.
ni
●● Allow users to share system resources when appropriate and safeguard them
against unauthorised or malicious involvement when necessary.
Though editors and translators, as well as numerous utility programmes (such as
U
sort and file transfer programmes), are not normally considered part of the operating
system, the operating system is responsible for giving access to these system
resources.
ity
their operating systems were like, because operating systems have historically been
intimately related to the architecture of the computers on which they run. We may not
be able to exactly match operating system generations to computer generations, but we
)A
We may broadly split them into five generations, each of which is defined by
hardware component technology, software development, and the manner in which
computer services are delivered.
(c
0th Generation
The term “0th generation” refers to the period of computing development that
preceded the commercial production and sale of computer hardware. We believe the
Notes
e
time period begins with Charles Babbage’s invention of the Analytical Engine. Following
that, John Atanasoff designed the computers in 1940; Howard Aiken and a group of
IBM engineers built the Mark I at Harvard in 1944; Wallace Eckert and John Mauchly
in
designed and built the ENIAC at the University of Pennsylvania in 1944; and John
Von Neumann, Arthur Burks, and Herman Goldstine developed the EDVAC in 1944-
46 (which was the first to fully implement the idea of the stored programme and serial
nl
execution of instructions).
The growth of commercial computing and operating system software began with
the development of EDVAC. Electronic vacuum tubes were the hardware component
O
technology of the time. These early computers didn’t have an operating system; hence,
they didn’t have an operating system. Early programmes were written in machine
language, and each one contained instructions for starting the computer.
ty
The manner of operation was known as “open-shop,” which meant that users
signed up for computer time, and when their time came, the entire (at the time, very
large) computer system was handed over to them. All machine set-up and operation, as
si
well as subsequent clean-up and preparation for the next user, were the responsibility
of the individual user (programmer). This approach was clearly inefficient, and it was
reliant on the individual programmers’ differing abilities as operators.
r
First Generation (1951-1956)
ve
The release of Eckert and Mauchly’s UNIVAC I in early 1951, as well as the IBM
701 (also known as the Defence Calculator) a few years later, heralded the beginning
of commercial computing. The vacuum tube was once again the active component
technology in the first generation.
ni
For a period, operations were carried out without the use of an operating system.
The mode was known as “closed shop,” and it was characterised by the appearance
U
of paid operators who would select the task to be executed, load the system with an
initial programme, run the user’s programme, and then select another job, and so on.
The operator’s routine grew when programmes began to be written in higher-level,
procedure-oriented languages.
ity
The operator then chose a job, ran the translation programme to assemble or
compile the source programme, combined the translated object programme with any
existing library programmes that the programme might require for input to the linking
programme, loaded and ran the composite linked programme, and then repeated the
m
Application programmes were run one at a time, and their data was obtained from
a specific physical I/O device. They were translated with absolute computer addresses
)A
that bound them to be loaded and run from these reassigned storage addresses set
by the translator. There was no way to move a programme to a new storage place for
any reason. Similarly, if any of the devices that a programme is bound to is busy or
malfunctioning, the application will not be able to execute at all.
(c
The inefficiencies inherent in the preceding ways of operation led to the invention
of the mono-programmed operating system, which reduced human interference in
job execution and gave programmers with a variety of useful features. The operating
e
well as a task scheduler and a number of utility applications that were retained in
secondary storage.
in
Control or specification cards (computer programmes were submitted on data
cards back then) notified the operating system of what system resources (software
resources such as compilers and loaders; and hardware resources such as tape drives
nl
and printers) were required to run a particular application. The systems were created to
be used in a batch processing environment.
O
who started them up by inserting a magnetic tape containing the operating system
executable code into a “boot device” and pressing the IPL (Initial Program Load) or
“boot” button to start the operating system bootstrapping process.
ty
Once the system was loaded, the operator set the date and time, and then
started the job scheduler programme, which read and interpreted the control
statements, secured the necessary resources, executed the first user programme,
recorded timing and accounting data, and then went back to start processing another
si
user programme, and so on, as long as there were programmes waiting to be
executed in the input queue.
r
From hands-on operation through closed shop operation to the creation of mono-
programmed operating systems, the first generation witnessed it all. At the same time,
ve
programming languages were evolving away from fundamental machine languages,
first to assembly language, and then to procedure-oriented languages, the most notable
of which was John W. Backus’ invention of FORTRAN in 1956. However, other issues
persisted, the most visible of which was the inefficient use of system resources, which
ni
was most noticeable when the CPU waited while the slower mechanical I/O devices
received or wrote programme data. Furthermore, system protection was a problem
because the kernel of the operating system was not protected against being rewritten
U
most part, computer systems were still based on cards and tapes. The first widespread
usage of random-access devices, like as discs, did not occur until the second
generation. Large, centralised computers running mono-programmed batch processing
)A
As a result, the CPU’s high-speed electrical component was frequently forced to wait for
I/O operations using mechanical devices (card readers and tape drives) that were an
order of magnitude slower.
Near the end of the first generation, this problem led to the introduction of the
Notes
e
data channel, an integral and special-purpose computer with its own instruction set,
registers, and control unit designed to process input/output operations separately and
asynchronously from the computer’s main CPU, and its widespread adoption in the
in
second generation.
Some I/O could be buffered via the data channel. That is, the input data for a
nl
programme might be read “ahead” from data cards or tape into a specific memory block
known as a buffer. The data may then be transmitted from the buffer locations at the
quicker main memory access speed rather than the slower I/O device speed when the
user’s programme came to an input statement. Similarly, the output of a programme
O
may be written to another buffer and then transferred to the printer, tape, or card punch.
The data channel’s capacity to function asynchronously and concurrently with the
main processor was what made it all work. As a result, slower mechanical I/O could be
ty
occurring at the same time as primary programme execution. I/O overlap was the name
given to this process.
A channel programme was set up by the operating system I/O control routines and
si
activated by a particular instruction executed by the CPU to control the data channel.
The channel then processed data to or from the buffer on its own. This allowed the CPU
to communicate with the data channel in order to start an I/O operation. It was left to the
r
channel to inform the CPU of events such as data errors and transmission completion.
Initially, this communication was done through polling, in which the CPU would pause
ve
its work and poll the channel to see whether there was any new information.
Because polling was clearly wasteful (imagine stopping your work every few
minutes to go to the post office to see if an expected letter had arrived), another key
ni
second-generation innovation – the interrupt – was born. The data channel could send
a message to the CPU, which was usually “I/O complete.” In fact, the interrupt concept
was eventually extended from I/O to allow signalling of a variety of unusual events,
including arithmetic overflow, division by zero, and time-out. Of course, interval clocks
U
were added in tandem with the latter, and the operating system gained a means of
recovering control from a programme that was running endlessly.
I/O and data channel communication and control became operating system functions,
both to relieve application programmers of the difficult details of I/O programming and to
protect the system’s integrity in order to provide better service to users by segmenting
jobs and running shorter jobs first (during “prime time”) and relegating longer jobs
m
To further reduce the I/O wait time, the system was set up to spool the input batch
from slower I/O devices like the card reader to the much faster tape drive, and the
output batch from the higher speed tape drive to the slower printer. The user submitted
a job at a window, a batch of jobs was gathered and spooled from cards to tape “off
line,” the tape was moved to the main computer, the jobs were run, and their output was
(c
collected on another tape, which was then sent to a satellite computer for off line tape-
to-printer output. The user then went to the submission windows to pick up their output.
As random access devices were accessible at the end of this decade, tape-
Notes
e
oriented operating systems began to be superseded by disk-oriented ones. Users
perceived a virtual machine as the operating system and more advanced disc hardware
supported a growing percentage of the programmer’s work.
in
The second generation was marked by rapid advancements in operating systems.
It was also the time of sequential batch processing. However, the sequential processing
nl
of one job at a time remained a major drawback. As a result, CPU utilisation for I/O
bound jobs remained low, as did I/O device utilisation for CPU bound jobs. Because
computers were still enormous (room-size) and expensive equipment, this was a
serious problem. In their computer services known as the time-sharing system,
O
researchers began to experiment with multiprogramming and multiprocessing. The
Compatible Time Sharing System (CTSS), created at MIT in the early 1960s, is one
notable example.
ty
Third Generation (1964-1979)
With the unveiling of IBM’s System/360 series of computers in April 1964, the third
si
generation officially began. Integrated circuits (ICs) were introduced into hardware
technology, which provided considerable speed and cost benefits.
flowing straight from the cards and output going directly to the printer, as it had been
before.
U
The concept of utilising the computer’s data channel I/O capabilities to its
maximum potential continued to evolve. That is, designers realised that I/O only needed
to be begun by a CPU command, and that the actual data transfer could be controlled
by a separate, asynchronously functioning channel programme. It was possible to keep
ity
the slower mechanical I/O device operating by swapping control of the CPU between
the currently executing user programme, the system reader programme, and the
system writer programme. This reduced the amount of time the CPU spent waiting for
I/O completion. As a result, system throughput and resource utilisation increased, which
benefited both users and providers of computer services.
m
instruction at a time) necessitated the addition of new features and complexity to the
operating system.
First, because the input queue was now on disc, a direct access device, the
system scheduler was no longer bound by the first-come-first-served policy, allowing it
to choose the “best” next job to enter the system (looking for either the shortest job or
(c
Second, because the user software, the system reader, and the system writer
Notes
e
would all be using the same CPU, some sort of processor allocation mechanism
or policy was required. The CPU was dispatched to the reader, the writer, and the
programme in that order because the goal of spooling was to increase resource
in
utilisation by allowing slower I/O devices to run asynchronously with user programme
processing, and because I/O processing only required the CPU for short periods to
initiate data channel instructions, Furthermore, if something became available to read
nl
while the writer or user programmes were running, the reader programme would pre-
empt the currently running programme to reclaim control of the CPU for its initiation
instruction, and the writer programme would pre-empt the user programme for the
O
same reason. The static priority rule with pre-emption was implemented as a system
dispatcher programme in the operating system.
Because more than one programme was resident in main storage at the same
ty
time, the spooling operating system had multiprogramming. Multiprogramming
was further extended to include more than one active user application in memory at
the same time. Both the scheduler and the dispatcher were upgraded to support this
expansion. The dispatcher incorporated policies for allocating processor resources
si
among the competing user programmes, and the scheduler was able to manage
the different resource needs of the multiple continuously active used programmes.
Furthermore, memory management became increasingly complicated in order to
r
ensure that the programme code for each job, or at least the portion of the code that
ve
was being performed, was stored in main storage.
●● Relocation hardware that allowed code blocks to be moved around inside memory
without incurring unnecessary expense.
●● Storage protection hardware is available to ensure that user jobs are protected
from one another, and the operating system is protected from user programmes.
ity
telephone lines) in this setting. Users “logged in” to the central system at their terminals
and interacted with the system. The multiprogramming operating system facilitated the
system’s apparent concurrency. Users shared not only the hardware of the system, but
Notes
e
also its software resources and storage space.
The third generation was an exciting moment for computer hardware development
in
as well as the operating system that accompanied it. During this time, the issue of
operating systems evolved into a prominent component of the computing field.
nl
Fourth Generation (1979 – Present)
The appearance of the personal computer and workstation characterises the fourth
generation. Miniaturization of electronic circuits and components continued, and the
O
third generation’s component technology, large scale integration (LSI), was replaced by
the fourth generation’s component technology, very large scale integration (VLSI). With
its ability to incorporate thousands of transistors on a compact chip, VLSI enabled the
development of desktop computers with capabilities that far outstripped those that filled
ty
entire rooms and floors of buildings just twenty years before.
The operating systems that run these desktop machines have taken us full circle,
back to an open shop atmosphere where each user takes up an entire computer for the
si
duration of a project. This works better now, not only because advances in technology
have made the virtual computer created by the operating system/hardware combination
considerably easier to use, or “user-friendly,” in the terminology of the popular press.
r
However, advances in hardware miniaturisation and technology have accelerated
ve
to the point that we now have low-cost workstation-class computers capable of
multiprogramming and time-sharing. As a result, the operating systems that run today’s
personal computers and workstations are quite similar to those that ran third-generation
minicomputers. Microsoft’s DOS for IBM-compatible personal computers and UNIX for
ni
Many of these desktop computers, on the other hand, are now networked or
distributed systems. In a networked system, each computer’s operating system
U
is enhanced with communication features that allow users to log into any system on
the network and move data between machines linked to the network remotely. From
the user’s perspective, the machines that make up a distributed system operate as if
they were a single processor system; a central operating system controls and makes
ity
transparent the location in the system of the specific processor or processors and file
systems that are handling any given programme.
Modern computer operating systems can be divided into three categories based on
the type of interaction that occurs between the computer user and his or her software
while it is being processed. Batch, timesharing, and real-time operating systems are the
)A
three categories.
at the computer where they will be run. In this situation, the user has no contact with the
work while it is being processed, and the turnaround time is the period from when the
job is submitted until it is completed, and the results are available to be returned to the
Notes
e
person who submitted it.
Time Sharing
in
Time sharing operating systems are another way of offering computing services.
In this scenario, a computer provides online computing services to several or many
users at the same time. The operating system facilitates, controls, and monitors the
nl
multiple users’ sharing of the core processor, memory, and other resources of the
computer system. In this environment, the user has practically complete control over
the programme throughout execution, and the computer’s response time should be only
O
a few seconds.
ty
The third type of operating system is real-time operating systems, which are
designed to support applications when response speed is critical to avoid errors,
misrepresentation, or even disaster. Airlines reservations, machine tool control, and
nuclear power plant monitoring are all examples of real-time operating systems. In
si
this situation, the systems are built to be disrupted by external signals that require the
computer system’s urgent attention.
r
Machines, research instruments, and industrial systems are all controlled by these
real-time operating systems. The user interface of an RTOS is often limited, and there
ve
are no end-user tools. An RTOS is responsible for managing the computer’s resources
in such a way that each operation takes exactly the same amount of time every time
it occurs. In a complex machine, having a part move faster just because system
resources are available could be just as disastrous as having it not move at all because
ni
operating system could, and often does, have multiple active user programmes in main
storage at the same time. “Multiprocessing” is another essential and all-too-similar
phrase.
m
Multiprocessing System
A multiprocessing system is a computer configuration that contains multiple
independent processing units. Large computer hardware complexes encountered in
)A
own stand-alone functionality, provisions for handling communication and data transfer
between the other computers with which it is connected.
e
in many ways. They clearly require a network interface controller and some low-level
software to control it, as well as applications for remote login and file access, but these
additions do not alter the operating systems’ fundamental foundation.
in
Desirable qualities of OS
The desirable qualities of an operating system are in terms of: Usability, Facilities,
nl
Cost, and Adaptability.
●● Usability
O
●● Robustness
●● Accept all valid inputs and can handle them.
●● Consistency
ty
●● Proportionality
●● Convenience
si
●● Powerful with high level facilities.
●● Facilities
●● Sufficient for intended use
r
ve
●● Complete
●● Appropriate.
●● Costs
ni
●● Low overhead.
●● Cost of doing nothing should be low. E.g., idle time at a terminal.
ity
●● Adding new facilities and features - which look like the old ones.
e
DOS
in
The first widely used operating system for personal computers was DOS (Disk
Operating System). It’s a master control programme that launches automatically
whenever you turn on your computer (PC). DOS runs in the background of the
computer, allowing you to run programmes and manage data. It is a Microsoft single-
nl
user operating system for the PC. It is the fundamental control programme for Windows
3.1, 95, 98, and ME. It was the first operating system for the PC. To support current
DOS applications, Windows NT, 2000, and XP imitate DOS.
O
UNIX
Sun Microsystems, Silicon Graphics, IBM, and a number of other businesses
ty
employ UNIX operating systems in widely sold workstation products. The Internet’s
evolution and the remaking of computing as based on networks rather than individual
computers were aided by the UNIX environment and the client/server application
model. Linux, a UNIX clone that comes in both “free software” and “commercial”
si
variants, is gaining traction as a viable alternative to proprietary operating systems.
UNIX is written in the C programming language. AT&T developed both UNIX and
r
C, which were freely supplied to government and academic institutions, allowing it to be
ported to more machine families than any other operating system. As a result, the term
ve
“UNIX” evolved to mean “open systems.”
The kernel, file system, and shell make up UNIX (command line interface). The
Bourne shell (original), the C shell, and the Korn shell are the three main shells. The
ni
UNIX command set is extensive, with over 600 procedures that alter data and text in a
variety of ways. Many instructions are cryptic, but the Motif GUI hides the DOS prompt,
similar to how Windows hides the DOS prompt. Even with its various versions, UNIX
U
mainframes run UNIX applications. Another UNIX variation, Linux, is also getting a lot of
traction.
Windows
m
world’s largest software corporation, as well as the Windows industry as a whole, which
comprises tens of thousands of software developers, support Windows.
This networking support is what made Windows so popular in the first place.
Notes
e
Windows 95, 98, ME, NT, 2000, and XP, on the other hand, are complex operating
systems. Certain hardware and software combinations might cause issues, and
diagnosing them can be difficult. Every new version of Windows introduces interface
in
changes that confuse customers and keep support staff busy, and installing Windows
apps is also a challenge. Microsoft has put a lot of effort into making Windows 2000 and
Windows XP more resistant to installation issues and crashes in general.
nl
Macintosh
Apple Computer’s Macintosh (often known as “the Mac”), released in 1984, was
O
the first widely-sold personal computer with a graphical user interface (GUI). The Mac
was created with the goal of providing users with a natural, intuitive, and “user-friendly”
computer interface. The mouse, the use of icons or small visual pictures to represent
ty
items or activities, point-and-click and click-and-drag actions, and a variety of window
operating ideas are all examples of this. In its original Windows operating system,
Microsoft was successful in integrating user interface features popularised by the Mac.
si
The main disadvantage of the Mac is that there are far fewer Mac programmes
available than there are for Windows. However, all of the essential apps are available,
and the Macintosh is a machine that practically everyone can use. Data compatibility
r
between Windows and Mac is a problem, but it is frequently exaggerated and easily
resolved.
ve
The Macintosh has its own operating system, Mac OS, which is now known as
Mac OS X in its most recent edition. Mac versions are now powered by the PowerPC
microprocessor, which was developed collaboratively by Apple, Motorola, and IBM.
Originally built on Motorola’s 68000 series microprocessors, Mac versions are now
ni
While Mac users account for only approximately 5% of all personal computer
U
users, they are extremely popular and nearly a cultural need among graphic designers
and online visual artists, as well as the companies for which they work.
the goal of providing simple but powerful utilities that could be cobbled together in a
flexible way to handle a wide range of tasks.
Unix is designed for multiple users, which distinguishes it from other operating
)A
systems you may be familiar with (e.g., PC). As a result, multiple users may have
multiple jobs running at the same time. Its original goal was to make software
development easier. It is the major operating system used by physical scientists
worldwide, and it is used by all supercomputing facilities. To put it frankly, if you work
on the numerical side of physical sciences, you must learn how to use a Unix operating
(c
system.
(1) Unix development is backed by a corporation. This implies it is more stable and
Notes
e
is preferred by individuals who value stability above all else.
(2) Linux is free and developed by a community of users. As a result, you get what
in
you pay for, right? It does, however, have some stability difficulties and bugs are
beginning to appear.
However, faults are swiftly resolved, and fresh content, programmes, and
nl
functionality have soon eclipsed Unix.
O
●● The Kernel: The Kernel is the heart of the UNIX operating system. Loaded
upon system startup (boot); handles all of the system’s resources. Interpreting
and executing shell commands, controlling the machine’s memory and
assigning it to processes, and scheduling the work done by the CPUs are all
ty
examples of what it performs.
●● Shell: When you log into a Unix system, you are automatically logged into a
shell software. The shell is a command interpreter that takes each command
si
and sends it to the kernel of the operating system to be executed. The
outcomes of this process are then displayed on your screen. On most Unix
systems, several shells are available, each with its own set of advantages and
r
disadvantages. The Bourne Shell (sh), C Shell (csh), and Bourne Again Shell
ve
are all examples (bash).
●● Services: Utilities UNIX has a large number of utility programmes, referred
to as commands. The commands perform common tasks such as printing,
modifying files, and so forth.
ni
shell, an editor, and an assembler for a sluggish Digital Equipment Corp. (DEC) PDP-7
minicomputer, writing the first version of Unix in assembly language.
Thompson and a colleague, Dennis Ritchie, had been feeling stranded since Bell
Labs pulled out of a controversial project to develop a time-sharing system called
ity
Multics earlier this year (Multiplexed Information and Computing Service). They didn’t
want to use any of the batch operating systems that were popular at the time, and they
didn’t want to reinvent Multics, which they thought was hideous and bulky.
Thompson built the first version of Unix after tossing around some ideas for a new
m
system, which the pair would continue to develop over the next several years with the
help of colleagues Doug McIlroy, Joe Ossanna, and Rudd Canaday. Some of Multics’
ideals were carried over into Unix, but the beauty of Unix back then (if not now) was in
)A
Five years later, in the Communications of the ACM (CACM), the magazine of the
Association for Computing Machinery, Ritchie and Thompson would write, “A strong
operating system for interactive usage need not be expensive either in technology
(c
or in human effort.” “We expect that Unix users will discover that the system’s most
significant traits are its simplicity, elegance, and ease of use.”
e
extensively used in universities, government agencies, and enterprises to run servers
and workstations. The ACM awarded Thompson and Ritchie the A.M. Turing Award for
contributions to IT in 1983, noting that “the concept of the Unix system has pushed a
in
generation of software designers to new ways of thinking about programming.”
Early steps: Ken Thompson and Dennis Ritchie in the early days of Unix
nl
Of course, Unix’s success did not come overnight. It was ported to the PDP-11
minicomputer in 1971, which was a more powerful platform than the PDP-7 on which
it was designed. It was rolled out to a few typists in the Bell Labs Patent department,
O
its first users outside the development team, with text-formatting and text-editing tools
added.
ty
Thompson’s prior B language); Thompson then rewrote Unix in C, considerably
increasing the OS’ portability across different computing environments. It was given the
moniker Unics (Uniplexed Information and Computing Service), a play on Multics, along
si
the line, and the spelling morphed into Unix.
It was now time to get the news out. “The UNIX Time-Sharing System,” a CACM
essay by Ritchie and Thompson published in July 1974, took the IT industry by storm.
r
Until then, Unix had only been used by a few people at Bell Labs. But now, with the
ve
support of the Association for Computing Machinery (which one editor described as
“beautiful”), Unix had reached a critical juncture.
In his book The Daemon, the Gnu, and the Penguin, IT historian Peter Salus noted,
“The CACM essay had a huge influence.” “Ken was soon inundated with demands for
ni
Unix.”
Hackers’ Heaven
U
Their technique, as well as the code they produced, drew a lot of interest from
university programmers and, later, from new firms without the massive budgets of
IBM, Hewlett-Packard, or Microsoft. Other hackers, such as Bill Joy at the University of
California, Rick Rashid at Carnegie Mellon University, and David Korn later at Bell Labs,
wished for nothing more than Unix.
m
In the CACM essay, Thompson and Ritchie said, “Nearly from the outset, the
system was able to, and did, maintain itself.” “We were willing to change and rewrite
the system and its software when new ideas were conceived, found, or suggested
)A
by others because all source programmes were always available and easily modified
online.”
In the 1970s, Korn worked as a programmer at Bell Labs and is now an AT&T
Fellow. “One of the defining features of Unix was that tools could be built and replaced
(c
by better tools,” he recalls. “It wasn’t a one-size-fits-all situation where you had to buy
into everything; you could actually create better versions.” He created the Korn shell,
which is essentially a programming language for directing Unix activities and is currently
Notes
e
open-source software.
Salus, an author and technology historian, recalls his work as a lecturer at the
in
University of Toronto in the 1970s with the programming language APL on an IBM
System/360 mainframe. It wasn’t going well at all. However, a buddy at Columbia
University gave him a presentation of Unix running on a minicomputer the day after
nl
Christmas in 1978. “I said, ‘Oh my God,’ and I became a total convert,” Salus adds.
He claims that the “pipe” function, which was introduced in 1973 and made it easy
to send the output of one application to another, was the most important element of
O
Unix for him. Many operating systems, including all Unix variants, Linux, DOS, and
Windows, have imitated the pipeline concept, which was pioneered by Bell Labs’
McIlroy.
ty
Unix was created at Bell Labs in Murray Hill, New Jersey. Alcatel-Lucent/Bell Labs
is the source of this image.
Unix also had the advantage of not requiring a million-dollar mainframe to run on,
si
as Salus puts it. Because that’s all Thompson and Ritchie could get their hands on in
1969, it was written on the tiny and primitive DEC PDP-7 minicomputer. Salus recalls,
“The PDP-7 was almost incapable of anything.” “I got sucked in.”
r
Many others were addicted as well. Unix was widely adopted by university
ve
researchers because it was simple and easy to modify, had low resource requirements,
and the source code was virtually free. For the same reasons, startups like Sun
Microsystems and a slew of now-defunct scientific computing firms like Multiflow
Computer chose it as their operating system of choice.
ni
Unix Offspring
Because AT&T was prohibited by a federal consent order from straying from its
U
Unix, which was developed without management’s approval, was initially regarded
ity
Then, in the late 1970s, AT&T understood it was dealing with a commercially
significant asset. As they explored for ways to safeguard Unix as a trade secret, its
lawyers began to embrace a more favourable interpretation of the 1956 consent decree.
m
Unix licences banned colleges from utilising the Unix source code for study in their
courses beginning in 1979, with the release of Version 7.
)A
Salus states, “Minix included all of the ideals of Unix, and it was a wonderful
(c
e
Berkeley in the 1970s, received a copy of Unix from Bell Labs and recognised it as a
good foundation for his own work on a Pascal compiler and text editor.
in
Berkeley Software Distribution (BSD) Unix was created as a result of his and
others’ modifications and additions at Berkeley. Joy handed out copies of 1BSD for $50
in March 1978.
nl
So, by 1980, there were two major Unix lines, one from Berkeley and the other
from AT&T, and the stage was set for the Unix Wars. The good news was that software
developers from all over the world could obtain Unix source code and modify it to suit
O
their requirements and preferences. The bad news is that they actually did it. Unix
became more popular, and its variations began to diverge.
Joy co-founded Sun Microsystems in 1982 and offered the Sun-1 workstation,
ty
which ran a variant of BSD called SunOS. (Solaris would arrive ten years later.) The
next year, AT&T published the initial version of Unix System V, a hugely significant
operating system that would go on to become the foundation for IBM’s AIX and Hewlett-
HP-UX. Packard’s
si
Unix Application Programming Interface
Many proprietary operating systems present application activity in a simplified
r
manner. A typical programme reads data from a disc, tape, or terminal, then processes
ve
it. The output is saved to a disc, cassette, terminal, or printer. To support these types of
capabilities, operating systems often provide simple to use, well-implemented facilities.
capabilities are generally difficult to use, poorly documented, and only accessible
through assembly code in older operating systems. When these characteristics are
used in a software, the programme becomes substantially more complex and difficult to
U
maintain.
from the C language without creating any assembly language is fairly simple in UNIX.
Furthermore, the documentation for these advanced capabilities follows the same
structure and is located in the same area as the documentation for standard application
calls.
m
Users could create applications in C and take advantage of all of the operating
system’s features after UNIX was deployed. Using these tools, application developers
were able to swiftly construct considerably more sophisticated apps.
)A
UNIX system and application developers for the most part, code in the same
(c
language and use the same application programming interface. The operating system
programmers in typical proprietary operating systems programme in assembly language
and have access to numerous capabilities that the application developer does not.
Unix Networking
Notes
e
Version 4.2BSD, released in 1984 by the University of California at Berkeley,
provided a comprehensive implementation of the TCP/IP networking protocols. Based
in
on Ethernet networking, systems based on this and later BSD releases allowed multi-
vendor networking functionality.
Remote login, file transfer, electronic mail, and other vital networking technologies
nl
were included.
UNIX networking enabled many different sorts of systems to share and use data as
UNIX was ported onto more and more various types of computer hardware. As a huge
O
distributed system, networks made up of many separate systems could be used.
This capacity to share and mutually use data was substantially strengthened when
SUN Microsystems incorporated NFS (Network File System).
ty
Unix Popularity
The reader may be wondering at this point, “This guide is intended for people
si
who are new to UNIX. What is the point of all this talk about programming and system
programming?” Because the document uses UNIX’s history to explain why it is so
popular, the answer is yes. Many hardware and software vendors have chosen UNIX
r
because of application portability and system programming concerns.
ve
As a result of many vendors selecting UNIX, users can choose from a wide range
of UNIX systems at competitive pricing. There are three main reasons for UNIX’s
popularity (none of them are related to the user interface):
using the application programme interface, which eliminates the need to write
assembly language. These applications run on a variety of hardware platforms
from different vendors. Third-party software vendors can save money by providing
a single UNIX version of their product instead of four vendor-specific versions that
require four times the maintenance.
m
●● Users can simply network several systems from various suppliers using vendor-
independent networking. These characteristics of UNIX have aided its success
since the mid-1980s.
)A
(c
User Interface
Notes
e
There has been no discussion of the UNIX user interface thus far. For skilled
programmers, UNIX is a good operating system. Because the operating system was
in
conceived and implemented by experienced programmers, it contains everything that
an expert programmer requires, but little else. The on-line documentation known as
“man-pages” or manual pages is a wonderful illustration of this. There is virtually little
nl
tutorial content in the material, which is totally reference oriented. The man pages
are extremely beneficial to experienced programmers, but they might be daunting to
newcomers.
O
There has been a lot of work done in the last few years to improve the UNIX user
interface. The inclusion of windowing interfaces on top of UNIX, such as X-windows,
Suntools, NextStep, Motif, OpenLook, and others, has been the most spectacular
endeavour. These windowing interfaces are constructed on top of UNIX to provide a
ty
more intuitive interface to UNIX. They do not affect UNIX itself. Each of the various
user interfaces has its own set of benefits and drawbacks. All of these Graphical User
Interfaces are currently undergoing extensive development (GUIs).
si
UNIX vendors have also worked to improve the user interface of their own versions
of UNIX for users who do not use windowing interfaces. Despite all of these attempts,
UNIX has a poor end-user interface.
User Portability
r
ve
UNIX has a large user base of non-programmers, despite its poor user interface.
The main reason for this is that UNIX runs on a wide range of computer systems, from
modest PCs to the world’s largest supercomputers. After learning UNIX, a user’s skills
ni
can be used to a variety of platforms. “User portability” refers to a user’s ability to work
on a variety of computer systems without having to retrain.
Many users of other operating systems have switched to UNIX because they
U
Open Systems
ity
In the field of international standards, there has been a recent effort to define what
constitutes a “open system.” An open system is one that provides application portability,
system interoperability, and user portability across a variety of hardware platforms from
various computer vendors.
m
UNIX is a good example of the benefits of having a “open system” for the user.
)A
(c
e
in
nl
O
ty
r si
ve
Figure: UNIX Family tree
The UNIX family is large and diverse, and in this section, we’ll take a look at a few
ni
of its most important members, focusing on the present state of the various operating
systems and their key characteristics.
U
Linux
Linus Torvalds, a Finnish student, founded Linux in 1991, and it is currently at
Version 2.6 as of this writing. Traditionally, Linux utilised a three-number naming
scheme, such as kernel version.majorrevision.minor revision, as in 2.4.28. A stable
ity
version of the kernel had an even major revision number, while a development version
had an odd major revision number. The development version became stable (e.g., 2.1.x
2.2.x) at some time, and a new development version (e.g., 2.3.x) was established. The
purpose for updating to a new version number was always tied to a new feature. There
m
had been enough new features added and developed to warrant a change in the major
revision number.
Starting with the 2.6 tree, this paradigm has modified, partly because odd/even
)A
major revisions resulted in an unstable tree that took years to stabilise. In the new
paradigm, feature development takes place within the same major revision number, and
an additional number is added to track patches, problems, and (most importantly for
us) security fixes applied throughout the minor revision release life. As a result, as of
2.6.27, the numbering is kernel version.majorrevision.minorrevision.extra version. 2.
(c
Main kernel releases (also known as vanilla releases) are numbered sequentially,
making it simple to identify kernels affected by a given vulnerability. They’re the
releases with numbers that are lower than the one in which the problem was fixed but
Notes
e
higher than or equal to the one in which the feature or bug was first introduced. Each
version also includes a Changelog, which summarises the commit messages for the
changes made, as well as a diff, which is a text file that illustrates where the code has
in
changed. When looking for problems, this information is incredibly useful, especially
when a bug fix may be overlooked and not deemed a security issue.
nl
Using the uname -r command, you may find out what kernel version is currently
running on a specific machine:
linuxbox
O
But not everyone can live with a growing and potentially unstable kernel. $ uname
-r 2.6.28.2 linuxbox$ In truth, the great majority of large/deployed corporate installations
require the polar opposite: a robust, long-supported, and dependable system. For
ty
a production server, having a computer stop working because of a newly introduced
functionality is unacceptable. As a result, a stable team has been established tasked
with maintaining a set of feature-frozen versions. This task is usually overseen or
si
assigned to someone who selects which bug fixes and patches should be put in the
stable trunk.
Stable trees debunk our overly optimistic assumption that we can tell if a system is
r
susceptible only by glancing at its version number. Because stable releases maintain
ve
the minor version number while integrating security updates from higher releases in
the tree, our vulnerability may have been patched even though the number suggests
otherwise. Stable releases, on the other hand, guarantee that no substantial redesigns
have been incorporated and that no external patches have been made (as we will see
later in this section), so they nevertheless provide us with some assurance about what
ni
Solaris/OpenSolaris
U
recent commercial release at the time of writing. A release is a kernel version freeze in
which new features or changes are simply backported from the continuing development
tree. Large swaths of patches are delivered on a regular basis, with names like Update
1 (U1), Update 2 (U2), and so on. The most recent upgrade is U8 as of this writing.
The showrev –p tool can be used to inspect the current set of patches installed on a
m
machine.
OpenSolaris was born as a result. The OpenSolaris kernel is based on the Nevada
development tree, which arose from the Solaris 10 tree. On the Opensolaris.org Web
site, you may learn more about OpenSolaris, its licence, its connection to Solaris, and
the motivations for its establishment. In 2008, the first OpenSolaris release, 2008.05,
(c
was released. OpenSolaris releases have been announced every six months since
then.
e
System (IPS), which is comparable to those used by many Linux distributions. We shall
always refer to open source OpenSolaris systems in this book. However, because of
the osmotic relationship between OpenSolaris and Solaris 10, many of the concepts
in
covered here may also apply to Solaris (or require only slight modification).
Checking the current running version of the kernel is as simple as executing the
nl
uname -a command in Linux:
bash-4.0$ uname -a
O
bash-4.0$
The snv_ string, which identifies one of the biweekly Nevada releases, is what
ty
we’re looking for here. This is a pretty recent release at the time of writing, indicating
that the computer is running so-called development bits. The /dev repository, to which
the package management can be set to point, provides these. The /release repository
is installed by default in OpenSolaris, and it is only updated once every major release
si
of the operating system (in other words, roughly every six months, when a new release
comes out).
For paying clients, a third repository called /support is provided, which combines
r
the stability of the /release repository with bug/security fix backporting. All of this
ve
highlights a significant distinction between OpenSolaris and Linux. Kernel versions are
much more clear in OpenSolaris.
Despite the fact that anyone can compile the kernel, OpenSolaris lacks the
variety of options and combinations that Linux does. Because OpenSolaris, like
ni
Linux, values community and open development, it’s easy to keep track of changes
between releases. Each changeset, as well as the kernel, is available through a public
mercurial repository, making it simple to re-create a specific configuration. Additionally,
all modifications are tracked each build at the OpenSolaris Download Center and are
U
consider the kernel to be nearly same everywhere (in other words, those distributions
do not maintain large sets of patches to the kernel). “The Bright Side of Distributions,”
which we mentioned in the Tools & Traps sidebar, also applies to OpenSolaris.
BSD Derivatives
m
The main members of the BSD family are FreeBSD, NetBSD, and OpenBSD. We
can roughly consider all of them as derivatives of the 4.4 BSD-lite operating system,
which is the last release produced by the Computer System Resource Group at the
)A
makes it easy for navigation and a good supportive environment. The internal design
view of this operating system can be known from its architecture.
Notes
e
in
nl
O
Figure: Unix Architecture
Unix Architecture
ty
This operating system has a four-layered design. Hardware, Kernel, System Call
Interface (shell), and application libraries/tools, utilities, and so on are all part of it. The
kernel, which is at the heart of the architecture, is in charge of the computer’s hardware.
si
The kernel and other libraries communicate using system calls. These libraries
are made up of general functions and are based on system calls. Shell is a unique
programme that serves as an interface to the architecture’s other apps.
r
ve
Kernel
The kernel, as the operating system is known in UNIX, interacts directly with the
hardware and offers services to the user programmes. These user programmes do
not require any knowledge of the hardware. They only need to know how to interact
ni
with the kernel, and the kernel will take care of the rest. The fact that most well-written
user programmes are independent of the underlying hardware, making them easily
transferable to new platforms, has been one of UNIX’s biggest draws for programmers.
U
The UNIX operating system is a multi-user, multi-tasking system. Many users can
be logged into a system at the same time, each executing many programmes. The
m
kernel’s job is to keep each process and user distinct, as well as to control access to
system hardware such as the CPU, memory, disc, and other I/O devices.
)A
Kernel is the primary core of this operating system, and it interacts directly with the
machine’s hardware. Kernal’s primary functions are as follows:
●● Computer hardware such as memory, disc, printers, etc.. are controlled by the
kernel.
(c
●● The kernel schedules the processes, control and executes various user-
defined tasks.
●● Manages the data storage and control the computer accesses by several
Notes
e
users.
●● The kernel is composed of several sub-components such as configurations
in
including boot code, device drivers to control hardware, header files.
The kernel structure is built to support the most basic UNIX requirements. Which
are listed below, divided into two groups.
nl
●● Process management.
●● File management.
O
Process Management
Process management will handle resource allocation in the CPU, RAM, and
services, to name a few things.
ty
File Management
File management include keeping track of all the data in files that the process
requires, as well as interfacing with devices and controlling data flow.
si
The Shell
r
Because it shows the user with only one prompt, the shell is commonly referred
to as a command line shell. The user types a command; the shell executes it and then
ve
returns the user to the prompt when the command is complete. This is done line by line,
which is why the phrase “command line” was coined.
The shell software includes a way for customising each user’s setup needs and
ni
saving this data for later use. The user communicates with /bin/sh, which understands
the user’s commands. Internal commands (set, unset) are processed by the shell, while
external commands are referred to as programmes (ls, grep, sort, ps). There are a
variety of command line shells available (user interfaces).
U
●● Bourne (sh)
●● Korn (krn)
ity
●● C shell (csh)
The command interpreter in Windows is built on a graphical user interface. A line-
oriented command interpreter is available in UNIX.
It serves as a conduit between the user and the kernel. Shell commands are
m
used to interact with the shell. Shell has two major responsibilities: understanding user
commands and executing them using the kernel, and giving users with the ability to
write shell commands for a shell script to accomplish specified tasks.
)A
●● File opening.
●● File writing.
●● Executing programs.
(c
e
System Utilities
The system utilities are designed to be command and control tools that excel at a
in
single task (e.g., grep finds text inside files while wc counts the number of words, lines
and bytes inside a file). Instead of building a massive monolithic application software,
users can solve problems by integrating various technologies. Linux’s system utilities,
nl
like those of other UNIX flavours, include daemons, which are server programmes that
provide remote network and administration functions (e.g., telnetd provides remote login
facilities, httpd serves web pages).
O
Application Programs
The emacs editor, gcc (a C compiler), g++ (a C++ compiler), xfig (a drawing
package), and latex are some examples of application programmes (a powerful
ty
typesetting language).
UNIX is a totally distinct operating system. Rather than kernel tasks inspecting a
si
process’s requests, the process itself enters kernel space. This means that instead of
waiting “outside” the kernel, the process enters the kernel itself (i.e., the process will
start executing kernel code for itself).
r
This may sound like a recipe for disaster, but a process’ ability to enter kernel
ve
space is tightly forbidden (requiring hardware support). On x86 for example, a process
enters kernel space via system calls, which are well-known locations that a process
must invoke to enter the kernel. The hardware is switched to the kernel settings when a
process calls a system call. The process will be executing code from the kernel image
at this point. Unlike when it was in user space, it now has complete control over the
ni
Introduction
In UNIX, a file is the fundamental unit for organising data. The UNIX file system
ity
is a logical way to organise, store, retrieve, manipulate, and manage data. The files
are organised in a hierarchical file system, with directories containing files. The way
UNIX treats files in general is a significant time-saver. Physical devices for example,
are handled as files, allowing the same commands to be used for both ordinary files
m
and physical devices. For example, printing a file (on a printer) is treated the same as
showing it on the terminal screen.
)A
saved on the disc. The file system structure, the file idea in UNIX, how to handle files
and file systems, and file representation and storage will all be covered in this chapter.
Files
Notes
e
On the UNIX System, a file is the most fundamental structure for storing data (and
on Windows systems, as well). A computer file is conceptually analogous to a paper
in
document. A file is a sequence of bytes saved on a storage device, such as a hard
disc, in technical terms. Any type of data that can be represented as a series of bytes
can be stored in a file. Files include word processing documents, bitmap graphics, and
nl
computer applications.
Filenames
O
Every file has a name, which is referred to as a filename. A filename can be nearly
any combination of characters, with a maximum length of 255 characters. (On some
older versions of UNIX, if the first 14 characters of two filenames are identical, they are
regarded the same, so be careful if you use long filenames on these systems.) Except
ty
for the null letter (ASCII NUL) and the slash (/), which have specific meaning in the
UNIX file system, you can use any ASCII character in a filename. Between directories
and files, the slash works as a separator.
si
Despite the fact that UNIX permits you to include special characters in filenames,
it is best to use alphanumeric characters (letters and digits) when naming files. When
you use or show the names of files that contain non alphanumeric characters, you may
r
run into issues. Although the following characters can be used in filenames, they should
be avoided wherever possible. Many of these characters have unique meanings in the
ve
command shell, making their use in filenames tricky.
ni
U
ity
Capitalisation
In filenames, Windows does not distinguish between uppercase and lowercase
letters. You may save a file called Notes.DOC and look for it by typing notes.doc in
the search box. However, the UNIX file system is case-sensitive, which means that
uppercase and lowercase letters are separated. NOTES, NOTES, and NOTES would
m
be three separate files in UNIX. If you save a file called Music, you won’t be able to find
it if you search for music. This also applies to UNIX commands. Typing EXIT will not
work if you’re trying to log out with the exit command. By the way, since the first web
)A
server was built on a UNIX-based platform, and many web servers still run UNIX, URLs
(web addresses) can be case-sensitive.
Filename Extensions
(c
Filenames in Windows usually start with a basename, then a period, and a short
filename extension. The extension is used by many Windows apps to determine how to
use a file. A file named solitaire.exe for example, is believed to be a file named solitaire
with the extension.exe, where the.exe extension indicates that the file is an executable
Notes
e
application in Windows. It will be more difficult to operate with the file on Windows if the
file extension is changed or removed.
in
In UNIX, file extensions are more of a convenience than a required part of the
filename. They can assist you in remembering file content or organising your files,
although they are usually optional. Many UNIX filenames, in fact, do not have an
nl
extension. An executable programme for example, would usually be called solitaire
rather than solitaire.exe. Furthermore, with UNIX, filename extensions can be longer
than three characters. Some people use the suffix.backup to denote a backup copy of a
file, so notes.backup would be an additional copy of the file notes.
O
Some programmes require or output a file with a certain filename extension.
Files containing C source code for example, have the extension.c, so sorting.c is a C
language file. In the same way, web browsers anticipate HTML files to end in.html, such
ty
as index.html. Some of the most widely used filename extensions are shown in the
table below.
r si
ve
ni
U
ity
m
A UNIX file can have a lot of different extensions. For example, the file book.
)A
tar.Z was archived using the tar programme (which adds the.tar extension) and
then compressed using the compress utility (which adds the .Z). A single script can
decompress and untar the file by taking the filename as input and interpreting each of
the extensions to do the appropriate operation.
(c
The filename conventions in UNIX are sufficiently flexible to allow for some
variation in filenames. The filename of a Perl programme could be program.perl,
program.pl, or even just programme. For open files, you can even create your
own file extensions. A text file containing research notes could be called res nov,
Notes
e
ResearchNovember, or research.notes.nov. In this situation, the extension.nov only
serves as a reminder that the notes are from November. It won’t change the way you
work with the file.
in
1.2.2 Unix Paths
nl
There should be means to get to the required directories and/or files when the
complete file system is structured in a hierarchical manner. A specific route is required
to reach any certain directory and/or file. The path to a file (of any type) in a file system
O
is the path that is taken to get to that file.
The UNIX file system is considered hierarchical because directories can contain
other directories. A UNIX directory can have an unlimited number of files and
directories. These file systems are called tree-structured file systems because each
ty
directory can branch into new directories and files. In most cases, tree-structured file
systems are drawn upside-down, with the tree’s root at the top of the graphic. A typical
hierarchical tree structure is shown in the diagram below.
r si
ve
ni
U
The root of every UNIX system is a directory called /. In the example above, root
ity
contains a home subfolder. There are three subdirectories inside home, each for a
separate user on the system. One of these subdirectories is for the user with the login
name raf; it has two subdirectories (Email, Work) and a file (notes.august); and other
subdirectories or files can be found in those folders (inbox, save, sent,final, save).
m
Your home directory is the directory in which you are placed when you log in. Every
user on a UNIX system has their own home directory, which is commonly the same as
their login name. You begin each login session at your home directory and work your
way up and down the directory tree. (Some users have multiple home folders, each
)A
used for a different purpose, although this is not a problem for beginners.) A user can
only have a certain number of folders; this limit is imposed by the system administrator
to prevent a user from destroying a file system.
Pathnames
(c
Figure above shows two files with the same name, but they are stored in distinct
parts of the file system. In the Email directory, there is a save file, and in the Work
directory, there is a save file. The UNIX System allows you to specify filenames that
Notes
e
include the file’s location in the directory tree in order to identify files with the same
name.
in
This form of name is referred to as a pathname since it is a list of the directories
you pass through on your way to the file. The path across the file system begins at root
(/), and slashes separate the names of directories and files in a pathname.
nl
One of the save files for example, has the pathname
/home/raf/Email/save
O
and the pathname for the other is
/home/raf/Work/save
Full (or absolute) pathnames are those that track the path from the root to a file.
ty
A complete and unambiguous name for a file can be obtained by specifying a full
pathname. The first slash (/) in a full pathname refers to the file system’s root. All of the
other slashes separate directory names until the last slash divides the filename from the
si
directory it’s in.
When there are multiple levels of directories, such as in this filename, using
complete pathnames can be inconvenient.
r
/home/dkraut/Work/cs106x/Proj_1/lib/Source/strings.c
ve
There are two sorts of pathnames depending on the starting location from which a
path is defined.
They are
ni
●● Absolute pathnames.
●● Relative pathnames.
U
Absolute Pathnames
A typical Unix file system, the path one has to take to reach the file sample starting
from the root is /usr/mgv/sample. The path one has to take to reach the file date starting
ity
from the root is /bin/date. Such pathnames that start with a slash (/) (from the root) are
called absolute pathnames.
Relative Pathnames
m
The current directory, also known as the current working directory, is the directory
in which a user is now working. A path name that starts with the current working
directory can be used to reach a directory or file beneath the current working directory.
)A
If usr is the current working directory, then the pathname mgv/sample can be used to
access the file sample. Relative path names are pathnames that begin with the current
working directory (without expressly specifying it).
The hierarchical structure of the UNIX file system, consistent treatment of file
data, ability to create and delete files, dynamic growth of files, file data protection,
and treatment of peripheral devices (such as terminals and disc) as files are all
Notes
e
characteristics of the system. The UNIX file system may be easily understood by users
in three ways:
in
●● How files are structured in the system.
●● How files are kept on secondary storage.
●● How files are read and written.
nl
On block media like drives, the UNIX kernel preserves regular files and directories.
There could be multiple physical disc units in the system. Administrators can handle
data stored on a disc more easily by partitioning it into numerous portions. Otherwise,
O
the kernel interacts with file systems rather than discs on a logical level, considering
each one as a logical device with a logical device number.
Even though a computer system has several disc drives that contain user and
ty
system files, a user does not need to worry about which disc drive contains the file that
is needed to access because the UNIX kernel provides its services transparently and
hides the device distinction from users, even though a computer system has several
disc drives that contain user and system files.
si
A user does not need to worry about which disc drive contains the file that is
needed to access because the UNIX kernel provides its services transparently and
r
hides the device distinction from users who are familiar with MS-DOS and Microsoft
Windows know that these operating systems can have several discs or partitions, such
ve
as C:, D:, E:, and so on.
However, in UNIX, these drives or partitions are hidden from the users. All of the
disc drives or partitions can be mounted on the same file system structure, allowing
ni
access to them as directories rather than named drives C:, D:, E:, and so on. The
pathnames of files and directories on these discs or partitions can be specified as if
they were part of the file system on a single partition of a single disc. The advantage of
U
this technique is that users do not have to remember where their files (and directories)
are located on the drive or partition.
Every non-leaf node of the file system structure is a directory of files, and files at
the leaf nodes of the tree are either directories, regular files, or special device files in
the file system hierarchy. The UNIX file system is organised as an upside-down tree
with a single root node called the root directory (written as “/”); every non-leaf node of
m
the file system structure is a directory of files; and files at the leaf nodes of the tree are
either directories (Bach 2006; Floyd et al 1989; Sarwar et al 2006).
As a result, the file system structure begins with a single root directory and can
)A
have an unlimited number of files and subdirectories beneath it. A hierarchy relationship
exists between a directory and its subdirectories and files as a result of its structure.
Figure below shows how a typical UNIX system is arranged.
(c
Notes
e
in
nl
O
ty
Figure: An example of UNIX directory tree r si
ve
In above Figure, some of the standard files and directories are shown. Because
these folders and files are present in every UNIX operating system, users should be
aware of what they are, what they contain, and how they are utilised. A summary of
some of these directories can be seen in the table below.
ni
Notes
e
in
nl
O
ty
si
Note: The/bin directory is often a link to the/use/bin directory. The/etc. directory is
r
of the system administrators, so common users cannot access into this directory. The/
ve
lib directory is often a link to the/user/lib directory.
There are several files in the UNIX file systems that are worth explaining in detail:
one is passwd, and the other is unix.
Each line in the passwd file in the /etc directory contains user information for each
ni
user on the system. Seven fields are separated by colons on each line. The line should
be formatted as follows.
U
●● login name, It is the login name tghat the user types in when logging in and
ity
●● grouID. It identifies the group that the user belowng to, and it also is an integer
between 0 and 65535, with 0-99 reserved.
●● user info. It contains information about the user, such as the user’s full name.
●● home directory. It holds the sbsolute pathname for the user’s home directory.
(c
login shell. It holds the absoklute pathname for the user’s login shell. When the
user logs on, the system executes the command corresponding to the pathname listed
in this field.
For example, the following line from the etc/passwd file on the system is for the
Notes
e
user of wang.
wang:x:120:101:wang wei:/users/project/wang:/usr/bin/sh
in
where the login name is wang, the password field contains x, the user ID is 120,
the group ID is 101, the personal information is wang wei, the home directory is/users/
project/wang, and the login shell is /usr/bin/sh, which is the Bourne shell.
nl
The unix file in the root directory holds the binary program of the UNIX kernel to be
loaded into memory at system booting time. For different UNIX operating systems, the
unix file can have different names and also can be located in different directories. For
O
example, in BSD, it is vmunix in the root directory; in Sun’s Solaris system, it is unix in /
kernel directory
ty
Home and Working Directories
When a user logs in, the UNIX system creates a home directory for them. The
directory in which the user is working is termed the working directory (also known as
si
the current directory). For example, the wang directory is the login home directory for
the username wang. For the C and Korn shells, the tilde (∼) character can be used as
the shell metacharacter for the home directory. You can use instead of for the working
directory .(dot). The parent directory of the working directory is replaced with..(double
dots). r
ve
Absolute and Relative Pathnames
The pathnames are useful for accessing directories and files because the UNIX file
system is organised in a hierarchy.
ni
To indicate a file or directory location, use its pathname. A pathname serves like the
address of the directory for file in the UNIX file system. An absolute pathname instructs
the path of directories to travel from the root to the directory or file the user wants to
U
go into because the UNIX file system organises its files and directories in an inverted
tree structure with the root directory at the top. A relative pathname, which indicates the
position relative to the working directory, can also be used to locate a file or directory.
ity
A relative pathname always traverses from the current directory to the location
or file that the user desires. The slash (/) at the beginning of the pathname denotes
the root, hence an absolute pathname must begin with a slash (/). However, because
all relative pathnames begin with the working directory, they never begin with a slash.
m
A relative pathname can have distinct destinations for different users, whereas an
absolute pathname can be used by any user from anywhere in the file system structure.
Put slashes (/) between directory names in a pathname. Pathnames can be written
)A
e
The internal representation of a file in the UNIX kernel is provided by an inode,
which is a data structure that comprises a description of the file data’s disc layout, the
in
file owner, access permissions, and access timings. The term inode is an abbreviation
for index node. Every file has one inode, although it may have multiple links, each of
which maps to the inode through a pathname.
nl
When a process refers to a file by its name, which is often a pathname with several
components separated by slashes, the kernel parses the file name one component at
a time, ensures that the process has permission to search the path’s directories, and
O
finally obtains the file’s inode. When accessing files, the kernel reads the inode list from
the file system on the disc and stores it in an in-core inode table in main memory.
The file table and the per-process file descriptor table are two further data
ty
structures in the kernel that are used to manage the file system. The file table is a
global kernel structure, although each process has its own perprocess file descriptor
table. When a process uses the open or create system calls to open or create a file on
behalf of a user, the kernel allocates an entry from each table corresponding to the file’s
si
inode.
The state of the file and the user’s access to it are managed by entries in the three
r
structures — per-process file descriptor table, file table, and inode table. The file table
stores data such as the open process’s access permissions and the byte offset in the
ve
file where the user’s next read or write will begin. All open files for a process are stored
in the user file descriptor table. The kernel returns a file descriptor, which is an index
into the user file descriptor table, when the open or create system calls are used.
The kernel utilises the file descriptor to access the user file descriptor table and
ni
follows its reference to the file table when performing read and write system calls. Then
it follows the file table’s pointers to inode table entries. Finally, the kernel deduces the
data in the file from the inode.
U
1.2.4 Inode
An inode is a small table (on disc) that is associated with each file under UNIX. A
ity
file’s properties, including the structure of its data on disc, are described by an inode,
which is a record. Inodes persist on disc in a static state, and the kernel reads and
manipulates them in main memory. The following fields make up disc inodes:
●● File owner identifier: The collection of people who have access rights to a
m
access permissions to the file, including the ability to delete, write, and
execute it. Although a directory is a file, it cannot be executed; nonetheless,
execution permission for a directory grants the ability to search for a file name
Notes
e
within the directory.
●● File access times: Giving the date and time the file was last changed and
in
accessed.
Furthermore, the inode contains 15 pointers to disc blocks storing the file’s data
contents. The first 12 of these pointers (as indicated in the diagram below) refer to
nl
direct blocks, which are addresses of blocks that hold the file’s data. Because a copy of
the inode is retained in main mem.ory while a file is open, data for small files (no more
than 12 blocks) can be referenced instantaneously. If the block size is 4K, the inode can
access up to 48K bytes of data directly.
O
ty
r si
ve
ni
U
ity
The inode’s following three pointers are to indirect blocks. The indirect blocks
are each of the major block sizes if the file is large enough to utilise indirect blocks;
)A
the fragment size only applies to data blocks if the file is large enough to use indirect
blocks. The address of a single indirect block is the first indirect block pointer.
The single indirect block is an index block, which contains the addresses of blocks
that do contain data rather than data itself. Then there’s a double-indirect-block pointer,
(c
which is the address of a block that contains the addresses of blocks that contain
pointers to the data blocks themselves.
The address of a triple indirect Mock would be in the last pointer, but it is
Notes
e
unnecessary. Because the minimal block size for a file system in 4.2BSD is 4K, files
with as few as 232 bytes will only employ double indirection, not triple. Because each
block pointer is 4 bytes long, we have 49,152 (4K x 12) bytes accessible in direct
in
blocks, 4,194,304 bytes accessible via single indirection, and 4,294,967,296 bytes
accessible via double indirection for a total of 4,299,210,752 bytes, which is larger than
23? bytes.
nl
Because the file offset in the main RAM file structure is saved in a 32-bit word,
the value 232 is crucial. As a result, files can’t be more than 232 bytes long. The real
maximum file size is 2 bytes since file pointers are signed integers 232-1 (for looking
O
backward and forward in a file). For the most part, two gigabytes is sufficient.
Contents of an Inode
ty
An inode is a data structure that contains file metadata.
si
●● User ID of file
●● Group ID of file
●● Device ID
●● File size r
ve
●● Date of creation
●● Permission
●● Owner of the file
ni
Inode Table
U
When a file system is built, the inode table is created, which contains all of the
inodes. The df -i command can be used to determine how many inodes in the
filesystem are free and underutilised.
ity
m
)A
Inode Number
Each inode has a unique number, which may be viewed with the ls -li command.
(c
Notes
e
in
nl
1.3 Process
A process is a running instance of a software that is currently in use. It is
O
made composed of programme instructions, data read from files, data from other
programmes, and user input.
ty
All processes in the Unix system are generated when an existing process calls
the fork system call to establish a new one. Bootstrapping is the first process in the
si
Unix system, commonly known as process 0. Bootstrapping or booting is the process of
starting a computer. A computer does a self-test and loads a boot programme from the
boot device into memory during bootstrapping.
r
The boot software installs the kernel and provides control to it, which configures
ve
the devices, verifies hardware status, identifies new hardware, and initialises existing
devices and system processes. The kernel creates an init process with process
identification 1 after conducting these initial actions (PID 1). Process 0 is part of the
kernel and serves as a scheduling mechanism (or swapper). It also performs the
function of swapping or moving in and out of processes.
ni
While the system is functioning, the init process is always running in the
background. It is the forerunner of all subsequent processes. The getty process, which
U
allows users to log in to the Unix system, is forked by the init process. When a user
logs in, the command shell is the initial process that executes, from which additional
processes fork in response to the user’s commands, applications, utilities, and so on.
ity
Note; The process that calls for is known as the parent process and the process that
is created through fork is known as the child process. The child process is an exact
clone of the parent process. Both these processes share the same memory, registers,
environment, open files, etc. In addition, the parent and child processes ahve separate
address spacess enabling them to execute independently.
m
User mode
)A
User mode is the mode in which user-related processes are carried out. This mode
executes the user’s commands, programmes, utilities, and so on. The code in the user
mode operates under a non-privileged protection mode since these processes are
basic. When a user’s process asks services from the operating system via a system
(c
call, or when an interrupt occurs during events such as timers, keyboard, and hard disc
input/output (I/O), the operating system switches from user to kernel mode.
Kernel mode
Notes
e
System processes, that is, processes relevant to controlling a computer
system and its resources, are executed in kernel mode. In this mode, operations for
in
allocating memory and accessing hardware peripherals like printers and disc drives
execute. These operations are critical in nature, meaning that if they are not managed
appropriately, they can cause an operating system to become unstable. As a result,
nl
these processes are operated in privileged protection mode for security reasons.
O
Users execute their processes in user mode, as seen in the Block diagram of
kernel architecture below. When a user process requires a kernel service (such as
memory, a disc file, a printer, or other hardware peripherals), it uses the system call to
ty
communicate with the kernel.
Kernel-mode functions are known as system calls. As a result, the user process
shifts from user mode to kernel mode while executing system calls. The two basic
si
components that make up the kernel are shown in Figure Block Diagram of Kernel
Architecture:
r
ve
ni
U
ity
m
File Subsystem
)A
The file subsystem is in charge of the Unix system’s files. We learned in the
previous chapters that everything in Unix is in the form of files, including all devices
and peripherals. The file subsystem is in charge of managing communication between
the hardware and its associated device drivers. The file subsystem also manages the
(c
buffers that are used to store data that is either fetched from devices or is to be written
to devices.
e
All of the tasks required for proper process execution are managed by the process
control subsystem. It allots memory to the processes and schedules, synchronises
in
them, and even allows them to communicate. Processes are essentially executable files
that are used to perform specific tasks. The process control system works with the file
subsystem to load the executable file into memory and then executes it to accomplish
nl
the appropriate action. The following three modules make up the process control
subsystem:
Interprocess Communication
O
An application is frequently made up of numerous processes that run at the same
time. Furthermore, the data generated by one operation must be fed into another for
additional processing. This module synchronises and performs all of the operations
ty
required to establish communication between the various processes. Process
synchronisation means that the module maintains locks when two processes update the
same type of information, ensuring that no two processes update the same data at the
same time.
si
Memory Management
r
This module is in charge of memory management. It gives the required process
enough memory. If the memory is insufficient, it transfers a subset of the current
ve
process’ pages to secondary storage, freeing up space for the required process.
Furthermore, when the process is terminated, it frees the memory assigned to it so that
it can be used by another process.
ni
Scheduler
This module’s job is to retrieve ready-to-run processes from memory and assign
the CPU to them. Its job is to find the next process and schedule it for execution when
U
the current process is suspended for some I/O task. In addition, when some higher
priority process comes in, the scheduler pre-empts the present process and brings in
the higher priority process and assigns the CPU to it.
ity
The file and process subsystems are both utilised to manage a system’s hardware.
These work with the drivers and hardware interface (part of the kernel) to get the
hardware to do what it’s supposed to do. We’ll now go through the processes in greater
depth, including the segments that form them and the structures involved in their
management.
m
area are all part of a user mode process. When in kernel mode, the process employs
a kernel stack to address the kernel data and code sections. In a nutshell, a process is
divided into three parts:
●● Data: It is a representation of the programme variables and other data that the
Notes
e
programme code processes. It is a global content that the programme and its
subroutines can access (if any).
in
●● Stack: It is a programme segment that is used to store information about
parameters, return addresses, and other things while implementing procedure
calls.
nl
A memory heap is used to hold the dynamic structures in addition to these
three parts. These dynamic structures are those that are created during the process
execution and successfully removed once their task is completed; that is, the resources
allocated to the dynamic structures are immediately freed once their purpose is
O
completed, allowing those resources to be reused by other structures.
Process Structure
ty
A process structure is a sophisticated set of data structures that offer all of the
information needed by the operating system to manage and dispatch processes. To
keep track of that process, it comprises of an address space and a collection of data
si
structures in the kernel. The execution code, data, signal handlers, open files, and other
items are all stored in the address space. The following data structures describe the
information about processes:
Process Table r
ve
The process table (also known as the kernel process table) is a collection of
structures with one entry for each process. Every process entry is kept in main memory
because it contains process control information that the kernel needs to manage the
process. The process control block (PCB), often known as the process entry, provides
ni
Process State
U
It indicates the current status of the process, such as ready, running, waiting,
sleeping, or zombie.
It consists of the three elements listed below and uniquely identify a process:
●● User identifier (UID): This is the user ID for the person who started the process.
The process identification also includes the user’s group ID (GID), effective user
ID (EUID), set user ID (SUID), file system user ID (FSUID), effective group ID
)A
(EGID), set group ID (SGID), and file system group ID (FSGID), as well as the
user’s effective group ID (EGID), set group ID (SGID), and file system group ID
(FSGID).
●● Parent process identifier (PPID): This is the parent process’s identification, which
was used to create the process.
(c
Program counter: It saves the address of the next instruction that this process will
execute.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 41
CPU registers: It facilitates the start of the process by utilising general-purpose and
Notes
e
other registers.
in
scheduling.
nl
Accounting information: It keeps track of things like process counts, job numbers,
and CPU usage.
O
I/O status information: It keeps track of things like the list of I/O devices and the
status of the process’s open files.
User Area
ty
The Unix kernel runs in the background of specific processes. In the context of a
procedure, the user area (U area) refers to private information. The following items are
found in the U region of a process:
si
●● User IDs that determine user privileges
●● Current working directory
●● r
Timer fields that store the time the process spent in the user and kernel
ve
modes
●● Information for signal handling
●● Identification of any associated control terminal
●● Identification of data areas relevant to I/O activity
ni
●● User file descriptor table that stores the file descriptors of the files that the
process has opened
You may recall that an inode stores all of a file’s metadata, including file data,
access permissions, and access times. Inodes are preserved in the inode table. The
ity
file table and the user file descriptor table are two further file structures in the kernel, in
addition to the inode table.
File Table
m
It is a global kernel structure that stores information such as the file’s byte offset
and the place from where the next write/read operation will begin mode of opening, as
well as the reference count of all currently opened files. The permissions assigned to
)A
creates a file, which is recorded as a new entry in the user file descriptor table.
Notes
e
in
nl
O
Figure: Relation between user file descriptor tables, files table, and inode table
The file descriptor in the user file descriptor table is located for reading and writing
into a file, and pointers from it to the file table and inode table are used to access or
ty
write the file contents.
Let’s look at the next structure that keeps process-specific information after we’ve
gotten a handle on the process table.
si
Per Process Region Table
r
Because each process is associated with a per process region (pregion) table, the
kernel process table points to it. The region table for each process points to the region
ve
table to show which regions are private to it and which are shared with other processes.
This means that a region table entry may be shared with other processes as well. The
following information about a pregion is kept in the per process region table:
ni
●● A pointer to an inode of the source file that contains a copy of the region, if
any exists
●● The virtual address of the region
U
●● Permissions of the regions, that is, whether the region is read-only, read–
write, or read– execute
●● The region types (e.g., text, data, and stack)
ity
Region Table
A region is a continuous portion of the address space of a process, such as text,
data, or stack. Region table entries indicate whether the region is shared or private.
They also indicate where in the memory the region is located. The following information
m
Notes
e
in
nl
O
ty
Figure: Structures that make up a process
si
●● Region size
●● Pointers to page tables that store the region
●● Bit indicating if the region is locked r
ve
●● The process numbers currently accessing the region
created automatically when the Unix system is booted. In addition, after their tasks are
accomplished, the processes can be terminated to free up the resources they have
accumulated.
U
System resources such as memory, storage space, and CPU time are all
consumed by processes. When multiple processes are executing at the same time, the
kernel allocates system resources to one of them while keeping the others waiting.
ity
various stages of a process. The kernel keeps track of the processes in memory that
are ready to execute and schedules them according to the operating system’s logic.
)A
When the process is scheduled, it runs in kernel mode, which means it enters the
kernel running state. If a process with a higher priority is scheduled, the process can be
shifted to the pre-empted state from the kernel running state.
(c
Notes
e
in
nl
O
ty
r si
Figure: Different states of a process
ve
As a result, process switching occurs, with the current process being preempted
and another process being scheduled to move to the kernel operating state.
Furthermore, a process executing in kernel mode can switch to user mode, or the user
running state. In addition, a process in the kernel operating state can enter the sleep
ni
state while waiting for an event to occur (like waiting for the user to enter some data).
finished child process that has issued a SIGCHLD signal to its parent, allowing it to read
its exit status. Until and unless the parent reads the child process’s departure status, its
entry in the process table remains.
ity
If the event that it was waiting for occurs, the process sleeping in memory can
either be swapped out to secondary storage to sleep and swapped out or woken up to
go to the ready to execute in memory state. The process will be moved from the sleep
and swapped out states to the ready to run swapped out state, where it will wait for the
m
Note: When a process is required, the space for it is created in the primary memory and
is swapped into the primary memory by the swapped switching it to the ready to run state.
)A
When the user requires it, the process in the pre-emption state returns to the user
mode, i.e., the user operating state. When an interrupt occurs, a system call to access
operating system services is made, or a fault or exception occurs, the process running
in user mode changes to kernel mode.
(c
Note: The scheduler decides the process that has to be submitted next to the CPU for
action.
e
in
nl
O
ty
1.3.3 Type of Shells
At the very least, every Unix system has a shell. A shell is a kernel-based
si
application that functions as an agent or interface between users and the kernel,
and hence the hardware. In the MS-DOS environment, it’s similar to command.com.
A command interpreter or processor is referred to as a shell. The shell displays a
r
command line prompt (typically a $ or a percent sign) where the user can type any Unix
ve
command as soon as the system is successfully booted. After taking the command,
the shell parses it, evaluates variables (if any are available), performs command
replacement, interprets metacharacters like * and? (if any are present), and determines
the PATH. The kernel is then given this basic command line to execute.
ni
U
ity
m
)A
(c
Aside from being a command interpreter and a user-to-kernel interface, the shell
Notes
e
has certain programming capabilities of its own. Programs known as shell programmes
can be built using this feature. Shell scripts are the most common name for shell
programmes.
in
Types of Shells
Shells come in a variety of shapes and sizes. A few of them are described in this
nl
article.
O
The Bourne shell, also known as “sh,” was one of the first shells for Unix systems,
developed by Stephen Bourne at AT&T’s Bell Labs in 1977. Because of its lengthy
history of use, it is well-known among software engineers. Input and output redirection,
ty
shell scripting with string and numeric variables, condition testing, and looping are all
included.
si
was compatible with it but improved in various ways. The sh shell is still available on
Linux computers, but “bash” (the “Bourne-again Shell,” based on sh) has become
the new standard. Bash’s ability to run sh shell programmes unaltered is one of its
r
most appealing features. Shell scripts are complicated sequences of instructions that
automate programming and maintenance tasks; the ability to reuse these scripts saves
ve
time for programmers. Command completion and a command history are features not
included in the original Bourne shell.
This shell was created at UCB as part of the BSD release by Bill Joy. The C shell
gets its name from the fact that its syntax and usage are remarkably similar to that
of the C computer language. Unfortunately, not all PCs have this shell installed. The
U
Bourne shell does not support shell scripts written in the C shell. The ability to execute
programmes in the background is one of the key advantages of the C shell over the
Bourne shell. Under Linux, a free version of this shell named tcsh is available.
ity
public-domain Korn shell (pdksh) is one of its versions that is free with Linux. The shell
of Bourne-Again (bash) B Fox and C Ramey of the Free Software Foundation created
this shell. This shell is the default shell on some Linux operating system variations. It’s
)A
from the kernel of the operating system on which it is running. A system call is a method
of interacting with the operating system via programmes. When a computer software
makes a request to the kernel of the operating system, it is called a system call.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 47
The operating system’s services are provided to user programmes via the
Notes
e
Application Program Interface (API). It acts as a link between a process and the
operating system, allowing user-level programmes to request operating system
services. The kernel system can only be accessed using system calls. System calls are
in
required for any programmes that use resources.
Because the kernel maintains all system resources in UNIX, any user application
nl
request for access to a system resource must be sent to kernel code. User processes
must not have access to kernel code for security reasons. User processes can use the
UNIX operating system’s function calls, often known as system calls, to activate the
execution of kernel code.
O
As a result, the system call interface allows access to the UNIX kernel. Create
and terminate processes, create, delete, read and write files, manage directories, and
execute input and output are just a few of the functions provided by system calls. Nearly
ty
half of the approximately 64 system calls in System V are used frequently.
The set of internal algorithms that implement system calls makes up the kernel
body. About 100 system calls are defined by POSIX (Portable Operating System
si
Interface for UNIX, IEEE). POSIX-compliant programmes can easily be transferred
from one hardware system to another. Although POSIX was founded on UNIX operating
system services, it was written in such a way that it could be implemented by other
r
operating systems. Some of the POSIX system calls are listed in the table below.
ve
Calling a system in Linux entails shifting control from unprivileged user mode to
privileged kernel mode; the specifics of this transfer vary by architecture. The libraries
are in charge of gathering system-call arguments and, if necessary, organising them in
the particular format required to make the system call.
ni
●● Process Control
U
●● File Management
●● Device Management
●● Information Maintenance
ity
●● Communication
m
)A
(c
Process Control:
Notes
e
This system performs tasks such as process generation, process termination, and
so on. Fork(), exit(), and exec() are examples of Linux system calls.
in
fork()
●● The fork() system call creates a new process.
nl
●● With fork(), you can establish a new process without running a new
programme; the new sub-process just continues to run the same programme
that the original (parent) process was running.
O
●● Under process management, it is one of the most often utilised system calls.
exit()
ty
●● A program’s execution is terminated using the exit() system call.
●● After the exit() system call, the operating system reclaims resources used by
the process.
si
exec()
●● Following a call to exec, a new programme will begin to run ()
●●
r
It is not necessary to create a new process before running a new programme;
any process can call exec() at any time. The currently running application is
ve
terminated instantly, and the new programme begins to operate inside the
context of the existing process.
File Management:
ni
File manipulation tasks like as creating, reading, and writing are handled by file
management system calls. open(), read(), write(), and close() are the Linux System
calls in this case ().
U
open():
●● It’s the file-opening system call.
ity
●● This system call only opens the file; to do activities like read and write, we
must use a different system call.
read():
m
write():
●● The file is opened in writing mode using this system call.
●● With this system function, we can edit the files.
(c
●● The write() system call cannot be used by several processes on the same file
at the same time.
close():
Notes
e
●● This system call closes the file that has been opened.
Device Management:
in
Device management is responsible for device manipulation such as reading from
and writing to device buffers. The ioctl system call in Linux is used for this ().
nl
ioctl():
●● ioctl() stands for Input and Output Control.
O
●● ioctl is a system call that can be used to perform device-specific input/output
operations and other operations that aren’t supported by standard system
calls.
Information Maintenance:
ty
It manages data and its transit between the operating system and the user
software. Furthermore, the operating system stores information about all of its activities,
and system calls are used to access this data. getpid(), alarm(), and sleep() are some
si
of the system methods that fall under this category ().
getpid():
●●
r
Get the Process ID is what getpid stands for.
ve
●● The getpid() function should return the calling process’s process ID.
●● The getpid() function must always return true, and there is no return value set
aside to signify an error.
ni
alarm():
●● This system call activates an alarm clock for the delivery of a signal when it is
required.
U
sleep():
ity
●● This System call pauses the current process’s execution for a specified
amount of time.
●● During this time, another process is given the opportunity to run.
Communication:
m
These system calls are designed specifically for inter-process communication. For
inter-process communication, two models are utilised.
)A
pipe():
(c
●● Pipe() is a system call that allows Linux processes to interact with one
another.
e
●● To open file descriptors, use the pipe() system function.
shmget():
in
●● Shared memory segment is abbreviated as shmget.
●● It’s primarily utilised in shared memory communication.
nl
●● This system call is used to connect with the process by accessing the shared
memory and messages.
mmap():
O
●● To map or unmap files or devices into memory, use this function call.
●● The mmap() system call is in charge of mapping the contents of a file to the
process’ virtual memory area.
ty
Services Provided by System Calls:
●● Process creation and management
si
●● Main memory management
●● File Access, Directory and File system management
●● Device handling(I/O)
r
ve
●● Protection
●● Networking, etc.
Because the UNIX kernel does not contain many functions that are part of the
“kernel” in other operating systems, such as compiler and editor functions, which are
ni
user-level programmes in the UNIX operating system, the UNIX kernel does not contain
many functions that are part of the “kernel” in other operating systems.
When a process makes a system call, the process’s execution mode switches
U
from user to kernel mode. At that point, the operating system runs and tries to fulfil the
user’s request, failing and returning an error code. Even if no user process requests
system services, the UNIX operating system nevertheless performs accounting tasks
connected to the user process, such as process scheduling, memory management, and
ity
so on. The following are the distinctions between the two modes:
●● User-mode processes can access their own instructions and data, but kernel
instructions and data are not accessible. Kernel-mode processes, on the other
hand, have access to both kernel and user addresses. Assume that a process’
m
virtual memory address space is partitioned into addresses that can only be
accessed in kernel mode and addresses that can be accessed in either mode.
●● Some computer instructions can only be run in kernel mode (for example, to
)A
change the processor status register), and when run in user mode, they fail.
The changeover between kernel and user modes is frequently supported by the
hardware system. The kernel operates on behalf of a user process, regardless of which
of the two modes the system is in. The kernel is not a separate collection of processes
that operate in parallel with user processes; instead, it is integrated into each user
(c
process. In other words, system calls allow processes to perform actions that would
otherwise be prohibited.
Summary
Notes
e
●● Unix was seen as a system for the most technically skilled users who didn’t
care about aesthetics or user friendliness. Unix has grown to meet the needs of
in
numerous users, from the business environment to the home computer user. For
example, several Unix varieties include extensive desktop environments, and
every Macintosh sold today runs a version of Unix out of the box.
nl
●● The second generation of computer hardware was distinguished by the use of
transistors rather than vacuum tubes as hardware components.
●● An operating system is a piece of software that controls how computers work. The
O
hardware must include necessary measures to ensure the computer system’s
correct functioning and to prevent user programmes from interfering with the
system’s proper operation.
ty
●● An operating system is a resource allocator. An issue on a computer system may
necessitate the use of CPU time, memory, file storage, and other hardware and
software resources. The operating system manages resources for programmes
and users. To manage the computer system equitably and effectively, the
si
operating system must select which requests for resources are allocated.
●● A personal research project at first, UNIX has grown to play a major role in
r
operating systems on a wide range of machines, from desktop microcomputers to
massive mainframes. UNIX’s key advantages are its portability across numerous
ve
hardware platforms, vendor-independent networking, and robust application
programming interface.
●● UNIX’s adoption has not been hampered by its poor end-user interface.
ni
●● Users do not directly benefit from portability and API. However, end-users have
already experienced significant savings in computing expenses when many
suppliers offer the same operating system and software.
U
●● Customers like UNIX now because it is cheap and adaptable, not because it has
the best user interface.
●● Multiprogramming’s success cleared the way for time-sharing, a new computer
ity
●● This OS has four layers. It includes hardware, kernel, SCI (shell), application
libraries/tools, utilities, etc. The kernel is in charge of the computer’s hardware.
System calls connect the kernel and additional libraries. General functions and
)A
system calls make up these libraries. Shell is a unique app that connects the
architecture’s other apps.
●● The system utilities are command and control tools that specialise in one task
(e.g., grep finds text inside files while wc counts the number of words, lines and
(c
bytes inside a file). Rather than creating a big monolithic application software,
users can integrate multiple technologies. Daemons are server programmes that
e
remote login facilities, http serves web pages).
●● This chapter introduced you to the UNIX System concepts of files and directories,
in
as well as the basic commands that govern them. You learned to create, rename,
and move files. After learning to list files in a directory, you learned to check their
contents, search for files, and print files.
nl
●● The root of every UNIX system is a directory called /. In the example above, root
contains a home subfolder. There are three subdirectories inside home, each for a
separate user on the system. One of these subdirectories is for the user with the
O
login name raf; it has two subdirectories (Email, Work) and a file (notes.august);
and other subdirectories or files can be found in those folders (inbox, save,
sent,final, save).
●● The root of every UNIX system is a directory called /. In the example above, root
ty
contains a home subfolder. There are three subdirectories inside home, each for a
separate user on the system.
●● Command Summary
r si
ve
ni
U
ity
●● We can construct our own processes in addition to the built-in processes that are
created automatically when the Unix system is booted. In addition, after their tasks
m
are accomplished, the processes can be terminated to free up the resources they
have accumulated.
●● System resources such as memory, storage space, and CPU time are all
)A
commands, programmes, and utilities. Because they are simple processes, the
code in user mode is protected by non-privileged mode. When a user process
makes a system call to the operating system, or when an interrupt occurs during
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 53
timer, keyboard, or hard disc I/O, the operating system changes from user to
Notes
e
kernel mode.
●● System processes, which govern a computer’s system and resources, run in
in
kernel mode. In this mode, you can allocate RAM and access hardware like
printers and disc drives. These operations are crucial, and if not managed
properly, can cause an operating system to malfunction. As a result, these
nl
processes are protected by privileged mode.
●● A process structure is a sophisticated set of data structures that offer all of the
information needed by the operating system to manage and dispatch processes.
O
To keep track of that process, it comprises of an address space and a collection of
data structures in the kernel. The execution code, data, signal handlers, open files,
and other items are all stored in the address space.
●● A shell is a command interpreter or processor. On successful boot, the shell
ty
displays a command line prompt (usually a $ or %) where the user can type any
Unix command. The shell then parses the command, evaluates variables, replaces
commands, understands metacharacters like * and?, and determines the PATH.
si
This basic command line is then supplied to the kernel.
●● A system call is how a computer software requests the kernel of the operating
system for help. System calls allow programmes to communicate with the
r
operating system. A system call is made by a computer programme to the
ve
operating system kernel. The operating system’s services are given to user
programmes via API.
●● User-mode processes can access their own instructions and data, but kernel
instructions and data are not accessible. Kernel-mode processes, on the other
ni
hand, have access to both kernel and user addresses. Assume that a process’
virtual memory address space is partitioned into addresses that can only be
accessed in kernel mode and addresses that can be accessed in either mode.
U
ity
m
)A
(c
Notes
e
in
nl
O
ty
●● A system call is a method that allows a process to communicate with the operating
system. It’s the method by which a computer programme requests a service from
si
the operating system’s kernel.
Glossary
●● OS: Operating System
r
ve
●● CPU: Central Processing Unit
●● I/O: Input/Output
●● CTSS: Compatible Time Sharing System
ni
that the process requires, as well as interfacing with devices and controlling data
flow.
●● Sh: Bourne
●● Krn: Korn
(c
●● csh: C shell
e
the directories you pass through on your way to the file. The path across the file
system begins at root (/), and slashes separate the names of directories and files
in a pathname.
in
●● RAM: Random Access Memory
●● PID: Process Identification
nl
●● PCB: Process Control Block
●● UID: User identifier
O
●● GID: Group id
●● Program counter: It saves the address of the next instruction that this process will
execute.
ty
●● CPU registers: It facilitates the start of the process by utilising general-purpose
and other registers.
●● CPU scheduling information: It includes an algorithm for determining the process’s
si
scheduling.
●● Memory-management information: It keeps track of how much memory is utilised
and released by the process.
●●
r
Accounting information: It keeps track of things like process counts, job numbers,
ve
and CPU usage.
●● I/O status information: It keeps track of things like the list of I/O devices and the
status of the process’s open files.
ni
c. ICs
d. None of the above
)A
e
a. Multi-User System
b. Multi-Tasking System
in
c. Both a and b
d. None of the above
nl
4. Linus Torvalds, a Finnish student, founded Linux in:
a. 1991
O
b. 1990
c. 1992
d. 1989
ty
5. On the UNIX System, a _ _ _ _ _ _ _ is the most fundamental structure for storing
data.
a. Directory
si
b. File
c. Path
d. None of the above r
ve
6. This unix operating system has a _ _ _ _ _ _ _layered design.
a. Three
b. Five
ni
c. Four
d. None of the above
U
c. mnode
d. tnode
8. The minimal block size for a file system in 4.2BSD is _ _ _ _ _ , files with as few as
m
c. 3K
d. 4K
9. Process _ _ _ _ _ _ is part of the kernel and serves as a scheduling mechanism (or
swapper).
(c
a. 0
b. 1
Notes
e
c. 2
d. 0.5
in
10. In the kernel running state, a process can also terminate by moving to the _ _ _ _ _
_ _ _ state.
nl
a. Zombie
b. Running
c. Waiting
O
d. None of the above
11. open(), read(), write(), and close() are the Linux System calls comes under
____________.
ty
a. File Management
b. Communication
si
c. Information Maintenance
d. Process Control
12. The Bourne shell, also known as _______. r
ve
a. csh
b. sh
c. ksh
ni
d. pkdsh
Exercise
U
1. Define OS.
2. Define the history of Unix.
3. Explore UNIX Family.
ity
Learning Activities
1. Which according to you is the best in OS in 2022 and why?
Amity Directorate of Distance & Online Education
58 Unix Operating System and Shell Programming
2. What are the differences you find between windows file system and a Unix file
Notes
e
system?
3. Explain different types of shell command.
in
Check Your Understanding - Answers
1. a
nl
2. c
3. c
O
4. a
5. b
6. c
ty
7. a
8. d
si
9. a
10. a
11. a
12. b
r
ve
Further Readings and Bibliography:
1. The Linux Command Line: A Complete Introduction, William E. Shotts Jr. and
William E. Shotts, Jr.
ni
4. Understanding the Linux Kernel, Daniel Pierre Bovet and Marco Cesati
5. Linux System Programming, Robert Love
6. Linux Device Drivers, Alessandro Rubini, Greg Kroah-Hartman, and Jonathan
ity
Corbet
7. UNIX Filesystems: Evolution, Design, and Implementation, Steve D. Pate
8. Tru64 Unix file system administration handbook, Steven Hancock
m
e
Learning Objectives:
in
At the end of this module, you will be able to understand:
nl
●● Internal and External Commands
●● Shell, Current Working Directory, Referring to Home Directories
O
●● Commands to Move Around by Path Concept, Creating New Directories
●● Creating Files – touch, cat
●● Copying Files; Moving Files, Deleting Files and Directories
ty
●● View File - cat, more, pg, less
●● head, tail, Cal, banner, file, wc, sort, cut, grep, cmp, comm., diff, expr, bc, exit
si
●● Getting Online Help; Manual Pages
●● Meta Characters, Wildcards, Listing Files and Hidden Files
●● Introduction to Redirection
●● Redirecting Input
r
ve
●● Redirecting Output and Error
●● Concept of User and Group Permissions and Its Dependencies
●● Changing, Setting Permissions
ni
●● Changing Permissions
●● File Links and Types of Links
U
●● ln Command
●● Concept of Jobs and Process, Process ID; Foreground and Background Jobs
●● Suspend and Interrupt a Process
ity
●● Killing Jobs
●● Changing Password, Exit
m
Introduction
Your system administrator must create a UNIX account for you before you may use
it. Consider this account to be your office—where it’s you’ll be in the UNIX world. Other
)A
users may be working on the same system as you. There will be a network of UNIX
machines at numerous locations. You may need to know the hostname (name) of the
computer that has your account in addition to your account name.
Types of Unix files – The UNIX files system contains several different types of files:
(c
Notes
e
in
nl
1. Ordinary files: A system file that contains data, text, or programme instructions is
O
known as an ordinary file.
●● Used to keep track of your data, such as text you’ve written or an image
you’ve created. This is the type of file you’re most likely to encounter.
ty
●● It’s always inside/under a directory file.
●● There are no additional files in this folder.
●● This type of file is indicated by the “-” sign in ls -l’s long-format output.
si
2. Directories: Both special and conventional files are stored in directories. Users
acquainted with Windows or Mac OS will recognise UNIX directories as folders.
Every file and subdirectory that it contains has its own entry in a directory file. If a
r
directory contains ten files, the directory will contain ten entries. There are two parts
ve
to each entry.
(1) The Filename
(2) A unique identification number for the file or directory (called the inode number)
ni
Never include “actual” information that you would use in your business
(such as text). Basically, it is just used to organise files.
The root directory (called /) at the top of the tree is the source of all files.
ity
This type of file is indicated by the “d” symbol in the long-format output of
ls –l.
3. Special Files: Used to represent a genuine physical device that performs input/output
m
(I/O) tasks, such as a printer, tape drive, or terminal. On UNIX and Linux systems,
device or special files are used for device Input/Output (I/O). They appear in a file
system in the same way that a regular file or directory does.
)A
For each device on UNIX systems, there are two types of special files: character
special files and block special files:
●● Data is sent one character at a time when a character special file is used
for device Input/Output(I/O). Raw device access is the term for this form of
access.
(c
●● Data is sent in huge fixed-size blocks when a block special file is used for
Notes
e
device Input/Output(I/O). Block device access is the name for this type of
access.
in
It’s one character at a time for terminal devices. Raw access, on the other hand
for disc devices involves reading or writing in large chunks of data – blocks – that are
inherent to your disc.
nl
●● In long-format output of ls -l, character special files are marked by the “c”
symbol.
●● In long-format output of ls -l, block special files are marked by the “b” symbol.
O
4. Pipes: UNIX allows you to use pipes to connect commands. The pipe serves as a
temporary file that stores data from one command until it is read by another. A Unix
pipe allows data to travel in just one direction. The first command sequence’s output
or result is used as the input for the second command sequence. To build a pipe,
ty
place a vertical bar (|) between two commands on the command line. For instance,
who | wc -l
The “p” symbol is used to indicate named pipes in the long-format output of ls –l.
si
5. Sockets – UNIX allows you to use pipes to connect commands. The pipe serves as
a temporary file that stores data from one command until it is read by another. A Unix
r
pipe allows data to travel in just one direction. The first command sequence’s output
or result is used as the input for the second command sequence. To build a pipe,
ve
place a vertical bar (|) between two commands on the command line. For instance,
who | wc -l
The “p” symbol is used to indicate named pipes in the long-format output of ls –l.
ni
6. Symbolic Link: A symbolic link is a file system reference that points to another file.
Soft link is another name for a symbolic link. It includes a written representation of
the path to the file it refers to. The symbolic link appears to have its own name to
U
the end user, but when you try to read or write data to it, it will instead refer you to
the file it points to. The data file would remain even if the soft link was removed. The
symbolic file will not function properly if the source file is deleted or moved to a new
location.
ity
Symbolic links are denoted by the “l” symbol (a lower case L) in the long-format
output of ls –l.
Special files with file descriptors 0, 1, and 2 are used for these communication
m
routes. Standard I/O refers to the combination of these three files. Figure: Standard
I/O shows a model of the standard I/O. Standard I/main O’s goal is to allow every
software to accept input from any source and write output to any target or destination.
A system like this has a number of benefits. For example, the user can build and create
)A
programmes without having to worry about the input and output variances.
The input can originate from a disc file, the keyboard, or another application
(command) via a pipe, as shown in Figure: Standard I/O. Through a pipe, the output
can be sent to any of the following destinations: a disc file, the display screen, or
(c
as the default output by default. By default, the standard error is also connected to the
Notes
e
display screen.
It’s worth noting that not all commands make use of standard input and output.
in
rmdir, mkdir, cp, mv, and other commands for example, do not use standard I/O.
nl
O
ty
Figure: Standard I/O
There are three types of users in the system. The user is the first. The person who
si
generated the file is usually the system user. At any time, the user has complete power
over restricting or allowing access to the file. In addition to individual file ownership,
one or more system users might collectively own the file, which is known as group
r
ownership. If the user belongs to the group of system users who are allowed to access
ve
the file, a system user who is not the file owner can access it. The other user, who is
neither the owner nor a member of the group, is the final type of system users. As a
result, there are three types of users in the system:
●● The system user that generated the file is referred to as the user and is also
ni
write in and change the contents of a file. To delete a file with the rm command, you
must have written permission.
m
)A
The long listing command can be used to see the permissions of a file or directory.
The extensive listing of file mce1 is shown in the example below.
(c
Example
$ ls –l mce1
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 63
The long directory listing for the ordinary file mce1 is requested in this sentence.
Notes
e
It’s possible that we’ll get the following result:
in
nl
Figure: Output of the long listing command for the file mce1
It is an ordinary file, as indicated by the dash (-) in the file type field. The access
O
permissions field specifies the different types of access permissions that have been
given. The number 1 denotes that this file has only one link from the directory, implying
that it has only one name. The owner’s name is Amit; the group name that has access
to this file is mce1; the file size is 120; the date and time the file was last modified is
ty
Mar 15 12:20; and the filename is mce1.
We’ve seen that the long listing displays the rights for all three system users—
User, Group, and Other—along with other details like the file’s (or directory’s)
si
name, size, date, and last access time. Assume that the file mce1 has the following
permissions:
rwxr-xr-- 1 Amit it
r
120 Mar 15 12:20 mce1
ve
The User’s permissions are represented by the first three characters, r, w, and
x. The permissions for Group members are then listed. The permissions for the other
member are represented by the last three characters. The aforementioned output
indicates that the User has all three permissions for the file mce1: r w x (read, write, and
execute). The permissions r – x indicate that the file mce1 has read and execute access
ni
for the members of the Group. A hyphen represents the absence of permission (-). For
the file mce1, the other users have only r, or read permission.
U
The permissions for the file mce1 show that the User has r - x, or read and execute
permissions. The Group members have - - x, which means they can only execute the
file, while the Other members have no permission (- - ), which means they can’t read,
ity
When the source of the link (what is being linked to) is moved or withdrawn, these
links act differently. Symbolic links are not changed (they simply carry a string that
is the target’s path name); hard links, on the other hand, always refer to the source,
m
Let’s say we have the file a.txt. If we make a hard link to a file and subsequently
)A
remove it, we can still use the hard link to access the file. However, if we make a soft
link to a file and then remove it, we won’t be able to access it through the soft link,
and the soft link will become dangling. Hard links, on the other hand, raise a location’s
reference count, whereas soft links serve as a shortcut (like in Windows).
In UNIX, a link is a pointer to a file. Links in UNIX are pointers that point to a file or
(c
directory, just like in any other computer language. Creating links is a type of shortcut
for getting to a file. Multiple file names might lead to the same file in different locations
Notes
e
thanks to links.
There are two kinds of links: one that connects two points and the other that
in
connects two points
nl
2.1 Work on Unix Terminal
O
A terminal or a window is used by each user to communicate with the computer.
You must first connect to the UNIX computer in order to enter the UNIX environment.
(You might already have a terminal connected to the computer.) After that, you log
in to your UNIX account and start a session. Logging in accomplishes two goals: it
ty
determines which user is currently in a session and it informs the computer that you
are ready to work. You log out—and, if required, unplug from the UNIX computer—once
you’ve finished working.
si
2.1.1 Telnet Connect through Login Account, Password, Logout
When working on a Unix system, the initial step is to log in or identify ourselves
r
with the system. The administrator should provide us with a user ID and password so
ve
that we may log in to the system. When a new user is added to the system, the user ID
and password (along with other information) are assigned. We are requested to log in
as soon as we turn on the Unix system. Login appears as the login prompt.
We enter the user ID at the login box (a unique login name provided by the
ni
administrator).
We are requested for a password after entering the user ID. The password you
enter is encrypted and appears as a string of asterisk symbols in the format shown
U
below.
Example
ity
login: Amit
password: *****
Note: One of the main security features of the UNIX operating system is the displaying
m
of asterisks while typing the password and storing the actual password in an encrypted
format (also known as the hash of the password) in the /etc/shadow file that can be
accessed only by the root.
)A
In case the user ID or password is wrongly entered, we get the following error
message:
Login incorrect
(c
login:
This notification advises the user that either the user ID or password has been
entered incorrectly, prompting them to try again.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 65
We will be able to log in to the Unix system and navigate to our home directory,
Notes
e
which contains our personal files and settings, if the user ID and password are right.
Along with the shell prompt, a notice indicating when we last logged in is displayed:
in
Last login: Fri Dec 15 10:30:05 on ttys17
The date, time, and terminal from which we last signed in are displayed in this
message. The normal Unix shell prompt appears after the message, allowing us to
nl
write and execute Unix commands. The dollar sign ($) is the default Unix prompt for the
Bourne, Bash, and Korn shells. The percentage sign is the prompt in C and tcsh shells
( $ ).
O
You’re probably wondering who the administrator is. Let’s have a look at what this
phrase means.
ty
System Administrator
A system administrator is someone who is in charge of configuring and maintaining
the Unix operating system. He or she is in charge of ensuring that the Unix system runs
si
smoothly and that the system’s resources are used to their full potential. The system
administrator’s responsibilities include the following:
exit: Exiting
U
To log out of a Unix system, exit from a shell, or quit from a shell script, use the
exit command. Exiting the syntax Exit as an example Ctrl-d is a shortcut that is used to
log out of the Unix shell. Before closing the Unix system, ensure sure that all open files
have been saved and closed; otherwise, they may become corrupted. When we exit
ity
the shell, the presently executing process or command is usually stopped automatically.
We should use the nohup command to perform the task in the background even after
exiting the terminal.
Make sure you see the login prompt before you leave your terminal, signalling
m
that you have successfully logged out. If you have any unresolved processes, the Unix
system will not allow you to log off until you resolve them. Other commands, such as
“logout” or even “bye,” may be recognised by some shells to log you out.
)A
Before you log out, it’s a good idea to clear the display so that the next user doesn’t
see a screen full of information about you, your work, or your user account. You can use
your keyboard to type the command
clear
(c
right before you log out, or you can press the return key until all the information is
scrolled off the screen.
Example-1:
Notes
e
To logout from current user session:
$ logout
in
output:
nl
2.1.2 Internal and External Commands
O
One-line statements that operate on the specified operands to accomplish some
actions are referred to as commands. Each command comes with a set of options
that enhance the command’s functionality. These choices allow the user to control the
command and have it performed the desired action. The options are usually prefixed
ty
with a hyphen (-), and a command can have many options.
These commands include, among other things, listing files and directories, creating
new directories, altering directories, removing directories, creating files, examining
si
file content, copying, renaming, and deleting files, viewing system date and time, and
knowing the list of logged-in users. When dealing with the Unix operating system, the
user performs relatively basic activities.
r
Unix commands are classified into two categories: basic and advanced.
ve
Internal Commands
Internal commands are a set of built-in commands that come with the shell. Some
of them, like cd and echo, don’t create a process and are instead run directly by the
ni
shell. These commands are included in the shell and do not need to be downloaded
separately.
U
External Commands
Unix utilities and programmes like cat and ls are examples of external commands.
These commands are separated into independent files and stored in various locations.
The /bin directory contains regularly used user commands, while the /etc directory
ity
The basic Unix commands passwd, ls, mkdir, cd, rmdir, pwd, uname, touch, cat,
cp, rm, mv, ln, unlink, tput, who, finger, date, cal, echo, bc, globbing, and exit, as well as
certain line-continuation characters, will be covered in the following sections.
m
If you’re familiar with UNIX commands, you’ve probably heard of the ls command.
Because ls is a separate programme or file in the /bin directory (or /usr/bin), it is
)A
referred to as an external command. This indicates that the ls command is not built into
the shell and is instead found in a separate file. To put it another way, the ls command
will be found in /bin when you type it in. The majority of commands are external in
nature, but there are a few that aren’t really located anyplace and aren’t generally
performed even if they’re in one of PATH’s directories. Take for example, the echo
(c
command:
$type echo
e
Echo isn’t an external command in the sense that the shell won’t look for it in
its PATH (even if it’s in /bin) when you type it. Rather, it will use its own set of built-in
in
commands that aren’t saved as distinct files to execute it. Internal commands refer to
the built-in commands, of which echo is one.
You may have noticed that the shell is the one who does all of these tasks. When
nl
a user logs in, this application begins to run and ends when the user logs out. The shell
differs from other external commands in that it has its own set of internal commands. If
a command exists both as an internal shell command and as an external command (in /
O
bin or /usr/bib), the shell will prioritise its own internal command of the same name.
This is also true with echo, which can be located in /bin but is rarely used since the
shell ensures that the internal echo command has precedence over the external echo
ty
command. Now we’ll go over the internal and external commands in greater detail.
si
If you are using bash shell you can get the list of shell built-in commands with help
command:
$help
Additionally, you can use type command: to learn more about a specific command,
ni
$type cat
U
cat is /bin/cat
external type//
ity
$type cd
cd is a shell builtin
m
//specifying that cd is
internal type//
)A
Internal vs External
The question of when to use which command (internal or external) is moot because
the user chooses a command based on the requirements of the problem he wants to
address. The main difference between internal and external commands is that internal
commands are much faster than external commands because external instructions
(c
You can avoid using external in some circumstances by using internal instead. For
Notes
e
example, if you need to add two numbers, you can do so as:
in
for addition//
$let c=a+b
nl
instead of using:
O
for addition//
$c=`expr $a+$b`
In this scenario, using let, which is a shell built-in command, will be a better option
ty
than using expr, which is an external command, because let is a shell built-in command.
si
Directories
In some shell scripts, the user may need to get the current working directory. Using
the pwd command or the PWD environment variable, you can simply accomplish this.
r
To get the current working directory, you can use the built-in shell command pwd or
ve
the shell variable $PWD. We’ll look at how to use both of them in the following sections.
$ pwd
/home/ubuntu
ni
$ echo $PWD
/home/ubuntu
U
To demonstrate the two methods for getting the current directory, let’s write a shell
script.
ity
$ sudo vi current_dir.sh
m
!/bin/sh
echo $(pwd)
echo $PWD
e
echo $OLDPWD
The file should be saved and closed. We specify the shell script execution
in
environment in the above code. The pwd command is then used to display the current
path. The PWD shell variable is then used to display the current working directory.
Finally, we use the OLDPWD shell variable to display the old working directory.
nl
3. Make Shell Script Executable
Run the following command to make it executable.
O
$ sudochmod +x current_dir.sh
ty
To check the output, run the shell script.
$ ./current_dir.sh
si
current directory using pwd command
/home/ubuntu
/etc/data
ni
To retrieve more path values, you can save the result of either of these instructions
in a variable and attach strings to it. Here’s an illustration:
U
#!/bin/sh
path =$PWD
subpath=”/data”
Home Directories
The Linux home directory is a collection of individual files for a certain user of the
)A
system. The login directory is another name for it. After signing into a Linux machine,
here is the first place you’ll see. For each user in the directory, it is automatically
generated as “/home.” It is a normal root directory subdirectory. All other directories,
subdirectories, and files on the system are located under the root directory. A forward
slash (/) is used to indicate it.
(c
Except for root, the home directory can be thought of as a personal working space
for all users. Every user has his or her own directory. Two users named ‘jtp1’ and
‘jtp2’ for example, will have folders named “/home/jtp1” and “/home/jtp2.” Under their
Notes
e
directory files, these users will have full access.
Only the root (administrative) user’s home directory is stored in a different place
in
by default. The root user’s path is ‘/root/’, and it has complete power over all directories
and files.
nl
How to find the Home Directory?
The home directory can be accessed and returned to in a variety of ways. cd, pwd,
mkdir, pwd, ls, and rmdir are all useful commands for working with directories. Open the
O
files application and select the Home option from the left side menu to access the home
directory graphically. Consider the following illustration:
ty
r si
ve
ni
U
In most cases, our terminal starts with the user’s personal directory. Use the cd
command to move directories to your home directory:
cd /home
The command above will change the current directory to home. Use the ls
m
ls
)A
By using the cd command without any arguments, we can return to our home
(c
directory. From any directory we are working on, it will return us to our home directory.
Follow these steps to put it into action:
cd
Notes
e
Consider the below output:
in
From the above output, we were in /Directory/files, by executing only cd command,
we reached to our home directory. We can use the “cd ~ or cd $HOME” commands as
nl
well, to do the same. Consider the below commands:
cd ~
O
cd $Home
Execute the pwd command as follows to see the current working directory:
Pwd
ty
Consider the below output:
si
Execute the mkdir command as follows to create a directory within a directory:
mkdir<directory name>
r
ve
Output
ni
U
We generated a directory named ‘new dir’ from the preceding output and displayed
it using the ls command.
We can also delete a directory. Execute the rmdir command as follows to remove a
ity
directory:
rmdir<directory name>
Output
m
)A
(c
e
The Unix file system is a logical technique of organising and storing huge
volumes of data in an easy-to-manage fashion. A file is the smallest unit of storage for
in
information. The Unix file system includes a number of useful features. In Unix, all data
is arranged into files. All of the files are separated into folders. The file system is a tree-
like structure that organises these folders.
nl
2.2.1Commands to Move Around by Path Concept, Creating New
Directories
O
mkdir: Making directories
Syntax:
ty
$ Mkdir –[Mp] Dirname
The –m option stands for mode, and it is used to create a directory with certain
si
permissions.
The –p option stands for parent and is used to create all of the non-existing parent
directories listed in the provided path.
r
ve
The directory name, which can be either an absolute or a relative path name, is
called dirname. On a single command line, we can give several directory names.
ni
There are two types of paths: absolute and relative. The absolute path is written in
reference to the root directory and links to a specific file or directory independent of the
U
current working directory, whereas the relative path is a path for a given file or directory
in respect to the current working directory.
Always remember that an absolute path begins with a forward slash, which
ity
denotes the root directory. Furthermore, the absolute path of a file or directory remains
constant, whereas the relative path varies depending on the current directory position.
The following are some instances
(a) The following are two paths to the projects directory, assuming a directory project
m
exists inside another directory usr, existing on the root, and the current working
directory is usr: /usr/projects is the absolute path. Projects are on a relative route.
(b) Similarly, if the current working directory is projects and there is another directory
)A
experiment inside /usr, the following are the two routes to the experiments directory:
Absolute path: /usr/experiments Relative path: ../experiments
Consider the following example.
$ mkdir courses: This command creates a directory by the name courses under the
(c
current directory
$ mkdir courses faculty placement: This command will create three directories by
Notes
e
the names: courses, faculty, and placement.
The mkdir command aborts if dirname already exists and does not alter the existing
in
directory.
$ mkdir courses
nl
Since a directory with the name courses already exists, this command generates
the following error:
O
The folders are established by default with read, write, and execute rights for
owners and read and execute permissions for groups and others. However, if we
want to make a directory with a specific set of permissions, we can use the following
ty
command:
This command creates a directory country for the owner, with read, write,
si
and execute permissions; only read permission for the group; and read and write
permissions for others.
r
The option –p stands for parent and is used for creating a parent directory in the
given path.
ve
Example
$ mkdir –p university/colleges/professors
ni
There are a few scenarios in which the directory is not created and the mkdir
U
Line-continuation Characters
)A
We occasionally run across commands that are too long to fit on a single line.
The line-continuation character is used to continue such directives in the following
consecutive line. The character for line continuation is \(backslash). As a result, when
we reach the end of a line in a long command, we must type \(backslash) and push the
Enter key without any space or letter after it.
(c
The ‘>’ sign in the shell indicates that the current line continues from the preceding
line. To retrieve the command’s output, we can type the rest of the command to the right
of the ‘>’ sign and then press the Enter key when it’s finished. For a single command,
Notes
e
we can use the line-continuation character \(backslash) as many times as we want.
Using the mkdir command and the line-continuation character, the following
in
example creates three directories: courses, faculty, and placement (i.e., backslash).
$ mkdir courses \
nl
> faculty \
> placement
O
2.2.2 Creating Files – touch, cat
touch: Creating files and changing time stamps
ty
Use the touch command to create files and adjust time stamps. The term “time
stamps” refers to the last time a file was accessed as well as the last time it was
updated.
si
Syntax
●●
r
The –m option is used to change the modification time
ve
●● The –a option is used to change the access time in this case.
The time_expression that we would provide should be in the following format:
MMDDhhmm, where MM: month, DD: day, hh: hour, and mm: minute.
ni
When the touch command is used without any options or a time expression, it
creates a zero-byte file.
U
Examples
(a) $ touch Amit.txt
This produces a zero-byte file called Amit.txt.
ity
With the touch command, we can easily create many empty files.
This will set the file Amit.txt access time to Jul 12 08:20.
Note: The commands is-I and Is-Lu can also be used to set the modification time and
Notes
e
access time, respectively, of any file.
in
cat: Showing, creating, and concatenating files
The cat command is an abbreviated form of the word concatenate, which meaning
‘combine.’ We may use the cat command to not only create files and view their
nl
contents, but also to combine them. When using the cat command, the operator > can
be used to join numerous files into a single one. To append to an existing file, use the
operator >>.
O
Syntax
In the table below, the options and parameters provided in the aforementioned
ty
syntax are briefly explained:
Table: Describes the choices available with the cat command in a nutshell.
r si
ve
Showing content:We only need to specify the filename after the cat command to
ni
$ cat Amit
If more than one filename is supplied, cat will display the contents of all the files
one by one, starting with the first file and moving on to the second, third, and so on.
ity
The contents of the file Amit are displayed first, followed by the contents of the file
notes.txt
m
We use the –n option with the cat command to acquire the line numbering as well
as the file contents:
$cat –n Amit
)A
Note: We think that Amit’s file contains a handful of tabs that were put to the file on
purpose.
Creating files:We redirect the standard output to a file instead of the monitor when
Notes
e
using the cat command to create files, as seen in the following example:
$ cat >Amit
in
If we press the Enter key, the cursor will move to the next line, ready for us to write
the information we wish to save in the file Amit. Ctrl-d after a few lines of typing
nl
Example
$ cat >Amit
O
Thanks so much
ty
Ctrl-d
Showing hidden characters in files: As shown in the below figure, the following
command displays hidden characters and new lines in the form of $.
r si
ve
Figure: New lines are indicated in the form of a dollar sign ($).
ni
$ cat –veAmit
As illustrated in Fig. above, the following command displays hidden letters and tabs
ity
$ cat –vtAmit
Apart from showing the contents of a file, the cat command can also be used to
m
Concatenating files: We can use the following command to combine the contents of
two files and store them in a third file:
)A
This command saves the contents of the files Amit1 and Amit2 to the file Amit3. If
something already exists in Amit3, it will be overwritten. We should use the following
command to keep it intact while appending the contents of Amit1 and Amit2.
(c
e
cp: Copying files
The cp command is used to make a copy of a regular file(s) with a different name.
in
Syntax
nl
$ cp –[ir] srcfiledestfile
The original or source filename is srcfile, and the destination filename is destfile.
If a file with the same name as the destination file already exists, the contents of the
O
source file will be replaced without warning.
The -ioption is used for interactive copying, which means that if a file with the same
name as the destination file already exists, cp will prompt us before overwriting it.
ty
The –r option is used for recursive copying, which is useful when we want to
transfer a complete directory (including its subdirectories and files) to a different
directory.
si
Example
$ cp Amit Amit1
r
This example copies the file Amit and renames it Amit1. By examining the contents
ve
of both files, we can corroborate this. If the contents of both files are identical, it means
the Amit file has been successfully transferred under the name Amit1. We may examine
the contents of the folders Amit and Amit1 using the cat commands.
$ cat Amit
ni
$cat Amit1
ity
The contents of Amit and Amit1 are discovered to be identical, indicating that the
Amit file is transferred into the Amit1 file.
$ cp /home/Amit/ajmer/a.bat .
)A
The file a.bat is copied from the directory ajmer (a subdirectory of Amit) to the
current directory with this command. The current directory is indicated by the period (.)
at the end of the copy command.
$ cp –iAmit Amit1
If a file with the name Amit1 already exists, we will be notified with the following
Amity Directorate of Distance & Online Education
78 Unix Operating System and Shell Programming
e
cp: overwrite Amit1 (yes/no)?
If we want to overwrite the file, we must type y followed by the Enter key.
in
We use the following command to copy a complete directory and its subdirectories:
$ cp –r courses latestcourses
nl
It will create a clone of the courses directory (with all files and subdirectories) called
latestcourses.
O
mv: Renaming files
The mv (move) command is used to modify a file’s name. Essentially, the mv
command copies the file from its current location to another. The link to the previous
ty
filename is removed from the directory entry and replaced with a link to the new
filename.
si
Syntax
$ mv oldname newname
r
The filename will be changed from oldname to newname using this syntax.
ve
Example $mv Amit Amit2
This command renames or transfers the Amit file to Amit2. When we examine the
contents of the file Amit2, we find that they are identical to the contents of the file Amit,
which is displayed above.
ni
$ cat Amit2
It implies that the file Amit2 is identical to the file Amit that existed previously.
ity
$ mv Amit2 ajmer
or
m
$ mv Amit2 /home/Amit/ajmer
The file Amit2 is now in the ajmer subfolder. In the current directory, the file Amit2 is
no longer available.
)A
The files notes.txt and programs.doc are relocated to the ajmer subdirectory from
(c
e
This command clears a directory of one or more ordinary files. By deleting the file’s
pointer in the relevant directory, the file is deleted. The link between that filename and
in
the physical file is thus severed, and the file is no longer accessible.
Syntax
nl
$ rm -[irf] filename
Each filename is separated by white space in this example. In the table below, the
options and parameters provided in the aforementioned syntax are briefly explained:
O
ty
si
Table: A brief summary of the rm command’s options is given below.
Examples
We type y followed by the Enter key to remove the file (or type character n in case
we do not want to delete the file).
U
The files syllabus, notes.txt, and programs.doc are all deleted with this command.
(d) $ rm –r courses
This command deletes the courses directory’s files and subdirectories, as well as
the courses directory itself.
m
(e) The following command can be used to remove a file that is write protected
(and for which we do not have write permission).
)A
$ rm –f results
Even though we don’t have write rights, this command deletes the results file.
rm/rmdir-Remove directory
(c
rm -d dirname
Notes
e
rmdirdirname
Use the rm command with the -r (recursive) option to remove non-empty folders
in
and all files within them:
rm -r dirname
nl
If you delete a directory or a file within it that is write-protected, you will be asked to
confirm the deletion.
Use rm with the -r (recursive) and -f arguments to remove non-empty folders and
O
all files without being prompted:
rm -rf dirname
ty
Use the rm -r command followed by the directory names separated by a space to
remove multiple directories at once.
si
To match multiple directories, you can use a wildcard (*) and regular expansions,
just as you can with files.
r
2.2.4 View File - cat, more, pg, less
ve
Displaying and Concatenating Files with the cat Command
One of the most useful commands is this one. The following can be done with this
command.
ni
●● Create files.
●● Display the contents of a file.
U
●● Display the contents of more than one file, one after the other continuously.
●● Concatenate or append files
cat: Displaying the contents of a file
ity
This command is frequently used to show the contents of a file. The contents of a
file can be displayed on the screen with this command, as illustrated below. In this way,
cat is similar to the DOS type command.
$cat review
m
This is the output of the cat command, which shows the contents of the file review.
$
)A
It’s worth noting that only the contents of the file specified as the command
argument are shown. In other words, there is no display of the file name, headers, or
EOF indications. The contents of a file are usually displayed on the standard output—
the monitor—by default. The contents of a file can be forwarded onto another file if
necessary, as seen below, using the redirection operator >.
(c
$
Notes
e
The contents of the file review are written into the file review.back when the
aforementioned command is run. If review.back already exists, it will be rewritten. If
in
the file review.back does not exist, it will be created and written. Text files are shown
with cat. Any effort to show binary files with cat will result in garbage. However, ASCII
character files can be shown with the –v option.If the file myreview has lines that
nl
include the Bell character (Ctrl-G) for example, the –v option can be used to display the
file together with the visible control characters.
$cat –v myreview
O
The ASCII control character ^G (007) will
ty
$
Concatenation of Files
si
The contents of multiple files can be displayed by passing the file names as
arguments to the cat command, one after the other, as illustrated in the following
example. The file names here are sample and bio.data.
The content of the bio.data file, which was used as the second file, is as follows.
$
ni
The contents of the second file are presented right after the contents of the first
file, without any break or additional information, as seen in the preceding example.
Concatenation of files is a method of listing or showing the contents of two or more files
U
in a sequential order. The original contents of the argument files are preserved when
they are concatenated.
Appending Files
ity
The >> sign can be used to add the contents of one file to the contents of another.
$
m
The contents of the file sample will be appended to the contents of the file synopsis
when the aforementioned command line is run. The target file summary will be
)A
file names. When employed, one screen full of information is displayed at a time, as
demonstrated in the example below. The more stops after each screen full of data is
displayed, with a message appearing in the bottom left corner of the screen.
Amity Directorate of Distance & Online Education
82 Unix Operating System and Shell Programming
$more trial.txt
Notes
e
This is a pager program and is a contribution
in
contents of a file … …
– – more(15%)–
nl
As seen in the picture above, a number appears inside a parenthesis to the left
of the message. This indicates how much of the data has been shown. If all of the
information fits on a single screen, it will be displayed at the same time. When files
O
get bigger, using the space bar usually keeps the contents of the file visible. More
immediately finishes after displaying the last screen full of information.
There are several commands that can be used when there are additional pauses.
ty
The h (help) command is the most significant one. This command generates a list of all
the alternative commands that may be used at this time. The next paragraphs provide a
very brief overview of the behaviour of several of these commands.
si
Using the q (quit) command, the user can exit the more software. One can move
ahead by one page by using the command f, and backward by one page by using the
command b. The d command can also be used to advance half a page. The return key
r
can be used to advance a single line. The f and b commands both support numeric
ve
repeat factors. As a result, if the command is 4f, the display advances four full screens,
and if the command is 3b, the display advances three full screens.
The current line number can be displayed by using the = (equal to) command. The.
(dot) command can be used to repeat the previous command. The more command, as
ni
When this command is used, all of the files are displayed one by one, page by
page, beginning with the first. The more command, however, pauses after the first file
is fully displayed, and the message “––more–– (Next file: sample2)” appears. The user
ity
The ex modecommands :n and :p are used to switch to the next and previous files,
respectively. The user has the option of searching for a pattern in either the forward
or backward direction. The command /pattern is used to search forward, and the
m
The command v can be used to enter the vi editor from within the more mode, and
ZZ,:x, or:wq can be used to exit.
Using the! (bang) character and the Unix command as!command, a single Unix
command can be executed within more. More has a lot of options, but the two most
(c
important ones are –c (clear) and –s (squeeze). Instead of scrolling, the –c option
instructs more to display each screen from top to bottom. Without altering the original
Notes
e
file, the –s option shows the output with single-line spacing. A numeric option can only
display the required number of lines.
in
More is, in fact, a filter. It can accept input redirected to it via a pipe, and its output
can be redirected to another command via a pipe. Here are two typical examples:
nl
$more trial.txt | wc –l
The character | in the preceding example is a pipe symbol that gets its input from
O
the output of the command to its left and feeds it on as input to the command to its right.
The pg Command
The pg command on Unix-like operating systems displays the contents of text files
ty
one page at a time.
pg shows a text file and takes a break after each “page” (the height of the terminal
si
screen). A prompt appears at the end of each page. The user can then choose to view
the next page by pressing the newline key or one of the keys listed below.
pg reads from standard input if no file name is specified on the command line. If
r
standard output is not a terminal, pg behaves similarly to cat, but it names each file if
ve
there are more than one.
If the data comes from a pipe, pg puts it in a buffer file while reading to allow for
navigation.
Options:
U
-number The number of lines per page. Usually, this is the number of CRT lines
minus one.
-c Clear the screen before a page is displayed, if the terminfo entry for the
terminal provides this capability.
ity
-p string Instead of the normal prompt “:”, string is displayed. If string contains “%d”,
its first occurrence is replaced by the number of the current page.
)A
+/pattern/ Start at the line containing the basic regular expression pattern given.
At the prompt, type the commands shown below. Commands that begin withitake
Notes
e
a positive or negative number as an input. This parameter is interpreted relative to the
current position in the input file if it begins with + or -, else it is interpreted relative to the
beginning.
in
Commands:
nl
i<newline> Display the next or the indicated page.
id, ^D Display the next half page. If i is given, it is always interpreted relative to
the current position.
O
il Display the next or the indicated line.
if Skip a page forward, i must be a positive number and is always interpreted
relative to the current position.
ty
iw, iz Behave as <newline> except that i becomes the new page size.
., ^L Redraw the screen.
$ Advance to the last line of the input file.
si
i/pattern/ Search forward until the first or the i-th occurrence of the basic regular
expression pattern is found. The search starts after the current page and
stops at the end of the file. No wrap around is performed, i must be a
r
positive number.
ve
i?pattern?, Search backward until the first or the i-th occurrence of the basic regular
i^pattern^ expression pattern is found. The search starts before the current page and
stops at the beginning of the file. No wrap around is performed, i must be
a positive number.
ni
A letter can be appended to the search command. The pattern line is displayed at
the top of the screen if t is specified, which is the default. The letters m and b choose
the middle and bottom of the screen, respectively. The chosen position is also used in
U
subsequent searches.
q, Q Quit.
If the user clicks the interrupt or quit key while pg is reading from the input file or
)A
writing to the terminal, the prompt will appear immediately. In all other cases, these
keys will bring pg to a halt.
Example:
(a) pg myfile.txt
(c
The contents of the text file myfile.txt are displayed on the first screen, along with
a prompt (“:”). The next page is displayed by pressing the Return key, or any of the
e
(b) $pg letter.txt -10
This command shows the contents of the file letter.txt one screen page at a time,
in
with each screen page containing 10 lines.
nl
From the 25th line, this programme displays the contents of the file letter.txt page
by page.
O
This command prints the contents of the file letter.txt from the point in the file where
the word joyful appears.
ty
Your system may feature the less command to display one “page” (a terminal full
from top to bottom) of text at a time if you want to “read” a long file on the screen.
si
If you don’t have less, you’ll probably have more or pg, which are related
programmes. (In fact, the word “less” is a pun on “more,” which appeared before.) The
syntax is as follows:
less ch03
.
ity
Although less displays the file’s name as a prompt for the first screenful, the basic
less prompt is merely a colon (:). The cursor is positioned to the right of this prompt as a
m
The prompt, like practically everything else about less, can be changed. Using the
)A
less –M option on the less command line for example, causes the prompt to display the
filename as well as your location inside the file.
Most options can be temporarily set or unset from the less prompt. If you have the
brief less prompt (a colon) for example, you can type –M while less is executing. Less
responds with “Long prompt (hit RETURN),” and then prompts with the filename, line
(c
number, and % of the file seen for the rest of the session.
While less is running, press “h” (for “help”) to see all of the available less
Notes
e
commands and settings. The table below shows some basic (but still useful)
instructions.
in
nl
O
ty
2.2.5 head, tail, Cal, banner
si
head: Displaying top contents of files
The head command is used to choose a given number of lines from the file’s
r
beginning and display them on the screen.
ve
Syntax
head –[n] file name
Example
head bnk.lst
U
This displays the first ten records (lines) of the provided file when used without an
argument.
head -3 bnk.lst
ity
The first three lines of files, bnk.lst and notes.txt, will be displayed one after the
other.
)A
Syntax
tail –[ncbr] filename
e
-c Selects the last c number of characters
in
-r Sorts the selected lines in reverse order
Note; There is one more option that is used with the tail command. In gives an instruc-
nl
tion to skip n-1 lines and select the rest until the end of the file.
Examples:
O
(a) $ tail -3 bnk.lst
The final three lines will be displayed.
ty
It will display the last 11 lines.
si
It will start extracting from the thirteenth line (it will skip twelve lines) up to the end
of the file.
The head utility extracts the first 30 lines from the file a.txt and pipes them to the
tail utility, which skips the first 20 lines and extracts lines 25 to 30. The results are then
stored in file b.txt.
The cal command displays the calendar for the month and year supplied.
Syntax:
)A
Here, values 1–12 represent the month, and values 1–9999 represent the year.
Examples
(c
(a) Use the cal command without any parameters to display the current month’s
calendar (refer to Fig. Calendar of current month and specified month).
$cal
Notes
e
(b) Write the following command to see the March 2012 calendar (refer to Fig.
Calendar of current month and specified month).
in
$ cal 3 2012
(c) Specify the year in the cal command to display the calendar for the entire
nl
year, as seen in Fig. The entire year’s calendar.
$ cal 2012
O
ty
Figure: Calendar of current month and specified month
r si
ve
ni
U
ity
m
)A
(c
banner command
Notes
e
In Linux, the banner command prints ASCII character strings in large font on
the standard output. When you wish to display a string when someone logs in using
in
the terminal, this is beneficial. The character ‘#’ is used to form the strings. As we
demonstrate the command here, this will become evident.
nl
standard output in huge letters.
Syntax:
O
banner text
Example:
ty
Output:
r si
ve
(b) banner LinuxForDevices
ni
Output
U
ity
Only the first 10 characters of the string are printed in the banner, as can be seen.
This corresponds to what we saw in the man page before.
m
to do now is make sure our string doesn’t contain any words that are longer than 10
characters.
Notes
e
in
nl
O
ty
si
2.2.6 file, wc, sort, cut
file: Determining File Type
r
ve
This command detects the file type of the provided name, that is, whether it is a file
or a directory. To determine the type of a file, the command performs a series of checks
or tests on it.
ni
Syntax
file [-f filelist]files
filelist -f the type of files in the file filelist is determined. Filenames whose file type
U
Examples
ity
regular file. If matter.txt is a regular text file, it checks to see if it’s empty and displays
the message ‘empty.’
If the file matter.txt is not empty, the file command looks to see if it starts with a
)A
magic number, which is a numeric or string constant that indicates the sort of file if it is
not suitable for human reading. It’s important to remember that files that aren’t ASCII
text files, like as executables, archives, and libraries, have a magic number at the start.
If matter.txt does not contain a magic number, the file command examines the first
512 bytes of the file to determine its language (i.e., whether its language is English, a
(c
programming language, or some other special data). The file command reveals the type
of the supplied file based on all of these assessments.
(b) The following example displays the file type of all the files and directories in
Notes
e
the current directory.
$ file *
in
(c) The file type of all the files mentioned in the file, filenames, is displayed in the
following example.
$ file –f filenames
nl
O
ty
r si
ve
ni
U
ity
m
)A
The file letter.txt is defined as the command text in this output of the file command
since it has the execute permission.
(c
It’s used to sort files either line-by-line or by specific fields, with fields referring to
Notes
e
words separated by one of the following: white space, tab, or special symbol.
Syntax
in
sort [-n][-r][-f][-u] filename
nl
O
ty
si
The first character of each line in the filename will be used to sort the lines in
alphabetical order.
r
The following is another way to use the sort command:
ve
Syntax:
sort +p1 - p2 filename
ni
This restricts the sort to characters beginning with field p1 and ending with field p2.
If p2 is not specified, the characters from field p1 to the end of the line will be used to
sort the data.
U
Examples
(a) $ sort +3 -5bnk.lst
This command skips the firstthreefields and uses the fourth and fifthfields for
ity
lst.
e
This command sorts the filebnk.lst from the third to the fifthfields.
in
This command sorts the filebnk.lst on the fifthfield after ignoring leading blank
spaces. The -f option is used to ignore the upper- and lower-case distinction.
nl
(h) $ sort +3bf bnk.lst
The command will sort the fourthfield after ignoring leading blank spaces and sort
the upper- and lower-case data together. The -n option is used for sorting the file on the
O
basis of numerical values rather than ASCII values.
ty
numerical field. The -r option is used for sorting a given file in reverse order.
si
The command will sort the filelink.lst in the reverse order. The -u option will
eliminate duplicate lines in the sorted output.
The wc command is used to determine the number of source code lines in any
Unix-based application software. By default, it displays all three counts of a file:
ni
Syntax
U
wc [options] filename
Examples:
(a) $ wcphone.lst
The number of lines, words, and characters in this file will be displayed. If we only
want to see the number of lines, words, or characters in the file, we can use the –l, –w,
(c
The number of lines in the file will be displayed. Similarly, the –w option returns the
Notes
e
total number of characters in a file, whereas the –c option returns the total number of
words in a file.
in
(b) The following script creates a file book from the three files, chapter 1, chapter
2, and chapter 3, and counts the number of lines in it.
mergefiles
nl
#!/bin/bash
O
cat course.txt >> book
ty
n=`cat book | wc –l’
$./mergefiles
si
Output
r
There are 13 number of lines in the file book
ve
Note; wc-I counts the number of lines in the output of the cat book command (contents
of book file). The output is then stored in the variable n.
(c) The following script counts the number of persons logged in.
ni
Countlogged
#!/bin/bash
U
n=`who | wc –l’
Output
ity
Slicing (cutting) a file vertically is done with the cut command. Both the fields and
m
Syntax:
)A
The following are the options available when using the cut command:
(c
Notes
e
in
nl
Example:
O
(a) $ cut –c 7–21,20–25bnk.lst
This retrieves characters 7–21 and 20–25 columns (characters) from the filebnk.lst.
ty
We get field number 3 of the file bank. lst provided the fields are separated by tab
space.
In most cases, files do not include fixed-length records, and fields may be
si
terminated by characters other than tabs, such as ‘,’ and ‘|’. As a result, we define the
field delimiter with –d (delimiter).
101 Anil
102 Ravi
103 Sunil
ity
104 Chirag
105 Raju
101 2429193
102 2627312
)A
103 2456789
104 2646189
105 2547856
(c
(d) If we only want to remove the second word (field) from the file Names, we may
use the command below.
e
We get the following output on the screen.
Anil
in
Ravi
Sunil
nl
Chirag
Raju
O
(e) $ cut –f2 Name >nn
(f) $ cut –f2 Telephone >tt
ty
The names and phone numbers will be saved in two separate files, nn and tt.
si
grep
The grep programme looks for lines with the pattern n in a file or files. The syntax
is:
The most basic application of grep is to seek for a single-word pattern. It can be
used in a pipe to send to the standard output just the lines of the input files that contain
a specific string. But first, let’s look at an example of reading from files: For example,
ity
search all files in the working directory for the word Unix. To quickly grep all filenames in
the directory, we’ll use the wildcard *.
m
)A
When grep searches many files, the filename where each matching line of text is
found is displayed. If you don’t give grep a filename to read, it will read standard input
instead; this is how all filter programmes work:
(c
Notes
e
in
nl
To list your directory, the sample uses ls –l. grep is fed the standard output of ls –l,
which only displays lines containing the string Aug. (that is, files that were last modified
in August). Those lines end up on the terminal screen since grep’s standard output isn’t
O
redirected.
The grep options allow you to fine-tune your search. Some grep options is a table
that lists some of the options.
ty
Table: Some grep options
Option Description
si
–v Print all lines that do not match pattern.
-n Print the matched line and its line number.
-1
-c
r
print only the names of files with matching lines (lowercase letter “I”).
print only the count of matching lines.
ve
-i match either uper-or lowercase.
Next, let’s use a regular expression that tells grep to find lines with carol, followed
by zero or more other characters (abbreviated in a regular expression as “.*”),* then
ni
followed by Aug:
$ ls -l | grep “carol.*Aug”
U
$
ity
Example:
m
)A
(c
This example searches the file bank.lst for a line with the pattern mic/Mic and any
Notes
e
number of repetitions of the character c, then r/R, and finally chip. Nothing will display
in the output since the file bank.lst used in the command does not contain the pattern
Microchip.
in
(b) $ grep current bank.lst
nl
Output
101 Aditya 0 14/11/2012 current
O
(c) $ grep current bank.lstbnk.lst
Both the files bank.lst and bnk.lst are searched for the string current, and the
filenames are displayed on the left side of the result.
ty
cmp
The cmp command compares two files and displays the line number where the
si
first difference appears. If the files being compared are identical, the cmp command
produces no output.
Syntax r
ve
cmp [[-l][-s]] file1 file2 [skip1] [skip2]
ni
U
ity
Example
m
The commands that are used to compare the two files are listed below.
(c
The cmp command compares the files users.txt and customers.txt and prints the
Notes
e
results.
in
The byte point where the first difference between the two files (users.txt and
customers.txt) occurs is 6, according to the report.
nl
For each difference detected in the two files, the following example provides the list
of byte positions and the differing byte values in octal format:
O
As a result, we receive the following output:
ty
r si
ve
Fig: List of byte locations and the differing bytes in the two files users.txt and
customers.txt
The output reveals the byte positions of the difference between the two files,
ni
as well as the fact that the users.txt file is smaller than the customers.txt file when
compared to the content of customers.txt.
The status obtained after comparing the two files is shown in the example below.
U
The exit status is returned in this example. We get a result of one when we display
the exit status value, confirming that the two files are not identical but distinct.
ity
m
The next example compares the two files after removing the offsets of 12 and 14
bytes from each file.
We get an output 0 when we display the return status (Fig. Comparison of files and
(c
display of status), confirming that the two files are identical after the offset values are
entered.
comm.
Notes
e
The comm command shows or hides the material that is shared by two files. The
output is presented in a column format, with the first column representing output from
in
the first file and the second column representing output from the second file.
Syntax:
nl
comm [-1] [-2] [-3 ]file1 file2
O
ty
si
Note: When the comm command is executed without any options, the output will com-
prise three columns, where the first column displays content unique to the first file, the
r
second column displays content unique to the second file, and the third column displays
content common to both the files.
ve
Examples:
Let’s say we have two files with the following content: users.txt and customers.txt.
ni
U
(a) The output of this example compares the two files (users.txt and customers.
txt) and is displayed in three columns. The first column shows content that is
ity
unique to the first file, the second column shows content that is unique to the
second file, and the third column shows content that is shared by both files
(Fig: Output of the comm command on comparing two files)
m
)A
(c
(b) This example compares the two files, users.txt and customers.txt, and
Notes
e
suppresses the unique material in users.txt while displaying the unique
content in customers.txt (Fig: Output of the comm command on comparing
two files).
in
$comm -1 users.txt customers.txt
(c) This example compares the two files users.txt and customers.txt, suppressing
nl
unique text in customers.txt while displaying unique content in users.txt (refer
to Fig. Output of the comm command on comparing two files).
$comm -2 users.txt customers.txt
O
(d) This example compares the two files stated above and removes the common
material found in customers.txt and users.txt (refer to Fig. Output of the comm
command on comparing two files).
ty
$comm -3 users.txt customers.txt
diff
si
When comparing two files, the diff command is used. The command produces no
output if there are no differences between the two files being compared. Otherwise, it
shows the output showing the modifications that must be done to the first file in order
for it to match the second file. r
ve
Syntax:
diff file1 file2
ni
All of the differences between the two files are displayed in a format that consists
of two numbers separated by a character. The line number in the first file is represented
by the number to the left of the character, while the line number in the second file is
represented by the number to the right of the character. Any of the following characters
U
1. d: delete
ity
2. c: change
3. a: add
Example
m
Assume we have two files with the following content: users.txt and customers.txt.
)A
When the two files are compared, we get the following result.
(c
2c2
< Peter
Notes
e
--
> Charles
in
Note: The<character precedes the lines from the first file and >precedes the lines from
nl
the second file.
The difference between the two files is only one line, according to this output. It
means that if the second line in the first file (users.txt) is replaced with the second line in
O
the second file (customers.txt), both files will be identical.
Let’s change the text of the first file users.txt to better understand the diff command:
users.txt
ty
John
Peter
si
Charles
When we compare the two files, keeping the content of customers.txt the same as
r
previously, we get the following result.
ve
$ diff users.txt customers.txt
2d1
< Peter
ni
3a3
> Troy
U
To make users.txt the same as customers.txt, delete the second line, Peter, and
add the third line, Troy, from customers.txt after the third line in users.txt, according to
the output.
ity
Here, arg1, arg2, ... represent the arguments that combine with operators to form
)A
Examples:
(a) $ i=2
(c
(b) $ j=4
Output
Notes
e
expr $i + $j
in
The aforementioned example adds the values of variables i and j. Similarly, the
following example displays the value of the addition of the values of variables iand j
nl
assigned to variable k.
$ echo $z
O
8
ty
$ echo $z
si
The following example adds constants 2 and 4.
$ expr 2 + 4
8
r
ve
The following example multiplies values 5 and 3.
$ expr 5 \* 3
15
ni
Note: Asterisk (*) refers to all files in the current directory. Hence\(backslash)is used to
escape it, that is, \*is treated as the multiplication symbol.
bc
ity
By typing the bc command at the shell prompt, the calculator (or base conversion)
mode is activated, and the $ prompt is removed. The calculator’s input is taken line by
line. We must first enter an expression, following which the result is produced by the
Unix command.
m
Examples
(a)
)A
$ bc
10/2*2
10
(c
2.5*2.5+2
8.25
quit
Notes
e
On typing quit, the bc command ends
in
Note: In order to get precise answers while working with a floating point number, we
set the variable scale to a value equal to the number of digits after the decimal point.
(b)
nl
$ bc
scale=1
O
25/2
12.50
ty
If the answer of an expression is greater than the scale value set after setting
the scale variable, the scale value is ignored and the right answer is displayed. Base
conversion is another useful function of bc.
si
(c)
$ bc
ibase = 2
r
ve
obase = 16
1010
A
ni
quit
When the variables ibase and obase are set to 2 and 16, all input values are
converted to binary numbers, and all output values are presented in hexadecimal
U
format.
Functions like sqrt, cosine, sine, and tangent are likewise supported by bc. sqrt() is
a built-in function, however s() and c(), which stand for sine and cosine, will only work if
ity
Exit
The exit command ends a programme early. When a script encounters an exit
m
Syntax exit
)A
Examples:
(a) 16 % 4 will return 0
(c
(c) The script below is used to enter a number and determine whether or not it is
Notes
e
a prime number.
fi ndprime
in
#!/bin/bash
nl
read n
i=2
O
while [[ i –ltn ]]
do
((m = n % i))
ty
if test $m –eq 0
then
si
echo “The number, $n is not prime number”
exit
fi r
ve
((i=i+1))
done
Output
Enter a number: 15
U
Enter a number: 19
ity
If you are new to the LINUX operating system and are having problems using
the command-line utilities supplied by LINUX, you should first learn about the help
command, which, as its name implies, assists you in learning about any built-in
command.
(c
As previously stated, the help command simply shows information on shell built-in
commands. The syntax is as follows:
e
$help [-dms] [pattern ...]
The pattern supplied in the syntax above relates to the command you want
in
to learn more about, and if it matches any shell built-in command, help will offer you
more information about it; if it does not, help will produce a list of help subjects for your
convenience. The d, m, and s in this example are help command options.
nl
Using help Command
To make you understand more easily about what help command does let’s try help
O
command for finding out about help itself.
// using help
$help help
ty
help: help [-dms] [pattern...]
si
Short summary of built-in commands are displayed. If PATTERN is specified, full
help will be provided for all commands that match PATTERN; otherwise, a list of help
topics will be printed.
r
ve
Options:
PATTERN
U
Arguments:
Exit Status:
ity
/* so that’s what help command does telling everything about the command and
helping you out */
m
Using -d: This option simply tells you what a command does without going into
depth about its alternatives or other information.
e
$help -d help
in
Using -s: This is the option to use if you only need to know the syntax of a
command.
nl
// using help with -s
$help -s help
O
help: help [-dms] [pattern ...]
ty
// using help with -m
$help -m help
si
NAME
SYNOPSIS
r
ve
help [-dms] [pattern ...]
DESCRIPTION
Options:
ity
PATTERN
Arguments:
Exit Status:
SEE ALSO
(c
bash(1)
IMPLEMENTATION
Amity Directorate of Distance & Online Education
108 Unix Operating System and Shell Programming
e
Copyright (C) 2013 Free Software Foundation, Inc.
in
Manual Pages:
nl
The man page (sometimes known as the manual page) is a documentation
handbook for various commands used in unix and unix-like operating systems.
Man command name can be used to check any command’s manual entry.
O
Let’s consider printF command for demonstration:
man printf
ty
Output
PRINTF(1) User Commands PRINTF(1)
si
NAME
r
ve
SYNOPSIS
printf OPTION
ni
DESCRIPTION
U
--version
\” double quote
(c
\\ backslash
\a alert (BEL)
Notes
e
...
in
If you look closely at the output above, you’ll notice that the top line contains
PRINTF(1), and the 1 in braces is the kind of man entry. The number corresponds to a
nl
certain portion of the manual page.
On Research Unix, BSD, macOS, and Linux, the documentation is divided into
eight sections:
O
Section Description
1 General Commands
2 System Calls
ty
3 Library functions, covering in particular the C standard library
4 Special files (usually devices, those found in /dev) and drivers
si
5 File formats and conventions
6 Games and screensavers
7 Miscellanea
8 r
System administration commands and daemons
ve
Continuing with the PRINTF example, try the following command for all entries:
man -a printf
ni
Output
PRINTF(1) User Commands PRINTF(1)
U
NAME
ity
SYNOPSIS
m
printf OPTION
)A
DESCRIPTION
Notes
e
--version
in
FORMAT controls the output as in C printf. Interpreted sequences are:
nl
\” double quote
O
...
When you type q to leave, the text below shows on the terminal; hit enter to see
another printf entry.
ty
--Man-- next: printf(3) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
Output Continued
si
PRINTF(3) Linux Programmer’s Manual PRINTF(3)
NAME r
ve
printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf, vsprintf, vsnprintf -
formatted output conversion
ni
SYNOPSIS
#include
U
#include
)A
...
Notes
e
Some useful man command options:
1) -f option
in
man -f printf
Output:
nl
If printf is available, it will provide a short summary of it, comparable to
O
2) -k option
ty
man -k printf
Output:
It will look for keywords in the short manual page descriptions and display any
si
matches.
3) -w option
It prints the location of cat files that will be displayed rather than the content of files.
ity
man -w printf
Output:
/usr/share/man/man1/printf.1.gz
m
4) -K option
man -K printf
Output:
It will display all man entries containing the printf keyword, and you can press enter
(c
to view the second entry after each entry has been displayed.
The command printf prompts you to examine the second entry; you may either skip
Notes
e
it or quit to end the command.
in
2.2.10 Meta Characters, Wildcards
Meta characters are unique characters that represent something other than
nl
themselves. Meta characters are characters that are neither letters nor numerals, as a
rule. The shell, like grep, sed, and awk, has its own collection of meta characters known
as shell wildcards. Shell meta characters can be used to group commands, abbreviate
O
filenames and pathnames, redirect and pipe input/output, and run commands in the
background, among other things.
ty
programmes like grep, sed, and awk to match patterns. Shell meta characters should
not be confused with these.
r si
ve
ni
U
ity
m
)A
(c
Notes
e
in
nl
O
ty
To make working with files easier, shells support a large number of wildcards. A
wildcard is a shell expression that expands to a number of file names — that is, to all
si
files that match the expression.
Zen-like quality to it. To make a more particular phrase, you usually need to pair a star
with some other characters. *.txt for example, matches all file names that end in.txt,
including the following:
U
.txt
a.txt
a_very_long_name.txt
ity
A_FILE_NAME_WITH_UPPERCASE_LETTERS.txt
The * denotes that you are unconcerned with the letters preceding the.txt.
An phrase like *.txt will usually match all text files. The wildcard expression can
m
be fine-tuned even more. a*.txt for example, matches all file names that begin with the
letter a and end with.txt. The * indicates that you are unconcerned about any letters in
between. Using the prior list of files, a*.txt would match just the following:
)A
a.txt
a_very_long_name.txt
(c
Use the expression [Aa]*.txt to get files that begin with an uppercase A or a
lowercase a and conclude with.txt. The following files would be matched by this
Notes
e
expression:
in
a.txt
a_very_long_name.txt
nl
A_FILE_NAME_WITH_UPPERCASE_LETTERS.txt
You can use the star many times, such as in the expression a*v*.txt. In the sample
list of files, this expression would only match one file:
O
a_very_long_name.txt
ty
Each software on MS-DOS must support wildcards on its own. A small number of
wildcard expressions are supported by a few DOS commands, such as DIR. With Unix
si
and Linux, however, the shell supports wildcard expressions so that each programme
does not have to.
Using Wildcards r
ve
The ls command might help you understand how wildcard expressions work.
For example:
ni
U
How It Works
The ls command in this example lists files in the /usr/lib directory, which has a large
number of files, the majority of which begin with the lowercase letter l. (short for library).
In this scenario, the shell will expand the wildcard expression to include all files with
ity
names that begin with the lowercase letter l, have a lowercase z somewhere in the
name, and conclude with.a (a common file-name extension for libraries).
Notes
e
in
nl
O
ty
This command displays a list of files in the /usr/lib directory that begin with a
lowercase l and end with an uppercase A or a lowercase z. This phrase, unlike the
previous one, does not require the file to finish in.a, or any other extension for that
si
matter.
Inside the square brackets, you can use more than two letters. Consider the
following scenario:
r
ve
ni
This example lists all files in /usr/lib that begin with the letter l, end with.a, and
contain an uppercase A, F, L, or lowercase z in their names.
The ? Wildcard
U
The question-mark expression,?, matches exactly one character, whereas the star
expression matches all or nothing. To narrow down a huge list of file names to a few,
you may need to use the question mark.
ity
In addition, while working with dot files, the question mark comes in handy. Dot files
and directories are those that begin with a period, or dot. Dot files are generally hidden
on Unix and Linux systems. Unless you specifically ask for dot files, the ls command for
example, will bypass them.
m
A major issue with dot files and wildcard expressions is that the current directory
has a single period (.) and the parent directory has two periods (..), which poses a
major problem when using a wildcard expression like.* to list all files that begin with
)A
a dot. The shell can simply treat all files in the current directory as having a name that
starts with./, as in./a.txt. ../file name, such as../a.txt, can also be used to access files in
the parent directory.
Use the question-mark syntax if you only want to look at dot files and directories.
(c
In this situation, you can start with all files with names that begin with a period and
include at least two additional characters, removing the. and.. directories. Consider the
following scenario:
Amity Directorate of Distance & Online Education
116 Unix Operating System and Shell Programming
$ ls .??*
Notes
e
Hundreds of files matching this expression are likely to exist on a normal Linux
machine. (By default, Mac OS X computers have significantly fewer dot files and
in
directories.) You can improve expressions after you have a clearer sense of what files
are available. Consider the following scenario:
nl
O
ty
This command displays all files that begin with the letter.j, or all files in a directory
that begins with the letter.j.
si
The ls command displays the contents of the disk’s files and folders. The files
and directories are presented in alphabetical order by default. If no directory name
r
is supplied, the list of files and directories in the current working directory will be
displayed.
ve
Syntax:
$ ls -[options]
ni
U
ity
m
)A
We can also utilise wild-card characters when listing and searching for files and
folders. These characters aid in the discovery of files and directories that start with a
specific character(s), have a specified character(s) or a range of characters in their
names, or have names of a specific length, among other things. They make it simple
(c
and quick to find the files and directories you’re looking for.
For the purposes of understanding these options, assume that when we run the ls
Notes
e
command in the current directory, we get a list of the following files and directories:
in
$ls
courses
nl
notes.txt
programs.doc
O
university
..
We may use the following command to get all the files that start with a given
ty
character.
Syntax :
si
$ ls charactername*
Example
r
ve
We can use the following command to get a list of all files that start with the letter n.
ni
$ ls n*
notes.txt
U
We give the command in the following form to get all the files in a particular region
that start with a certain character.
ity
Syntax:
m
$ ls [c1-c2]*
The beginning and ending characters of the range are represented by c1 and c2,
)A
respectively.
Example
We may use the following command to get all the files that start with the letters a
(c
through d.
$ ls [a-d]*
Notes
e
courses
Similarly, we may acquire the needed files by using the wild-card character?, which
in
signifies a single character. For example, if we want to collect all the files that start with
character a and have three characters, we can use the command:
nl
$ ls a??
We will not obtain anything as the output because none of the files fit these
conditions (assuming no filename exists that is three characters long and begins with
O
character a).
We can use the following command to acquire all the files that start with the
character a and end with any digit:
ty
$ ls a[0-9]*
As we can see, no file in our list of folders begins with the letter a and is followed
by a digit, hence no output is generated.
si
We may receive the following output if we use the –l option for long listing:
r
ve
File permissions, number of links, owner, group, size, date and time, and file/
ni
We use the –a option to see all files, including those that are hidden. The following
U
is the result:
ity
m
Note: Filenames that begin with the dot (.) are considered hidden files in Unix.
)A
The names of files and directories are ordered alphabetically by default. We may
use the –t option to arrange them by modification time; the most recently produced file
is presented first.
(c
Notes
e
in
We can use the I option to acquire the inode number of the provided file, as
nl
illustrated here:
$ ls –li programs.doc
O
The inode number for the file programs.doc is 39984.
ty
Stream is an intriguing mechanism in Unix. Streams are nothing more than a
collection of bytes. A file is made up of a series of bytes, as you may recall. As a result,
si
the distinction between a stream and a file is blurred. There are three types of streams
in Unix, each of which consists of files that serve as communication channels between
an input source and a command, as well as a command and a destination. Standard
input, standard output, and standard error are the three types of standard input, output,
and error. r
ve
2.3.1 Introduction to Redirection
A programme takes its input via standard input from any one source, the
ni
keyboard being the default source, and sends its output via standard output to any one
destination, the display screen being the default destination, as stated in the previous
section.
U
It is possible to change the source, which is where a programme receives its input,
as well as the destination, which is where the programme sends its output. Redirection
is a mechanism for changing the input source and/or output destination.
ity
Using the (less than) operator, the input source is redirected (to something other
)A
than the default, which is the keyboard). The > (greater than) or >> (twice greater than)
operators are used to redirect the output destination. The shell prefixes the redirection
operators and > with the file descriptors 0 and 1, respectively. When the file descriptor
2 (which represents the standard error file) is necessary, it must be indicated explicitly.
(c
A program’s output can be redirected using the > or >> operators. The usage of
> causes destination files to be overwritten. The current output will be appended to an
existing file when >> is used. If the destination file does not exist, it is created in either
Notes
e
scenario. It’s vital to remember that redirections are handled by the shell.
The input has been diverted from a file called sample in the following example. The
in
file name is not displayed because the shell (not the command) accesses the file.
$wc< sample
nl
3 20 103
The output has been routed to a file called newsample in the following example.
O
$wc sample >newsample
ty
In a single command line, you can use both the <and > redirection operators.
Despite the fact that such combinations are possible, the sequence in which the input
and output files are provided is not limited, as illustrated in the following example.
si
$wc< sample >newsample
Or
$wc>newsample< sample r
ve
Or
$ >newsample< sample wc
When commands like the ones listed above are issued, the input redirection
ni
operator will be handled first, followed by the > operator. This is because the <operator
takes precedence over the > operator in such situations.
Or
It should be noted that there should be no blanks between the file descriptor and
the redirection operator whenever file descriptors are used in a command line. As a
m
result, there is no blank between 2 and > in the first case and 2 and >> in the second
case in the aforementioned example.
In the first case, the value is between 2 and >>, while in the second case, the value
)A
is between 2 and >>. Here are a few more illustrations. The concept of redirection can
be better understood by looking at these illustrations.
1. $wc sample
(c
3 20 103 sample
The input file name has been specified manually in this case, while the output file
Notes
e
has been selected by default. The filename sample is presented as normal when the wc
command opens the file.
in
nl
O
No unique input filename has been utilised in this example. As a result, it obtains
its input from the keyboard, which is the default standard input. The input has been
taken as whatever is written in at the terminal till a new line is entered in. By default, the
ty
output file has been taken. The output does not include the filename. This is due to the
fact that the shell has already opened the file (not the command).
si
$
The contents of the conf file are copied into the meetings file in this example. The
r
meetings file is generated if it does not already exist. If a file named meetings already
exists, it will be rewritten.
ve
4. $cat conf >> meetings
$
ni
If the meetings file already exists, the contents of the conf file are appended to it.
The meetings file will be generated if it does not already exist.
U
Syntax :command<input_file
The name of the file from which the data will be sent to the command for
computation purposes is input file.
m
Examples
(a) $ sort < kk
)A
The sort command in this example takes a stream of bytes from the file kk as input.
Input and output redirection operators can also be combined.
Nothing will appear on the terminal screen when you run the command; instead,
(c
the contents of the file kk will be sorted and delivered directly to the file mm.
e
We can redirect standard error in addition to redirecting standard input and output
for a script or procedure. Despite the fact that standard error is sent to the same
in
location as standard output - the shell window or terminal. There are excellent reasons
for treating stdout and stderr separately.
The main reason for this is that while we can redirect the output of a command
nl
or instructions to a file, you have no way of knowing if an error happened. Separating
stderr and stdout allows you to see the error message on your screen while still writing
to a file.
O
The output redirection operator is used to redirect output (from a command) that
would normally be sent to a terminal to a file. Output redirection is the process of
redirecting output from its default destination. The ‘>’ operator in shell commands must
ty
be used to redirect the output. The output redirection operator is represented by the ‘>’
symbol, and it can be used to redirect the output of any command to a file rather than
the terminal screen.
si
Syntax to redirect stderr from a command to a file.
# command_options_and_agruments 2>output_file.
r
The 2 in 2> refers to the file descriptor 2, the descriptor number for stderr.
ve
Example:
To redirect standard error to the same location as standard output, use the 2>&1
ity
syntax.
Example: 1
linuxtechi@localhost:~$ ls /usr/bin > command.txt 2>&1
m
Example: 2
(c
e
ls: cannot access /usr2222/bin: No such file or directory
Note that the preceding example implies that your system lacks the “/usr2222/bin”
in
directory.
nl
linuxtechi@localhost:~$ ls /usr2222/bin &> command.txt
O
ls: cannot access /usr2222/bin: No such file or directory
The command ls is used in the above command, the parameter to the ls command
is /usr2222/bin, and ‘&> command.txt’ redirects both stdout and stderr to a file named
ty
command.txt.
Appending To Files
si
Use the ‘>>’ operator to redirect a command’s output but append it to a file if one
exists. The following is the syntax:
# Command >>file_to_append.
r
ve
Example:
linuxtechi@localhost:~$ uptime >> sysload.txt
Truncating Files:
By eliminating the instruction before > operator, we can utilise a shorthand syntax
for truncating files. The following is the syntax:
m
# >file_name
# :>file_name
If the file does not exist, both of these command-less commands will create it and
truncate it to zero bytes if it does.
(c
linuxtechi@localhost:~$ ls -l command.txt
e
linuxtechi@localhost:~$ > command.txt
linuxtechi@localhost:~$ ls -l command.txt
in
-rw-rw-r-- 1 linuxtechilinuxtechi 0 Dec 2 12:18 command.txt
nl
Sending Output to Nowhere Fast
In some cases, you don’t only want to redirect the output of a command; you also
want to toss it away. This can be accomplished by sending the output of a command
O
to the null file “/dev/null.” As if /dev/null were a black hole star, the null file absorbs all
output delivered to it.
ty
2.4 Unix Permissions
Defining access permissions for existing files and directories, setting default
si
permissions for newly created files and directories, altering ownership of files, and
sharing files among groups are all activities that advanced Unix commands help us
with.
r
In the Unix system, data is stored in files. We can limit or allow access to this data
ve
by restricting or allowing access to the files that contain it. Ordinary, directory, and
special files are the three categories of Unix files.
To avoid any unintentional changes, we can employ file permissions. We can keep
the ability to read the files but limit the ability to write to them. In a multi-user scenario,
ni
Other users should not be able to read some of the data you save on the machine.
Other students should not read your programming assignment if you are doing one for
your class. It may be required to restrict software use to a select group of users if your
ity
organisation has purchased software with conditions attached to its use (as is often the
case nowadays).
You might also want to avoid accidently overwriting a file and erasing vital
information.
m
controlled. Each user belongs to one or more groups, and each file is assigned to one
of them. Each user has a group that is the default group for new files they generate at
any given time.
Each group has a distinct name as well as a distinct number (its group-id or GID).
(c
The command id (‘identifier’) is used to query the database, and it displays the user’s
name, as well as the names and numbers of the groups to which they belong, as well
as the user’s user-id and current group-id. If you pass id another user’s username as an
Notes
e
argument, the information for that user will be displayed instead. As an example,
id chris
in
uid=145(chris) gid=12(ugrads) groups=12(ugrads),417(proj)
shows that user chris belongs to the groups ugrads and proj, both of which have
nl
GID values of 12 and 417. Chris is currently assigned to the ugrads group. Also notice
that id has assigned a number to chris, specifically 145; each user is assigned a unique
number, known as a user-id or UID. The command newgrp (‘new group’) should be
used to update your present group-id. This will make the required adjustment when
O
followed by the name of a group of which you must be a member. It will return you
to your default group-id as defined in the password file if you give it no arguments. If
you’re user chris and want to modify your assigned group to proj so you can manage
ty
access to users in group proj when you create new files for example:
$ newgrpproj
$ id
si
uid=145(chris) gid=417(proj) groups=12(ugrads),417(proj)
●● the owner of the file (normally the user who created the file).
ni
●● a collection of users
●● different users
U
The following access privileges may be granted or refused to each of these groups
of users:
●● read
ity
●● write
●● execute.
If a file has read permission, it can be viewed at a terminal, printed (if it’s a text file),
and so on. The contents of the file can be altered (for example, by an editor), and the
m
file can be rewritten or deleted if it has write permission. That programme can be run if it
has execute permission and is a binary or shell script (but copied only if it also has read
permission). Later on, an example will be given.
)A
The following is how access control is determined. The system initially determines
whether the user is the file’s owner, and if so, the owner rights are applied. Otherwise,
it tries to determine if the user belongs to the group that owns that file, and if so, it
verifies group file permissions. If a user is neither the file owner nor a member of the
file’s group, they are classified as other users. As previously noted, the group to which
(c
a file has been assigned must be a genuine group that the system administrator has
already created. The owner of a file has the ability to change the group to which it has
been assigned.
The notion of access privileges for directories differs from that of ordinary files.
Notes
e
If a directory has write permission, files can be created or destroyed in that directory.
It is possible to inspect the files contained in that directory (using ls for example) if it
has read permission; if it has execute permission, it is feasible to cd to it. If you want
in
to allow someone else to run one of your commands from that directory but don’t
want them to know what else you have, create a directory with execute but not read
permission.
nl
Use ls to determine a file’s access privileges. Consider the user chris, who has six
files in his home directory (including two subdirectories). The following output can be
seen by entering ls -l (‘long’):
O
ty
si
In fact, ls -l will show you the majority of the information you’ll need about files in
r
your daily job. The output should be in the following format:
ve
ni
The access privileges are displayed as a 10-character string. The first character is
usually a d or a -, which indicates whether the file is a directory or a regular file. Other
values are available, which we will examine later. The access privileges for the owner,
the group, and others are described by characters 2–4, 5–7, and 8–10, respectively.
U
The read, write, and execute privileges have been granted or denied for each of these
three-character substrings.
The first character for read privilege will be r, otherwise -; the second character for
ity
write privilege will be w, otherwise -. Finally, the third will be x if execute privilege and -
otherwise.
For example, anyone can read file myfile above, but only Chris can write to it (that
is, change the contents of the file in any way). Chris is the only one who can write to the
m
file prog.c, but everyone in the group proj can read it. Chris can read and execute the
file foo, but no one else has access to it. Chris also can’t write to it — this isn’t always a
problem; denying oneself write access to a file can help you avoid mistakenly deleting it
)A
If you don’t have write permission on a directory, you won’t be able to remove or
create new files in it. However, files with write permission in that directory can have their
contents modified.
(c
The following is the rest of the information provided by ls -l. Following the number
of links to a file, the owner of the file and the group to which the file is now assigned are
printed. The file’s size in bytes is next, followed by the date and time the file’s contents
Notes
e
were last modified. The file’s name appears at the end of the line.
in
The umask command sets the default permissions for newly created files.
Syntax
nl
umaskugo
The permissions that we don’t want the user, group, or others to have for the new
O
files are denoted by the letters u, g, and o. Yes, you read it correctly: umask is used to
remove rights from the three types of system users—user (owner), group, and other—
for future files, not to issue them.
ty
To further understand this, let’s use the touch command to create an empty file
called Amit and then try to list it.
Example
r si
ve
This file has a permission of 644. When we create a file, Unix determines the
default permissions based on the value stored in a variable called umask. The
permissions to mask or hide, that is, the permissions that we want to refuse, are defined
ni
by umask, which stands for user file creation mask. Umask’s current value can be
easily found by typing umask and then pressing the Enter key.
$ umask
U
0022
ity
The first 0 denotes that the number after it is an octal number. The permissions to
be denied to the owner, group, and others are denoted by the three digits following the
first zero. This signifies that no permission is granted to the owner but write permission
m
Unix thinks that the permissions for a file should be 666 whenever it is created.
However, because our unmask value is 022, Unix subtracts it from the default
)A
systemwide permissions (666) to arrive at a result of 644. The permissions for the file
that we create are then set to this value.
This is why the file Amit’s permissions were 644 or rw-r—r— Similarly, a directory’s
system-wide default permissions are 777. This means that the permissions for a
(c
e
$ umask 342
This ensures that any new file we create from now on will have the rights 324 (666
in
- 342), and any new directory we create will have the permissions 435 (777 -- 342).
nl
chmod stands for change mode, and it’s a command for changing file and directory
access permissions. Only the owner or super user has the ability to change file access
O
permissions.
Syntax
chmod [option] mode files
ty
Option here refers to the elements listed in the table below:
r si
ve
ni
U
The term mode refers to the three types of access permissions listed in the table
below:
ity
m
Examples
(a) $chmod 751 a.txt
)A
This command grants user (i.e., owner) permission 7, 5 to group, and 1 to other.
Permission 7 indicates 4(r) + 2(w) + 1(x), which means the user has read, write, and
execute permissions for the file a.txt. Similarly, the group members have 4(r) + 1(x)
permissions for the file a.txt, which means they can read and execute it but not write to
it. The other users, on the other hand, can only run the file.
(c
Refer to Figure given below for the output of the chmod command on the file a.txt.
This command grants the user (or owner) of the file a.txt permissions 7, 4(r) + 2(w)
Notes
e
+ 1(x), i.e., read, write, and execute permissions for the file a.txt. Permissions 6, 4(r) +
2(w), or read and write permissions, are assigned to the file’s group members, while
0 or no permissions are assigned to other users. The other users are unable to view,
in
write, or execute the a.txt file.
nl
O
ty
r si
ve
ni
U
This command grants read permission to the file a.txt other members. Other
permissions that already exist are unaffected.
write and execute permissions to the other users. The permissions that are currently in
place are not changed.
Note: There should not be any space after the comma (,) or while specifying permissions
(c
This command grants u, the user or owner of the file, the permissions rwx, which
Notes
e
means read, write, and execute. It also gives the group members of the file rx, which
means read and execute permission, and the other users x, which means execute
permission. Permissions granted to users, groups, and other members in the past will
in
be deleted.
nl
(f) $ chmod u=w a.txt
This command gives the user, who is the owner of the file a.txt, write permission.
Permissions previously granted to the user will be revoked. Permissions granted to the
O
group and other users will not be affected. The output of the command can be seen in
Fig.
ty
2.5 Unix Links
si
When the source of the link (what is being linked to) is moved or withdrawn, these
links act differently. Symbolic links are not changed (they simply carry a string that
is the target’s path name); hard links, on the other hand, always refer to the source,
regardless of whether it is moved or eliminated.
r
ve
Let’s say we have the file a.txt. If we make a hard link to a file and subsequently
remove it, we can still use the hard link to access the file. However, if we make a soft
link to a file and then remove it, we won’t be able to access it through the soft link,
and the soft link will become dangling. Hard links, on the other hand, raise a location’s
reference count, whereas soft links serve as a shortcut (like in Windows)
ni
A file can have many names, and having multiple filenames is one of the reasons
for having multiple filenames. For example, if a file has numerous names and one of
them is accidently or otherwise erased or deleted, the file will still be available under
another name. Another reason to have various filenames is in circumstances when
ity
several people from different places work on the same file (which ensures consistency
in the contents) and use distinct filenames.
There are two sorts of links: hard links and soft links, often known as symbolic
connections. The link information of a file can be accessed by running the ls command
m
with the –l option to get a long list of its properties. The paragraphs that follow provide a
quick overview of these links.
)A
Hard Links
In Unix, many users can access the same file, each with their own filename. The
link value will be 2 if the file contains two names, 3 if the file has three names, and so
(c
on. Using the ln command, a new filename can be linked to an existing filename, which
can then be connected to its physical file on the disc. The two files are connected using
the ln command, as illustrated below, if trial is an existing filename and test is another
Notes
e
name for the same file.
●● Because each hard linked file has the same Inode value as the original, they
in
all correspond to the same physical file. Although hard links are unable to
cross various file systems, they are more flexible and remain linked even if the
originating or linked files are relocated throughout the file system.
nl
●● The ls -l command displays all of the links, with the number of links displayed in
the link column.
●● The contents of the links are the actual files.
O
●● Removing a link reduces the number of links, but has no effect on other links.
●● Even if the original file’s filename is changed, the hard links continue to function
effectively.
ty
●● To avoid recursive cycles, we can’t build a hard link for a directory.
●● If the original file is deleted, the link will still display the file’s content.
si
●● The size of any hard link file is the same as the original file, and if the content of
any hard link changes, the size of all hard link files changes as well.
●● Hard links have the disadvantage of not being able to be made for files on various
file systems or for particular files or directories. r
ve
$ ln trial test
A comprehensive list of both of these linked files, as well as their inode values, is
ni
provided below for both observation and verification. Physical files are linked to their
names via inodes, as previously mentioned.
U
ity
The inode numbers of both linked files are the same, as can be seen in the
preceding listing. The link values are also the same. Both filenames are directly
connected to the same physical file because their inode numbers are the same. The
link value 2 denotes that the file has two links, one to this filename (which shows at the
m
It’s worth noting that all of the connected files have the same status. It’s not as if
)A
one file has the data while the other does not. Hard links are links that have the same
inode number, allow numerous names to be used, and hence allow several users to
work on the same physical copy of a file. The benefit of hard links is that any changes
or adjustments made by one user will affect the other user as well. As a result, the
file will be consistent across all users. The names and addresses file of a large
(c
organisation, on which more than one individual will be working, is a good example of
this.
When one of the files in a collection of linked files is destroyed with the rm
Notes
e
command, the link is de-linked or cutoff; the physical file remains unchanged. Only
when the link value is 0, that is, when the last link is eliminated, is the physical file
erased. The following are two significant drawbacks of hard links:
in
●● Links between directories are not possible.
●● Files across two separate file systems cannot be connected.
nl
Symbolic Links
Symbolic links are used to overcome the limitations of hard links. Soft links,
O
also known as symbolic linkages, were first introduced in BSD Unix. These symbolic
connections are similar to Windows 95 and NT shortcuts.
Symbolic links are files that contain the original file’s pathname. The inode
numbers of these linked files will differ since symbolic links are also files. As a result,
ty
a symbolic link is a file that serves as a placeholder for the contents of another file.
Symbolic linkages are created with the ln command and the –s option. When using soft
links to access a file, the Unix system uses the pathname stored in the link file.
r si
ve
The inode numbers of files linked using symbolic links are different, the file type
of the link file is l, the size of the link file is simply 4 bytes, which is enough to carry a
pathname, and the link value of either of the files is not affected, as can be seen in the
preceding listing.
ni
The original file is obviously more significant than the link file in the case of
symbolic links. The symbolic link will refer nowhere if the original file is removed first.
U
Symbolic links make it easier for a logical directory tree to accommodate files from
multiple computers connected by a network. As a result, one of the most prevalent
applications of symbolic connections is on the internet.
ity
●● A soft link is comparable to the Windows operating system’s file shortcut capability.
Each soft linked file has its own Inode value pointing to the original file. Any
changes to the data in one file are reflected in the other, much like hard links.
Although soft links can be linked across various file systems, the soft linked file will
not work if the originating file is removed or transferred (called hanging link).
m
●● The ls -l command displays all links with the first column value l? pointing to the
original file.
)A
●● The path to the original file is contained in Soft Link, but not the contents.
●● Removing the soft link has no effect, but removing the original file causes the link
to become a “dangling” link that refers to an invalid file.
●● A directory can be reached via a soft link.
(c
●● The name of the file for which the soft link is established determines the size of the
soft link. For example, if the file’s name is file1, the soft link’s size will be 5 bytes,
which is the same as the size of the original file’s name.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 133
●● If the original file’s name is changed, all soft links to that file become dangling,
Notes
e
meaning they are no longer useful.
●● Link files from different file systems: Only symlinks/soft links can be used to link
in
files across different file systems.
2.5.3 ln Command
nl
The ln command creates links between files using the command line. It allows you
to create both hard and symbolic links to data stored on disc.
Syntax:
O
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ty
ln [OPTION]... -t DIRECTORY TARGET... (3rd form)
In Linux, the ln command is used to create hard and soft links for files. Let’s take a
look at each of the three types one by one.
si
1st Form: This form is straightforward; you must provide the following information
for the source file and the destination link name:
r
ve
ni
U
ity
m
2nd Form: Here, we must provide a link to the source file and directory; the source
)A
file will then be copied to the directory that you specify. Take a look at the sample below.
(c
3rd Form: This is where we’ll specify the directory name and a file that will be
Notes
e
linked to it. It’s mostly the same as second form.
in
nl
O
ty
r si
ve
ni
A hard link is a direct connection to data on a hard drive. This means that data
can be retrieved directly via a hard link or the original filename. The hard link and
U
the original file are both direct links to the data on disc. Multiple filenames can be
associated with the same data on disc using a hard link.
A symbolic link (also known as a soft link) connects to another connection to data
ity
on disc rather than directly to the data on disc. A symlink is the sole way to link folders
on most operating systems. Symbolic links can be used to link a folder on an external
hard drive across file systems.
m
on the hard drive as the target file. The following example uses link.txt to link to target.
txt.
ls
cat target.txt
(c
target file
ln target.txt link.txt
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 135
ls
Notes
e
target.txt link.txt
cat link.txt
in
target file
The underlying data on disc is changed when you edit link.txt. As a result, the files
nl
link.txt and target.txt are interchangeable.
O
cat target.txt
target file
link edit
ty
How to create a hard link in the current directory
Pass the path to the target file or folder to create a hard link in the current directory.
si
A hard link to the target file will be created as a result of this.
r
ve
ni
U
ity
and link name. A file is symlinked into the bin folder in the following example.
ln -s ~/code/notes/notes ~/bin/notes
ln -s /mnt/external-drive ~/mydrive
Notes
e
The symlink in the home directory will still exist if the hard drive is unmounted. This
is a broken symlink scenario.
in
cd ~/mydrive
nl
2.6 Process Management in Unix
When you run a programme on a Unix system, the system creates a unique
O
environment for it to run in. This environment provides everything required for the
system to run the programme as if there were no other programmes running.
A new process is created or started whenever you issue a command under Unix.
ty
You began a process when you used the ls command to list the contents of a directory.
In simple words, a process is an instance of a running programme.
The pid, or process ID, is a five-digit ID number used by the operating system to
si
track processes. A unique pid is assigned to each process in the system.
Because all the potential numbers have been used up, pids eventually repeat
themselves, and the next pid rolls or starts over. Because Unix employs the pid to track
r
each process, no two processes with the same pid exist in the system at any given
ve
time.
The series of instructions sent to a computer from the time a task is initiated is
referred to as a job. One or more procedures may be present in a job.
You can use the Unix command-line shell to run programmes in the background,
U
allowing you to run numerous processes at once. It can also be used to pause and
resume commands that have been paused. Simply append an ampersand to the end
of a command line to instruct the shell to run it in the background. To run sleep in the
background for example, use the command:
ity
$ sleep 60 &
[1] 16556
$
m
This command puts the computer to sleep for 60 seconds. The sleep tool just
waits for a predetermined amount of time before exiting successfully. The ampersand
displays the next command-line shell prompt, which can be used to run other
)A
The following is an example output from the BASH shell. The job number (1
in brackets) is the job number, and the process ID is 16556. The shell, not the sleep
command, displays this information.
(c
Most shells include a jobs command that can be used to display your currently
executing shell tasks:
$ jobs
Notes
e
[1]+ Running sleep 60 &
A command that is executing in the background is not waiting for input from the
in
console, but it may be outputting text to it. In the forefront, a command can have
complete interaction.
nl
Using the built-in fg command, you may bring a work from the background to the
foreground. If you have numerous tasks, you can use the fg parameter to specify the
job number, like in:
O
$ fg 1
sleep 60
When you bring a command to the foreground, the prompt in your shell does not
ty
appear until the procedure is completed, and you cannot run another command until
then.
By default, the shell allows you to suspend a currently running foreground process
si
by pressing the Ctrl + Z keystroke combination. Practicing job control with the sleep
command is a simple way to get started. After running sleep, here’s an example of
hitting Ctrl + Z:
$ sleep 60
r
ve
^Z
$ jobs
$ bg %1
U
$
ity
Job number one was terminated when the ^Z (Ctrl + Z) keystroke was pressed.
Using fg or the built-in bg command, it can be unpaused. bg was used to run it in the
background in this case.
Shell jobs can be eliminated as well (or signals can be sent to shell jobs). Most
m
shells have a kill command that uses a percent sign to refer to a task number rather
than a PID. Kill 1% would for example, send the terminate signal to job number one.
If you’re utilising shell jobs, make careful to utilise the percent sign or you’ll send the
)A
[2] 15695
$ kill -18 %2
Amity Directorate of Distance & Online Education
138 Unix Operating System and Shell Programming
e
$ kill -19 %2
$ jobs
in
[2]+ Running sleep 120 &
netbsd
nl
$
Process
O
A process is an abstract concept that can be used to explain, comprehend, and
govern how a programme in an operating system is executed. A process can be
characterised as a programme in progress in its most basic form. Because Unix is a
ty
multiuser and multitasking operating system, many programmes from different users or
the same user may be running at the same time. The CPU is shared by all of these
apps. Every application under execution is assigned specified and equal CPU time
si
slots by the kernel, which constructs or spawns processes for each of them. The kernel
assigns each of these processes a unique ID number. These one-of-a-kind numbers,
known as process identification numbers or PIDs, are used to identify individual
processes.
r
ve
A process is mathematically represented by the tuple (process id, code, data,
register values, pc value), where process id (PID) is the unique identification number
that is used to distinguish the process from other processes, code is the programme
code that is being executed, data is the data used during execution, register values are
the values in CPU registers, and PC value is the address in the programme counter
ni
from which the programme execution starts or ends. PID’s maximum value is 32767 at
the moment.
U
The kernel is loaded into memory and then executed as soon as the system is
powered on. The swapper is immediately created as a system process. This process
will have a PID of zero (zero). This process 0 starts a new one called init, which stands
for initialiser. This init is one of the first programmes to be loaded, and it begins running
ity
as soon as the bootstrapping is complete. Init has a PID of one. All following processes
on the system are setup or initialised by this init process. Init chooses between
single-user and multi-user modes for the user. Init is also in charge of creating log-in
procedures. It (process 1) is the progenitor of all other processes on the system and will
exist as long as the system is operating.
m
freshly produced process, called the child, is a process that spawns another process.
When you provide the shell a command like $cat sample.lst, it spawns a process to run
the cat command. As a result, the shell sh (ksh or bash) generates another process
(cat). The parent process in this case is the shell, and the child process is the cat. A
(c
process is said to have been born when a parent process creates or generates a child
process. A process is said to be alive as long as it is active. When a process completes
its task, it becomes dormant and is said to be dead.
Notes
e
in
nl
Figure: Parent and child processes
O
When you provide the shell a command like $cat sample.lst | grep lecturer, it
creates two processes: one for running the cat programme and another for running
the grep programme. The shell process is once again the parent process, with the cat
ty
and grep processes as its children. The PIDs of the cat and grep processes, which are
children of the same parent, will differ. All child processes will inherit nearly all of their
parent processes’ environmental parameters.
si
In general, a parent process waits for its child process to finish its execution—it
waits for its kid to die. However, a parent may pass away before their child. In these
instances, the child is referred to as an orphan. These orphan processes are usually
associated with the init process, which has the PID 1.
r
ve
It’s worth noting that none of the commands create processes. Commands like cd,
mkdir, pwd, and others for example, do not create processes.
A PID is assigned to each process in the system. Each process will be assigned
U
a unique numeric identifier known as the process identification number, or PID. Each
process additionally includes a reference to its parent’s PID, commonly known as the
parent process id (PPID).
ity
Types of Processes
Interactive processes, non-interactive processes, and daemons are the three
general categories of Unix processes.
m
the shell, act on the users’ commands, and are generally attached to the terminal.
Foreground processes are the same as background processes.
Daemons
Notes
e
Daemons are all processes that run continuously without causing any terminals
to halt and wait for specific instructions from the system or the user before acting.
in
Daemons include swapper, init, cron, bdflush, and vhandle. These daemons are
created when the system is booted and remain active until the system is shut down.
These processes cannot be stopped too soon.
nl
More about Foreground and Background Processes
When you issue a command, the shell parses it, rebuilds it, and then passes it on
O
to the kernel to execute. The shell then sits and waits for the kernel to finish the job.
Because the terminal is busy with the command being executed, the user is unable to
issue any additional commands during this shell-waiting period. Foreground processes,
as previously stated, are commands that bog down the terminal while being executed.
ty
The main disadvantage of foreground processing is that no new commands may be
issued from the terminal while the previous one is still active. When a currently running
process is large and requires a long time to process, this disadvantage becomes
substantial.
si
It is possible to run processes without having to use the terminal. Background
processes take their input from a file and process it without causing the terminal
r
to pause (non-interactively), then write their output to another file. Sorting a huge
database file or using the search command to locate a file in a large file system are
ve
examples of jobs that can be executed in the background.
By ending the command line with an ampersand (&), a command can be made
to run in the background (as a background process), as illustrated in the following
example.
U
$sort –o student.lststudent.lst&
567
$
ity
The shell prompt $ is immediately followed by the process identifier (PID) number.
The PID of the just filed background job is 567 in the example above. Because the shell
prompt ($) reappears almost instantly, one can now work at the terminal with ease.
m
Running background processes should be done with caution, as the user may
encounter issues in some circumstances. Any of the following factors could be causing
any of these issues.
)A
●● The overall efficiency of the system suffers when there are too many processes
running in the background.
●● When some processes are still operating in the background, there is a risk of the
Notes
e
user logging out.
in
A running programme is referred to as a process or “job” in the shell. A job is
typically launched from the command line and continues to run until the application
nl
completes its task. While a programme is executing, the shell at the current terminal will
not accept any additional commands until the work is finished. A programme that runs in
this manner is referred to as “foreground.”
O
By pressing the control key and the “c” key at the same time, you can forcefully
terminate a foreground job. “ctrl>-c” or “C-c” can be used to show this. This method
of terminating a process can be useful if it is taking an excessive amount of time to
complete, if the software crashes or hangs for whatever reason, or if the user does not
ty
want the process to finish.
si
shell prints “Suspended” and reclaims control of the terminal, allowing the user to enter
more commands. The paused work continues to “run” on the system, but it is effectively
frozen until it is resumed.
r
ve
The Jobs Command
The jobs command can be used to display a list of all jobs currently executing from
the current shell. The first column of the report shows the job ID number. The second
column displays the job’s current status (Suspended or Running). The third command
ni
>
U
>xclock
Suspended
>
ity
Suspended
>
m
> jobs
>
>
(c
Restarting a Job
If a job has been suspended, it can be restarted with the command fg. The fg
command will bring the suspended work to the front of the queue, allowing it to continue
Notes
e
processing. If more than one job is put on hold, the most recent programme will be
prioritised.
in
>
nl
Suspended
> jobs
O
>xclock
Suspended
ty
>fg
xclock
si
Interrupt Process
The usage of terminals, or keyboards, is one of the most common ways to connect
r
with a system. On these terminals, a certain sequence of keys controls the behaviour of
any software now running. We’ve been using it as an example.
ve
●● <Ctrl–m>(^m), that is the <RETURN> key to end a command line and execute the
command.
●● <Ctrl–c>(^c) to interrupt a current process and to come back to the shell.
ni
The stty command displays or verifies the settings of various keyboard keys. By
executing this command without any parameters, the user can get a quick list of the
settings. It must be used with the –a (all) option to see all the settings, as illustrated in
the following example.
ity
$stty –a
echo echoe – – – – – – – – – – – – – – – – – – – –
$
)A
The result provided above is only for demonstration purposes. The terminal speed
is 9600 bauds, ^U is used to kill a line, ^D is used to mark the end of a file, everything
input at the keyboard is echoed on the display terminal due to echo, backspacing over a
character keeps its display, and so on.
(c
As seen in the samples below, this command can also be used to change the key
settings.
$stty –echo
Notes
e
$sttyeof \^a
The former command causes the display of characters typed at the keyboard to
in
be disabled. It should be remembered that this is the password handling setting. The
usage of <Ctrl–a> ends all standard input after the execution of the latter command.
nl
It is not advisable to tinker with the terminal settings. This could cause the terminal
to malfunction. If the user discovers that the terminal is malfunctioning, he or she can
restore sanity to the terminal settings by passing the word sane to stty as a single
argument, as illustrated below.
O
$stty sane
The above command modifies the terminal settings to make them more realistic.
ty
2.6.4 Killing Jobs
There are times when it is desirable to terminate a process prematurely. The
si
following are some of these scenarios:
foreground process. The DEL or BREAK keys are used to accomplish this. The kill
command, on the other hand, is used to terminate a background process. The PID of
the process to be killed is supplied as an input to this command. The ps command is
U
The kill command for example, can be used to terminate a process with the ID 555,
as illustrated in the following example.
ity
$kill 555
More than one process can be terminated using a single kill command as shown in
the following example.
$kill 330 333 375 # here 330, 333, 375 are process id’s.
m
When the kill command is used, it sends a termination signal to the process that
is being terminated. It broadcasts 15 as its default signal number when used without
)A
any options. Many processes ignore signal number 15, which is known as the software
termination signal. The shell process sh for example, ignores signal 15. In other words,
signal 15 does not guarantee that all processes will be killed. In such cases, signal
number 9, the sure kill signal, can be used to forcefully end a process, as seen in the
following example.
(c
As seen in the following example, a 0 (zero) as the argument of the kill command
Notes
e
can be used to terminate all of a user’s processes (excluding his login shell).
in
All processes, including the shell, can be killed with the 9 option and 0 (zero) as the
argument, as demonstrated in the following example.
nl
$kill –9 0 # kills all processes including the login shell
where pid is the process’ unique identifier and signal (optional) is the signal
to deliver to the process. The TERM signal, which informs the process to end, is the
O
default signal. Some systems require you to specify the signal numerically, while
others enable you to use the signal number or its symbolic designation. [For a list of
signal names, use kill -l; regrettably, the listing does not display the name-to-number
correlation.] They are, however, in order, so if you can count, you should be able to
ty
figure it out. — TOR]
After a kill command, a process could still persist. If this happens, use the -KILL
or -9 option with the kill command. Almost always, the process will be destroyed as a
si
result of this. However, it prevents the dying process from cleaning up, which may result
in inconsistencies in the process’ data.
r
Even after receiving the KILL signal, processes will occasionally refuse to die. The
great majority of such procedures can be classified into one of three groups:
ve
●● Zombies. In BSD ps displays, a zombie process is indicated as Z status, and
in System V as defunct>. When a process exits, it notifies its parent that it
is about to die; when the parent acknowledges, the PID is deleted from the
ni
process table. A zombie process is one that has had its whole resources freed
but has yet to be acknowledged by its parent process. When the parent is
absent, init usually takes over, but this does not always happen. Zombies
are always deleted the next time the system boots up and have no negative
U
will not die. To terminate such processes, use the QUIT signal (3) or the INT
(interrupt) signal (2).
●● Before departing, processes wait for a device to finish an operation. This
usually entails waiting for a tape to rewind.
m
When a process is killed, all of its progeny are likewise killed. If child processes are
blocking or “catching” the signal you’re using, they may not die — albeit, as previously
stated, the KILL signal will normally kill those processes. As a result, killing a shell kills
)A
all foreground and background processes started by that shell (including other shells).
Logging a user out is the same as killing their login shell. This is an effective (though
painful) method of recovering from certain types of issues. For example, if a user
confuses his editor by mistyping control keys and escape sequences, or if he becomes
stuck in an infinite loop that he can’t get out of, destroying his shell will allow him to
(c
retake control of the situation, albeit at the expense of some labour. Determine which
process is the offending user’s shell using the ps command. Remember that in order to
kill another user’s process, you must be a superuser.
e
Changing Password
in
It is recommended that login passwords be changed at regular intervals to avoid
the potential of unwanted access to files. The passwd command is used to change the
password.
nl
Syntax
$passwd
O
We will be requested to provide the old password before supplying the new
password when we run the passwd command (to confirm that only authorised people
are changing the password). Furthermore, the new password should be very distinct
from the old one. It should have at least six characters, two alphabets, one numeric
ty
character, and one special character. We might get the output given in the following
example after running the command.
si
Example:
$passwd
We may obtain the following error if the new password and the old password are
not substantially distinct from each other:
If the two passwords supplied in New password and Re-enter new password are
not the same, the following error will occur:
ity
Try again
If the two passwords entered in New password and Re-enter new password
are identical, the user’s password will be changed, and we will receive the following
m
confirmation message:
exit
To log out of a Unix system, exit from a shell, or quit from a shell script, use the exit
command.
Syntax exit
(c
Example exit
Ctrl-d is a shortcut that is used to log out of the Unix shell. Before closing the
Notes
e
Unix system, be sure that all of the open files are saved and closed; otherwise, they
may become corrupted. When we exit the shell, the presently executing process or
command is usually stopped automatically.
in
Summary
●● A system administrator is someone who is in charge of configuring and
nl
maintaining the Unix operating system. He or she is in charge of ensuring that the
Unix system runs smoothly and that the system’s resources are used to their full
potential.
O
●● The Linux home directory is a collection of files for a certain user. It’s also called
the login directory. The first location you’ll see after signing into a Linux system. “/
home” is generated for each user in the directory. It’s a regular root subfolder. The
ty
root directory contains all other directories, subdirectories, and files. It’s indicated
by a slash (/).
●● Multiple filenames are one of the reasons for having multiple filenames. For
si
example, if a file has multiple names and one is accidentally removed, the file will
continue exist under another name. Another reason to use alternative filenames
is when multiple persons from different locations work on the same file (ensuring
r
consistency in the contents).
ve
●● Absolute and relative pathways exist. Whereas an absolute path refers to the
root directory and links to a specific file or directory, a relative path refers to a
file or directory in relation to the current working directory. The root directory of
an absolute path is denoted by the forward slash. Another difference is that the
ni
absolute path of a file or directory does not change, whereas the relative path
does.
●● The cat command is an abbreviated form of the word concatenate, which meaning
U
‘combine.’ We may use the cat command to not only create files and view their
contents, but also to combine them. When using the cat command, the operator >
can be used to join numerous files into a single one. To append to an existing file,
use the operator >>.
ity
●● The ex modecommands :n and :p are used to switch to the next and previous
files, respectively. The user has the option of searching for a pattern in either the
forward or backward direction. The command /pattern is used to search forward,
and the command pattern is used to search backward. These procedures are
m
comparable to those performed with the vi editor while searching for patterns.
●● There are two sorts of links: hard links and soft links, often known as symbolic
connections.
)A
●● It’s feasible to change both the source and the destination of a program’s input
and output. Redirection changes the input source or output destination.
●● If you are new to the LINUX operating system and are having problems using the
command-line utilities supplied by LINUX, you should first learn about the help
(c
command, which, as its name implies, assists you in learning about any built-in
command.
●● Meta characters are unique characters that represent something other than
Notes
e
themselves. Meta characters are characters that are neither letters nor numerals,
as a rule. The shell, like grep, sed, and awk, has its own collection of meta
characters known as shell wildcards. Shell meta characters can be used to group
in
commands, abbreviate filenames and pathnames, redirect and pipe input/output,
and run commands in the background, among other things.
●● When an rm command is used to remove a file from a collection of linked files, the
nl
link is removed but the physical file remains. In other words, the actual file is only
deleted when the last link has a value of 0.
●● Links contain the original file’s pathname. Because symbolic links are files, their
O
inode numbers will vary. So, a symbolic link is a file that holds the contents of
another file. The ln command with the –s option creates symbolic links. To access
a file using soft links, the Unix system uses the link file’s pathname.
ty
●● Hard links have two major drawbacks:
o Files from two different file systems cannot be linked. Links between
directories are not possible.
si
o Symbolic links are used to overcome hard link constraints. They were
introduced in BSD Unix as symbolic linkages. These symbolic links are like
Windows 95/NT shortcuts.
●● r
This is a fascinating Unix feature. Streams are just a bunch of bytes. As you may
ve
recall, a file is made out of bytes. As a result, the line between stream and file
becomes hazy. In Unix, there are three sorts of streams: input, command, and
destination.
●● A programme receives standard input from any source (the keyboard is the default
ni
source) and sends standard output to any destination (the display screen is the
default destination).
●● It’s feasible to change both the source and the destination of a program’s input
U
operator to redirect output. The output redirection operator (‘>’) can be used to
redirect any command’s output to a file rather than the terminal screen.
●● In a single command line, you can use both the <and > redirection operators.
Despite the fact that such combinations are possible, the sequence in which the
input and output files are provided is not limited
m
●● When you run a programme on a Unix system, the system creates a unique
environment for it to run in. This environment provides everything required for the
system to run the programme as if there were no other programmes running. A
)A
new process is created or started whenever you issue a command under Unix.
You began a process when you used the ls command to list the contents of a
directory. In simple words, a process is an instance of a running programme.
Glossary
(c
●● exit: Exiting: To log out of a Unix system, exit from a shell, or quit from a shell
script, use the exit command.
●● Internal commands: Internal commands are a set of built-in commands that come
Notes
e
with the shell. Some of them, like cd and echo, don’t create a process and are
instead run directly by the shell. These commands are included in the shell and do
not need to be downloaded separately.
in
●● External commands: Unix utilities and programmes like cat and ls are examples
of external commands. These commands are separated into independent files
nl
and stored in various locations. The /bin directory contains regularly used user
commands, while the /etc directory contains commands that are only used by
system administrators.
O
●● $ mkdir courses: This command creates a directory by the name courses under
the current directory
●● $ mkdir courses faculty placement: This command will create three directories by
the names courses, faculty, and placement.
ty
●● Line-continuation characters: We occasionally run across commands that are too
long to fit on a single line. The line-continuation character is used to continue such
directives in the following consecutive line. The character for line continuation is \
si
(backslash).
●● touch: Use the touch command to create files and adjust time stamps. The term
r
“time stamps” refers to the last time a file was accessed as well as the last time it
was updated.
ve
●● cp: Copying files: The cp command is used to make a copy of a regular file(s) with
a different name.
●● mv: Renaming files: The mv (move) command is used to modify a file’s name.
ni
Essentially, the mv command copies the file from its current location to another.
The link to the previous filename is removed from the directory entry and replaced
with a link to the new filename.
U
●● cat: Displaying the contents of a file: This command is frequently used to show the
contents of a file. The contents of a file can be displayed on the screen with this
command.
ity
●● head: Displaying top contents of files: The head command is used to choose a
given number of lines from the file’s beginning and display them on the screen.
●● tail: Displaying bottom contents of the file: The tail command selects a number of
lines from the file’s end and displays them on the screen.
m
●● cal: Displaying calendar: The cal command displays the calender for the month
and year supplied.
●● file: Determining File Type: This command detects the file type of the provided
)A
name, that is, whether it is a file or a directory. To determine the type of a file, the
command performs a series of checks or tests on it.
●● wc: Counting Lines, Words, and Characters: The wc command is used to
determine the number of source code lines in any Unix-based application
(c
software. By default, it displays all three counts of a file: characters, words, and
lines.
●● Grep: The grep programme looks for lines with the pattern n in a file or files.
Notes
e
●● Cmp: The cmp command compares two files and displays the line number where
the first difference appears. If the files being compared are identical, the cmp
in
command produces no output.
●● ls: The ls command displays the contents of the disk’s files and folders. The files
and directories are presented in alphabetical order by default. If no directory name
nl
is supplied, the list of files and directories in the current working directory will be
displayed.
●● ln Command: Thiscommand creates links between files using the command line
O
●● Soft links: Soft links are also known as Symbolic Links
●● Symlink: A symlink is the sole way to link folders on most operating systems
ty
●● Create a hard link: Pass the entire path of the target file and the link name to the ln
command to create a hard link.
●● Create a symbolic link: To make a symbolic link, use the -s option with the ln
si
command, then the target file and link name.
●● < input redirection: Using the (less than) operator, the input source is redirected (to
something other than the default, which is the keyboard).
●● r
output redirection: The > (greater than) or >> (twice greater than) operators are
ve
used to redirect the output destination.
●● >> output redirection with appending: A program’s output can be redirected using
the > or >> operators. The usage of > causes destination files to be overwritten.
The current output will be appended to an existing file when >> is used.
ni
●● umask: This command sets the default permissions for newly created files
●● chmod: change mode
U
a. UNIX
b. Macaintosh
)A
c. Windows
d. All of the above
2. One-line statements that operate on the specified operands to accomplish some
action are referred to as _ _ _ _ _ _ _ _ _ _.
(c
a. Arguments
b. Commands
c. Operators
Notes
e
d. Links
3. Mkdir command used for:
in
a. Deleting directories
b. Copying directories
nl
c. Moving directories
d. Making directories
O
4. _ _ _ _ _ _ _ _ _is a mechanism for changing the input source and/or output
destination.
a. Command
ty
b. Programme
c. Redirection
d. none of the above
si
5. Using the _ _ _ _ __ operator, the input source is redirected (to something other than
the default, which is the keyboard)
a. (less than) r
ve
b. greater than
c. equal to
d. twice greater than
ni
c. greater than
d. None of the above
ity
c. Standard input
d. output redirection
)A
8. The _ _ _ _ _command creates links between files using the command line. It allows
you to create both hard and symbolic links to data stored on disc.
a. chmod
b. ln
(c
c. umask
d. none of the above
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 151
e
a. Link
b. Symlink
in
c. Hard
d. all of the above
nl
10. _ _ _ __ _ _ _links can be used to link a folder on an external hard drive across file
systems.
a. Symbolic
O
b. hard
c. both a and b
ty
d. None of the above
11. _ _ _ _ _ _ _ _ _includes a versatile technique of safeguarding files.
a. linux
si
b. windows
c. machintos
d. UNIX r
ve
12. The_ _ _ _ __ _ command sets the default permissions for newly created files.
a. chmod
b. umask
ni
c. Redirection
d. all of the above
U
c. system user
d. none of the above
14. _ _ _ _ _ _ _ _ _is a command for changing file and directory access permissions.
m
a. change mode
b. umask
)A
c. permissions
d. None of the above
15. _ _ _ _ _ _ _ _ _includes a versatile technique of safeguarding files.
a. linux
(c
b. windows
c. machintos
d. UNIX
Notes
e
16. _ _ _ _ _ _ _ _can be used to suspend a process.
a. Signal 18
in
b. Signal 19
c. Signal 20
nl
d. Signal 17
Exercise
O
1. How Telnet Connect through Login Account, Password, Logout?
2. Define Internal and External Commands.
3. Explain difference between Internal and External Commands.
ty
4. Explain current Working Directory.
5. Define home Directories.
si
6. How to create new directories?
7. How to create Creating Files – touch, cat?
8.
r
How to move, deletes and copy files?
ve
9. How to view file - cat, more, pg, less?
10. What do you mean by getting online help; manual pages?
11. Define meta characters, wildcards.
ni
15. Define the concept of user and group permissions and its dependencies.
16. What do you mean by changing permissions?
ity
Learning Activities
1. Explain various internal and external commands.
(c
e
Check Your Understanding - Answers
in
1. a
2. b
3. d
nl
4. c
5. a
O
6. c
7. d
8. b
ty
9. c
10. a
si
11. b
12. b
13. c
r
ve
14. a
15. d
16. a
ni
G. Sobell
6. Learning the bash Shell, Book by Cameron Newham
7. Linux Command Line and Shell Scripting Bible, Christine Bresnahan and
)A
Richard BLUM
8. How Linux Works: What Every Superuser Should Know, by Brian Ward
9. UNIX: The Complete Reference, Second Edition, Rachel Klee, Douglas A.
Host, Richard R. Rosinski, Kenneth H. Rosen
(c
e
Learning Objectives:
in
At the end of this module, you will be able to understand:
nl
●● Commands to Change Word, Change Line
●● Command to Delete Character, Insert Line, Delete Text
O
●● Commands to Delete Current Line, Delete n Lines, Delete Remainder of Lines
●● Command for Copying and Moving; Saving and Exiting
●● Command for Moving the Cursor; including Other Files
ty
●● Introduction to Shell Scripting, Concept of Vi Help
●● Running Shell Commands; Search and Replace Commands; Commands for
Changing and Deleting Text
si
●● A Sample Code Snippet of a Shell Script
Introduction
r
ve
Anyone who uses a computer can write programmes in any of the available
programming languages. The user would at the very least prepare a text file in the form
of a letter, either to send by mail or via e-mail, using the word processing tool. In all of
these situations, the users type in the programme or letter that they have prepared.
Most of the time, it is not practical to prepare correct programmes or letters and then
ni
Inserting text, erasing characters or lines, shifting paragraphs, and so on are all
examples of corrections. Editing is the term used to describe the process of carrying out
such tasks. A special software package called the editor is used to enter programmes
or texts into computers and edit them.
ity
There are numerous sorts of editors available nowadays. The popular editors
vi, vim, and emacs are being used with various types of Unix systems. All of these
editors are full-screen. The earliest Unix editor was called ed, and it was a line editor.
Ken Thompson was the one who came up with the idea. Every line in the file will
m
be numbered in line editors, and lines for editing will be picked based on these line
numbers. The user can only work with one line at a time.
Later, at the University of California, Berkeley, Bill Joy developed ex, a more
)A
sophisticated line editor (the extended editor). Compared to ed, the ex editor is
significantly more complete and powerful. Joy went on to create a screen-oriented
interface for ex. The vi editor was named after the ex editor’s screen-oriented interface,
where vi stood for visual. Because vi supports all of the ex commands, a huge number
of ed commands are still available today. Aside from the ex commands, Vi has its own
(c
set of special commands and rules for using the entire screen.
Users can see at one screen full of information at a time and edit on the entire
Notes
e
screen full of information using the vi editor. This feature gives the user a more accurate
view and feel of the file.
in
As a result, the vi editor is called a visual editor. Otherwise, unlike modern word-
processing systems, it does not have a lot of graphic techniques built in. The vim editor
is an upgraded version of the vi editor that is included with LINUX. The sections that
nl
follow provide a thorough examination of the vi editor’s most important features.
We can write these commands in a file and execute them in shell to prevent this
repetitive labour because shell can take commands from files as input. Shell Scripts
O
or Shell Programs are the names given to these files. The batch file in MS-DOS is
analogous to shell scripts. Each shell script has a.sh file extension, such as myscript.sh.
Shell scripts have the same syntax as other programming languages. It will be
ty
quite simple to get started if you have prior knowledge with any programming language
such as Python, C/C++, or others.
si
●● Shell Keywords – if, else, break etc.
●● Shell commands – cd, ls, echo, pwd, touch etc.
●● Functions
●●
r
Control flow – if..then..else, case and shell loops etc.
ve
Why do we need shell scripts?
●● The command and syntax are identical to those entered directly on the
command line, thus the programmer does not need to switch to a new syntax.
●● Shell scripts are substantially faster to write.
●● Quick start
m
●● Easily prone to costly errors, a single blunder can cause the command to
change, potentially causing harm.
●● Execution time is slow.
●● Design errors in the syntax or implementation of the language.
(c
●● Large and intricate tasks are not well suited to this tool.
●● Unlike other programming languages, it provides a minimum data structure.
3.1 Vi Editor
Notes
e
The vi editor is the default editor that comes with the UNIX operating system (visual
editor). We can edit an existing file or create a new one using the vi editor. This editor
in
can also be used to read a text file.
nl
variations. Because of its fullscreen editing features and a variety of other aesthetic
options, vi editor became well-known. The vi editor still supports many of the ex
(and ed) commands. Under Linux, vi editors are also available in the form of vim (vi
O
enhanced) editors. The vi command is entered at the shell prompt to launch this editor.
The vi editor has three modes of operation, which are detailed in the following sections.
ty
The vi text editor has three general editing operation modes: Command Mode,
Insert Mode, and Last Line Mode.
si
Command Mode
This is the mode that the user enters as soon as the vi command is used to enter
r
the vi environment from the shell. All keys pushed by the user are interpreted as editor
commands when the user is in command mode. In this mode, the keys pressed are not
ve
visible on the screen. As can be observed in the figurebelow, the user can enter either
the input mode or the ex mode from the command mode of the vi-editor. Any of the
commands listed below can be used to enter the input mode.
ni
U
ity
m
o and O stand for opening a line, r and R for replacement, I and I for insertion, s
and S for substitution, and a and A for appending in the above instructions list. Using
the key, the control can be returned to command mode. Commands input in command
(c
The ex prompt, which is the colon character, can be used to enter the ex mode
Notes
e
from within the command mode (:). When the editor is in ex mode, pressing the key
returns it to command mode.
in
Insert Mode
Input text mode or insert mode are other names for this mode. The user can enter
this mode by using any of the following commands, as indicated in the previous sub-
nl
section:
i, I, a, A, o, O, r, R, s, S (orisa)
O
This mode allows for new text to be inserted, old text to be appended to, and text
to be replaced. By pressing the Esc key, the user can return to command mode at any
moment (liberal use of the Esc key is recommended). When you press the Esc key
while in command mode, nothing happens; it just beeps to let you know that you’re
ty
already in command mode.
si
This mode enables the user to use the commands in the vi screen’s bottom line.
In fact, the command line is the bottom line (the last line) on the vi screen. The user
can enter ex mode from command mode by inputting the colon character (:). The most
r
significant benefit of this mode is that the commands submitted in it are printed in the
ve
command line, which is the last line. The editor is now in ex mode, as indicated by the
colon (:) prompt in the last line. Anything typed before the colon (:) prompt is considered
an ex command. Using the Enter key, the user can return to command mode.
ni
paragraph). You must also alter the letters and words (to correct misspellings or to
reflect a change of mind about a term). You might want to relocate text from one part
of your document to another. You may also need to copy text to reproduce it in another
portion of your document.
ity
In contrast to many word processors, vi’s command mode is the default. With just a
few keystrokes, you can execute complex, interactive modifications. You can use any of
the “insert” instructions to insert raw text, and then type away.
m
Basic commands are limited to one or two characters. Consider the following
scenario:
i
)A
Insert
cw
Change word
(c
You can quickly modify a file by using letters as commands. You won’t have to
memorise a bank of function keys or stretch your fingers to reach inconvenient key
combinations. You’ll never have to take your hands off the keyboard or fiddle with
Notes
e
several menu levels again! The letters that conduct the commands may be recalled for
the most part. Almost all commands have similar patterns and are interconnected.
in
The c command modifies the text. It works in the same way as the d command,
only it puts you in insert mode. If you enter visual mode (v), highlight a word (w), and
then press c, the word will vanish and you will be in insert mode.
nl
In the scenario below, we’ve changed the boss to an idiot
O
The boss said ...
ty
The c command in normal mode works similarly to the d command, only it puts you
in insert mode. cw for example, modifies a word. Or, to put it another way, it deletes a
word before putting you in insert mode. This command is demonstrated in the diagram
si
below.
r
every problem. That is the situation with the cw command example presented above.
Except for the cw and dw commands, the cmotion command is identical to the dmotion
ve
command. Unlike the cw command, which deletes the text up to the space after the
word (before entering insert mode), the dw command deletes the word and the space
after it.
ni
U
ity
m
)A
The cc command affects the whole line. In other words, it deletes the line before
switching to insert mode. In other words, cc, like dd, works on the current line. Similarly,
the commands c$ and C move the cursor to the end of the line.
Change Line
(c
To repeat the effect, either the full sequence or the text object might be preceded
by a number. You may for example, set the cursor on line 96 and enter either to alter
lines 96 through 99 as seen below.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 159
:96,99!sort
Notes
e
Place the cursor anywhere on the line and type cc to replace it. The line vanishes,
leaving you with a blank line to write your new text on (which can be of any length). To
in
exit, press Esc.
Highlighting numerous lines of text and pressing the Tab key in most GUI editors,
such as gedit, will indent the lines by one tab character. To get the same effect in VIM,
nl
first decide how many tab characters you want to add or remove. Shiftwidth must be
assigned this number. Next, determine the size of the depression (how many characters
of space a tab character should represent). Set tabstop is a command that can be used
O
to accomplish this.
The shiftwidth value should be a multiple of the tabstop size if you want an
indentation with only tab characters and no space characters.
ty
For example, I like my tabs to represent four spaces. So I set the tabstop size to be
four: :settabstop=4
I also want the lines of text to be indented with 4 tabs, so I set the shiftwidth size to
si
be 16 (16/4 = 4):
setshiftwidth=16
r
To pick lines of text in VIM, go to the visual mode by pressing the upper-case V
ve
character in command mode. The letter V in upper case allows you to pick multiple lines
of text. To indent the lines, press the right arrow key > after selecting the lines of text. 4
tab characters will be used to indent these lines.
When the cursor is on a line, use the right arrow key twice (») to indent only that
ni
line.
Characters
ity
You frequently want to erase a single character or two while editing a file. X deletes
a single character in the same way as r alters a single character. The x command
deletes any character on which the cursor is currently located. You can erase the letter
1 in the next line by pressing x.
m
)A
Insert Line
Notes
e
There are two modes in the vi editor: command mode and insert mode. In contrast
to many word processors, vi’s command mode is the default. To insert lines of text,
in
enter insert mode with a command and then type away. The majority of commands are
only one or two characters long.
nl
For example:
i insert
[address] i[!]
O
text
Insert text on the line before the supplied address, or on the current line if no
address is specified. During text input, add a! to toggle the autoindent setting. Enter a
ty
line consisting solely of a period to end new text input.
Delete Text
si
It’s unusual that the text you enter in your file is perfect. You’re looking for mistakes
or wish to improve a phrase. You must be able to alter the text once it has been
entered.
r
What exactly are the elements of editing? You’d want to add some text (a forgotten
ve
word or a missing sentence). And you wish to get rid of certain text? (a stray character
or an entire paragraph). You must also alter the letters and words (correct misspellings
or reflect a change of mind). You wish to relocate text from one part of your document
to another. You may also need to copy text to reproduce it in another portion of your
ni
document.
There are four basic edit commands: I for insert, c for change, d for delete, d then
p for move (delete and put), and y for yank (copy). This section goes over each sort of
U
edit.
We can also use the delete command, d, to remove any text from your file. The
delete command, like the modify command, requires an argument (the amount of text to
ity
be operated on). You can remove using the dw command, the dd and D commands, or
other movement instructions.
With all deletions, go to where you want the modification to happen and type delete
(d) followed by the amount of text you want to get rid of (such as a text object, w for
m
word).
Words
)A
Assume you wish to remove one use of the word srurt from the first line of the
following text.
(c
The dw command deletes a word from the cursor’s current location to the end. As a
Notes
e
result, dw can be used to erase a word’s component.
in
nl
As you can see, dw not only erased the rest of the word, but also the space before
any consecutive words on the same line. De, which deletes only to the end of the word,
O
might be used to keep the space between words.
ty
si
You can also remove a line backwards (db) or at the end or beginning (dS or do).
Line deletions seem differently if you’re using a dumb terminal or one that runs at
less than 1200 baud. Until you scroll past the bottom of the screen, the stupid or slow
ity
terminal will not redraw the screen. Instead, the deletion is written as:
@
m
a written plan
Until the terminal redraws the full screen, a @ sign “holds the place” of the deleted
line. (Depending on the terminal you’re using, you can force vi to redraw the screen
instantly by hitting CTRL-L or CTRL-R.)
From the cursor location to the end of the line, the D command deletes:
(c
Notes
e
in
Delete n Lines
nl
If tapping keys on the keyboard is troublesome for you, you can erase numerous
lines at once by adjusting the aforementioned command. You can specify the number of
lines you want to remove instead of simply pressing dd.
O
For example, typing 3dd will delete the next three lines from the file.
You must first switch to command mode in order to delete lines inside a defined
range (press Escape while in normal mode). Then, to make your own deletion
ty
command, use the following syntax:
:[start],[end]d
si
To eliminate the lines between ranges one and five for example:
:1,5d
r
In the aforementioned command, you can also utilise wildcard characters.
ve
. (Dot): Refers to the current line
:.,5d #deletes lines between the current line and the fifth line.
:.,$d #removes all lines starting from the current line till the end.
U
D: -Start from the current cursor point and delete the rest of the line.
ity
The vi delete to pattern command removes everything up to the word while from
the cursor, but leaves the rest of both lines intact.
A letter, a word, a line, a group of lines, or an entire file is considered text for the
sake of explaining and understanding the copying and transferring processes. Copying
)A
entails extracting the relevant text and then inserting it into the file at any desired
location. Moving entails erasing the required text and then inserting it into the file at any
desired location.
Using vi, the removing and placing activities are referred to as yanking and putting,
respectively. The text that is pulled or removed is saved in a buffer for general use.
(c
When the put command is used, the contents of this buffer are automatically inserted.
e
The editor buffer is a duplicate of the file that vi works on. This buffer is the only
one that is affected by any editing activity. The user must write or save the modifications
in
made to the original file for the change to take effect. Any of the save commands can
be used to save the contents of the editor buffer into the actual file. There are up to 36
buffers available in vi. They really are.
nl
●● Unnamed buffer
●● Named buffers: “a, “b, “c, …, “z
●● Numbered buffers: “1, “2, “3, …, “9
O
The general-purpose buffer is another name for the nameless buffer. When a text
is modified or erased, the old text is not immediately discarded. Instead, vi copies the
previous material to an unnamed buffer and holds it there until the current vi session is
ty
finished or some other text manipulation is done. This has the advantage of assisting
with text recovery (by, using the undo command u).
The named and numbered buffers can be used to move text blocks around a file
si
or between files. It is required to use double quotations (“) at the start of the named and
numbered buffer names.
three characters to the right of the current cursor position are erased, and when the
3X command is supplied, three characters to the left of the current cursor location are
deleted.
U
The dd command can be used to delete a line under the current cursor location.
This command can also be used with repeat factors. The 4dd command for example,
eliminates four lines starting from the current line.
ity
The d command in ex mode can also be used to delete one or more lines. For
instance,:d deletes the current line,:$d deletes the previous line,:1,4d deletes lines 1
through 4, inclusive, and so on. The editor remains in command mode after all delete
actions have been completed.
m
Notes
e
in
nl
O
ty
Placing the Text
The operation of putting (also known as pasting in word processing) is called
si
putting in vi. The p (lowercase p) or P (uppercase P) commands are used to put
something together. The p (lowercase) command moves the cursor just after or below
the current contents of the general-purpose buffer. The P (uppercase) command
r
displays the contents of the generalpurpose buffer immediately before or above the
ve
cursor.
The size of the text being handled determines the placement position, such as to
the left or right of the current cursor position, or above or below the current line. If the
text being handled is a single character or a single word, it will be placed before or
ni
after the cursor location. If the text to be handled is a single line or a series of lines, the
cursor will be placed above or below the current cursor location. The yanked or erased
contents of the general-purpose buffer could be the current contents.
U
text should be moved, written, copied, or erased. In a file, more than one marker can
be used. Lowercase is used for marking. The user cannot see this letter or the marking.
The vi, on the other hand, can see it. These identifiers are only valid for the current
session. To put it another way, once a file is closed, any markings on it will be erased.
m
Any place in a file can be marked by moving the cursor there and then issuing the
m command, where a act is the marker.
ma
)A
The line on which the cursor is placed will be marked with the label a when the
aforementioned command is issued. The command ‘a’ can be used to return to this
highlighted line (i.e., a single quote mark followed by the marker label).
(c
Following the aforementioned instructions, a few lines of text from the marked line
to the line on which the cursor is presently situated can be erased using the delete
command d, as well as the mark label with the command d’a. Similarly, the commands
e
Two markers are used to delete, move, or copy larger blocks of text. One of
these markers is placed at the beginning of the block, while the other is placed at the
in
end. If an is the block begin marker and b is the block end marker, then using the ex
modecommand:’a,’b d, the block between the markers a and b is erased.
All lines between the markers a and b, as well as the lines on which these markers
nl
are present, will be erased when this command is run. It’s worth remembering that line
numbers can also be used to delete or yank larger pieces of text. Other related tasks,
such as yanking, moving, and copying, can be done in the same way.
O
Saving and Exiting
The vi editor allows us to exit it with or without saving the material. It also allows
us to continue entering or altering content while writing to the file. To save the file, save
ty
and leave the vi editor; to exit without saving or to do equivalent activities, use the keys
listed in the table below:
si
Table: Commands to save the file and quit the vi editor
r
ve
ni
Note: When we type the colon commonds, the colon and the command appear in the
lower left corner of our screen. On pressing the Enter key, the command is executed.
U
Example
Take for example, the file a.txt in the vi editor, which contains the following
information.
ity
~
)A
~
(c
If we press Esc followed by:wq while in insert mode, the contents of the file will be
Notes
e
saved, and we can continue with our editing activity. If the file is the same as the last
saved version and has not been edited, pressing:q will exit the vi editor. The contents of
the file will be saved and we can exit the vi editor by pressing:x,:wq, or ZZ. We use the
in
command:q! to exit the vi editor without storing the content.
nl
To edit, the cursor must first be moved to the correct position, and then the editing
must be completed. There are numerous commands that may be used to move the
O
cursor. Moving by one position in all directions, moving by one word in either of the
horizontal directions, moving to either ends of a line, moving to either ends of the
screen or the file, and so on are all examples of different forms of cursor motions. All of
these movement commands are given in the editor’s command mode.
ty
Moving by One Position
Changing the cursor one position indicates moving one character position to the
si
left or right, or one line above or below the current line.
Command r Purpose
ve
H or Backspace Moves the cursor by one character persition to the left.
I or Spacebar Moves the cursor by one character position to the right.
J (lowercase) moves the cursor down one line forom its present position in
the same column.
ni
k (lowercase) Moves the cursor up one line from its present position in the
same column.
+ or Enter Moves the cursor down one line to its beginning.
U
The four arrow keys on the right side of current keyboards can also be used to
ity
do the above-mentioned cursor movements. However, using the arrow keys for these
movements is not suggested.
Moving by Words
m
The web instructions listed below can be used to move the pointer across words.
The behaviour of these instructions is determined by the cursor’s current position.
Command Purpose
)A
w (word) Moves the cursor forward to the first character of the next word
or the next punctuation mark.
e (end) Moves the cursor forward to the last charactger of the current
word or the next word or the next punctuation mark.
(c
b (back word) Moves the cursor backward to the first character of the current
word or previous word or the previous pu nctuation mark.
When the file being edited has a high number of punctuation marks, using these
Notes
e
commands becomes tedious. Uppercase WEB commands are utilised in these
situations. The capital WEB instructions are identical to the lowercase WEB commands,
with the exception that punctuation marks are ignored.
in
The Repeat Factor
Using a factor called the repetition factor, almost all vi commands can be executed
nl
multiple times. A repetition factor is just an integer that is added to the beginning of a
command. For example, the repetition factor in the 3w command is 3, the repetition
factor in the 5J instruction is 5, and so on. The cursor advances forward three words
O
when the command 3w is used, and the following five lines are joined to the current line
when the command 5J is used. It should be noticed that the repetition factor and the
command should not be separated by any blanks.
ty
Moving to Either Ends of a Line
The $ (dollar) command advances the cursor to the end of the current line. The
command 0 (zero) moves the cursor to the start of the line. The (caret) command
si
places the cursor at the start of the current line on the first non-blank character. In the
event of indented lines, this command comes in handy. A list of these commands is
provided below.
Command
r
Purpose
ve
$ (doller) Moves the cursor to the end of the current line.
0 (zero) Moves the cursor to the beginning of the current line.
˄ (caret) Moves the cursor to the first non-blank character at the beginning
ni
Using the H, L, and M commands, the pointer can be moved to the top of the
screen, that is, the first line on the current display screen, the bottom of the current
display screen, that is, the final line on the screen, and the middle line of the current
display screen, respectively. A list of these commands is provided below.
ity
Command Purpose
H (High) Moves the cursor to the beginning of the top line on the current screen.
L (Low) Moves the cursor to the beginning of the last line on the current screen.
m
M (Mid) Moves the cursor to the beginning of the middle line on the current
screen.
)A
Consider the following line of text, which is the last line on the current vi screen,
and the cursor is currently positioned someplace above it.
L$2b is the smallest set of vi commands that places the cursor on the C character
of the COMMAND. 2b moves the cursor backwards by two words to the beginning of
(c
the word, L moves the cursor to the beginning of the last line on the screen, $ moves
the cursor to the end of this line, and L moves the cursor to the beginning of the word.
When the following sequence of commands are supplied one after the other, the
Notes
e
cursor will shift to the lone comma character location on this line.
in
Moving by Screens
The and instructions can be used to move (scroll) a single screen full of
nl
information. Using and commands, a half-screen full of information can be moved
(scrolled). It should be noticed that f denotes forward movement, b denotes backward
movement, u denotes upward movement, and d denotes downward movement. The
O
behaviour of these instructions is summarised below. All of these commands can also
be used with repeat factors.
Command Purpose
ty
Ctrl-f (forward) Scrolls up the screen by one screen.
Ctrl-b (backward) Scrolls down the screen by one screen
Ctrl-u (up) Scrolls up the screen by half screen.
si
Ctrl-d (down) Scrolls down the screen by half screen.
If every line is numbered, the cursor can move faster throughout the full editor
buffer. The command mode :set number or :set nu commands can be used to number
every line in the file. The user can then use the line number and the go command G to
U
move the cursor to any desired line. The G command is also supplied in the editor’s
command mode, as is customary. The following are some examples.
Command Purpose
ity
20G Moves the cursor to the beginning of the line number 20.
1G Moves the cursor to the beginning of the very first line.
G Moves the cursor to the beginning of the last line.
m
The command can be used to find the line number of a current line. The name of
the file being edited, the current line number, the number of lines in the file, and the
percentage of the file (in lines) that precedes the cursor in the last line are all displayed
)A
you use terminal on Ubuntu, Linux Mint, or any other Linux distribution, you are
communicating with shell.
Shells are usually interactive, which means they accept user commands as
Notes
e
input and execute them. However, there are times when we need to run a series of
instructions on a regular basis, and we must type all commands into the terminal each
time.
in
3.2.1 Introduction to Shell Scripting, Concept of Vi Help
nl
The shell is a programme that gives the user a command-line interface to use the
operating system’s functionality. A shell script is a computer programme that performs
specified functions.
O
Shell scripts are usually used to avoid doing the same thing over and over again.
Instead of entering in commands one after the other n times, you can construct a script
to automate a series of instructions to be run one after the other.
ty
A text file containing one or more commands is known as a shell script. After all
of this, it may appear that this is a simple task, but that is exactly what a script is. The
power of shell scripts, on the other hand, is not in the concept’s simplicity, but in what
you can accomplish with them.
si
Shells are usually interactive, which means they accept user commands as
input and execute them. However, there are times when we need to run a series of
r
instructions on a regular basis, and we must type all commands into the terminal each
time.
ve
The First Shell Script
Each of these commands completes a simple task flawlessly and quickly. Using
ni
commands that are combined into a single unit within a file and run sequentially.
A shell script may also include commands for selective execution (control
commands), I/O operations (such as read and echo commands), commands for
ity
A shell script has the same name as any other file. A shell script’s name, on the
other hand, is always preceded by the.sh extension.
m
A shell programme runs in interpretive mode, which means that one sentence at
a time is executed. As a result, shell programmes take longer to execute. As a result,
shell scripts are typically developed at a later stage in the development of an application
)A
package. They’re converted to a higher-level language code after they’ve been tested
(like C).
An example of a shell script is given below, which may be found in the file my
script.sh.
(c
$cat my_script.sh
clear
Amity Directorate of Distance & Online Education
170 Unix Operating System and Shell Programming
e
echo “Today’s date is `date | cut –d “ “ –f 1–3`”
in
echo “GOOD LUCK”
nl
When a script file is created, it will only have read and write permissions. In other
words, a shell script will not have the execute permission assigned to it when it is
written.
O
Concept of Vi Help
VI can be classified into three states: command mode, insert mode, and last line
ty
mode.
●● Command mode
It allows you to move the screen pointer, delete characters, words, or lines, move
si
and copy a portion, and go into insert mode or last line mode.
vi is in Command Mode when it first starts up. vi considers any characters we input
r
as commands in this mode, and hence does not display them in the window. We can
use this mode to go through a file and delete, copy, or paste text.
ve
●● Insert mode
Only in insert mode can you type text. To return to command line mode, press
ESC.
ni
This mode allows you to add text to the document. In this mode, everything you
type is interpreted as input and then saved to a file. The vi opens in command mode
U
by default. You must be in insert mode to enter text. Simply type i to enter insert mode.
Insert mode can be exited by using the Esc key, which will return you to command
mode.
Set the editing environment, such as the searching string, listing line number, and
so on, before saving the file or exiting VI.
While vi is in Command Mode, type a colon [:] to enter Line Mode. Vi will wait for a
m
command while the pointer jumps to the last line of the screen. You can use this mode
to conduct operations like storing files and running commands.
However, we simplify VI into two modes in general, namely, the last line mode and
)A
Unix doesn’t seem to mind if you’re using one of the system’s built-in commands.
ls
Notes
e
Type the command at the shell prompt and hit Enter.
in
/home/jdoe/scripts/ls
It’s likely that you’ll want to develop a script that lists the files in your directory in
nl
a unique fashion, such as a script that lists the files and saves the listing to a new file
for later reference. (You could call it something else, but you could absolutely call it ls.)
Enter the entire path of the script to run it (so Unix doesn’t just run the first one it finds in
O
your path).
The :! command allows you to enter the name of a shell command, which is
executed within vi. For example, the command :!ls -F
ty
executes the ls command and displays the results on your screen.
The :r! command is similar to :!, but includes the standard output of the command
in the buffer. The command:
si
:r!ls -F
As you’ve seen earlier in this book, you can mix instructions on the same line.
r
Simply separate the commands with a ; and you’re done. To list files and display the
ve
current directory for example, type ls; pwd.
When you use a && to join two commands, the system will run them both in order,
but only if the first succeeds. You could for example, use mv todolist. To move your to-
do list to a different file and create a new one, type done && touch todolist. The second
ni
command will not run if the first command fails (for example, because you don’t have
permission to create a new file).
U
The command
:[x,y]s/pattern/replacement/flags
In the buffer, it looks for pattern between lines x and y and replaces instances of
m
pattern with the replacementtext. replacement is literal text but can contain several
special characters to refer to elements in the original pattern. pattern is a regular
expression; replacement is literal text but can contain multiple special characters to
)A
refer to elements in the original pattern. On lines 1 through 10, inclusive, the following
command changes the first occurrence of weeble with wobble:
:1,10s/weeble/wobble
Instead of specifying line numbers, the percent sign can be used to refer to the full
(c
file. In place of x and y, other special symbols might be used. The last line of the file is
denoted by $. To refer to the current line, leave x or y blank.
The flags g and c can be used to replace all instances of pattern on each line,
Notes
e
respectively, and to ask for confirmation for each replacement. Unless you just want to
replace the first occurrence of pattern on each line, you should use the g flag in most
cases.
in
You can also refer to lines with marks. Marks are just one-letter names assigned
to cursor points in a document. By dragging the cursor to a spot in the file and entering
nl
ma, the mark a will be set at that location. (Marks can be named after any letter from a
to z or A to Z.) With the command ‘a, you can move the cursor right to the mark a. (with
a backquote). The cursor will be moved to the beginning of the line that the mark an is
on if you use a conventional single quotation (as in ‘a’).
O
Marks let you to “remember” cursor points that represent a text region. If you want
to search and replace a block of text for example, you can move the cursor to the
beginning of the text, make a mark, then move the pointer to the conclusion of the text
ty
and type:
:’a,.s/weeble/wobble/
si
where ‘a refers to the line containing mark a, and . refers to the current line.
r
Using the d or dd commands to erase text and then replacing it with the P
command, as mentioned previously, is one technique to copy and move text. For
ve
example, if you wish to erase 10 lines, starting with the line containing your cursor, then
paste them somewhere else, simply type 10dd (to delete 10 lines), move the cursor to
the new text location, and press p. You can also copy text by entering 10dd followed
by P (at the same cursor point). This deletes the text and replaces it immediately. By
ni
moving the cursor and pressing p many times, you can paste the text somewhere else.
The yy command is similar to dd in that it “yanks” text without deleting it. As with
dd, you use p to paste the grabbed text. However, keep in mind that each yank action
U
will erase the text that was previously snatched from the clipboard.
Rather than lines, the deletion and yank commands can be used on more generic
regions. Remember that the d command deletes text by moving the cursor to the end
ity
of the line; for example, d$ deletes text from the cursor to the end of the line. y$, on the
other hand, drags text from the cursor to the end of the line.
Assume you want to yank (or delete) a text section. This can also be done with
marks. Set a mark, such as ma, at the beginning of the text that will be yanked. Using
m
the command y’a, move the cursor to the end of the text to be yanked. This moves
the text from the cursor to the mark a. (Keep in mind that the ‘a command moves the
pointer to the mark a.) When you use d instead of y, the text from the cursor to the mark
)A
is deleted.
Using registers is the most convenient approach to cut, copy, and paste text within
vi. A register is just a designated temporary storage area for text that you want to copy
between locations, cut and paste within the page, and so on.
(c
Single-letter names are assigned to registers, and any character from a to z (or A
to Z) is acceptable. The “ command (a quotation mark) defines a register, which is then
followed by the register’s name, as in “a for register a. Lowercase letters relate to the
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 173
same registers as their uppercase counterparts: using the lowercase letter overwrites
Notes
e
the previous contents of the register, whereas using the uppercase letter appends to it.
Similarly, “ay’a” yanks text off the cursor and places it in register a. There is no link
in
between the names of the mark and the names of the registers!
You can copy text between files by using registers. Simply copy the text to a
register, edit a new file with the:e command, then paste the text from the register.
nl
3.2.3 A Sample Code Snippet of a Shell Script
O
We define a function count lines in this version that counts the number of lines in
the file passed as an argument. The value of the argument is accessed by accessing
the variable Rs1 within the function.
Code 1:
ty
#!/bin/bash Counting the number of lines in a list of files
# function version
si
count_lines () {
local f=$1
}
ni
if [ $# -lt1 ]
then
U
exit 1
ity
fi
l=0
m
n=0
s=0
)A
while [ “$*” != “” ]
do
count_lines $1
n=$[ $n + 1 ]
s=$[ $s + $l ]
Notes
e
shift
done
in
echo “$n files in total, with $s lines in total”
Code 2:
nl
# !/bin/bash
O
function jump()
ty
# original value of Internal Field Separator
OLDIFS=$IFS
si
IFS=/
path_arr=($PWD)
ni
IFS=$OLDIFS
U
local pos=-1
do
pos=$[$pos+1]
dir_in_path=${#path_arr[@]}
(c
cwd=$PWD
limit=$[$dir_in_path-$pos-1]
Notes
e
for ((i=0; i<limit; i++))
do
in
cwd=$cwd/..
done
nl
cd $cwd
break
O
fi
done
ty
}
We can’t run our shell script right now because it doesn’t have permissions. We
must make it executable by entering the command –
si
$ chmod -x path/to/our/file/jump.sh
Summary
●●
r
The visual editor is vi. Full-screen editing software. Because vi is the POSIX
ve
standard editor, it is available on almost every Unix variant. Vi editor gained
popularity for its full-screen editing and aesthetic possibilities. Many ex (and ed)
commands still work in vi. Vim (vi improved) editors are available under Linux. To
use this editor, type vi at the shell prompt.
ni
●● A letter, a word, a line, a group of lines, or an entire file is considered text for the
sake of explaining and understanding the copying and transferring processes.
Copying entails extracting the relevant text and then inserting it into the file at any
U
desired location. Moving entails erasing the required text and then inserting it into
the file at any desired location.
●● The editor buffer is a duplicate of the file that vi works on. This buffer is the
ity
only one that is affected by any editing activity. The user must write or save the
modifications made to the original file for the change to take effect. Any of the save
commands can be used to save the contents of the editor buffer into the actual file.
There are up to 36 buffers available in vi.
m
●● If every line is numbered, the cursor can move faster throughout the full editor
buffer. The command mode :set number or :set nu commands can be used
to number every line in the file. The user can then use the line number and the
go command G to move the cursor to any desired line. The G command is also
(c
●● The G (go) Command: If every line is numbered, the cursor can move faster
Notes
e
throughout the full editor buffer. The command mode :set number or :set nu
commands can be used to number every line in the file. The user can then use the
line number and the go command G to move the cursor to any desired line. The G
in
command is also supplied in the editor’s command mode, as is customary.
●● Shell scripts are usually used to avoid doing the same thing over and over again.
nl
Instead of entering in commands one after the other n times, you can construct
a script to automate a series of instructions to be run one after the other. A text
file containing one or more commands is known as a shell script. After all of this,
it may appear that this is a simple task, but that is exactly what a script is. The
O
power of shell scripts, on the other hand, is not in the concept’s simplicity, but in
what you can accomplish with them.
●● A shell script may also include commands for selective execution (control
ty
commands), I/O operations (such as read and echo commands), commands
for repeated execution (loop–control structures) of a collection of commands or
statements, shell variables, and other features.
si
●● VI can be classified into three states: command mode, insert mode, and last line
mode.
●● Command mode: It allows you to move the screen pointer, delete characters,
r
words, or lines, move and copy a portion, and go into insert mode or last line
ve
mode.
●● Insert mode: Only in insert mode can you type text. To return to command line
mode, press ESC.
●● Last line mode: Set the editing environment, such as the searching string, listing
ni
line number, and so on, before saving the file or exiting VI.
●● If you wish to erase 10 lines, starting with the line containing your cursor, then
U
paste them somewhere else, simply type 10dd (to delete 10 lines), move the
cursor to the new text location, and press p. You can also copy text by entering
10dd followed by P (at the same cursor point).
ity
Glossary
●● POSIX: Portable Operating System Interface
●● PID: Process Id
●● Command Mode: This is the mode that the user enters as soon as the vi
m
●● Last Line Mode: This mode enables the user to use the commands in the vi
screen’s bottom line.
●● GUI:Graphical User Interface
●● Placing the Text: The operation of putting (also known as pasting in word
(c
moves the cursor just after or below the current contents of the general-purpose
Notes
e
buffer.
●● H (High): Moves the cursor to the beginning of the top line on the current screen.
in
●● L (Low): Moves the cursor to the beginning of the last line on the current screen.
●● M (Mid): Moves the cursor to the beginning of the middle line on the current
screen.
nl
●● Ctrl-f (forward): Scrolls up the screen by one screen.
●● Ctrl-b (backward): Scrolls down the screen by one screen
O
●● Ctrl-u (up): Scrolls up the screen by half screen.
●● Ctrl-d (down): Scrolls down the screen by half screen.
ty
Check Your Understanding
1. _ _ _ _ _ _ _ _ is the process of extracting a copy of the required text in vi.
a. Ranking
si
b. Yanking
c. Tanking
d. None of the above r
ve
2. Vi operates in two distinct ways, called _ _ _ _ _ _ _ _ _ and _ _ _ _ _ _ _ _ _ _ _ _.
a. Input mode and Command Mode
b. Input Mode and Running Mode
ni
3. Command for moves the cursor forward to the first character of the next word or the
next punctuation mark is:
a. w
ity
b. a
c. m
d. x
m
4. The _ _ _ _ command deletes the whole line on which the cursor is now positioned.
a. dd
b. de
)A
c. dt
d. none of the above
5. There are four basic edit commands:
(c
a. I,c,d,a
b. I,c,a,b
Notes
e
c. a,n,o,m
d. I,c,d,y
in
6. The _ _ _ _ _ _ command removes the character immediately preceding the cursor.
a. Y
nl
b. D
c. M
O
d. X
7. A shell programme runs in _ _ _ _ _ _ _mode
a. Running
ty
b. interpretive
c. both a and b
si
d. None of the above
8. The last line of the file is denoted by _ _ _ _.
a. #
b. @
r
ve
c. $
d. %
9. Which command is used to delete all files in a directory?
ni
a. del *
b. rm *
U
c. mv *
d. rmdir *
10. What is the function of cp command in Unix?
ity
Exercise
)A
1. Explain the command mode, insert mode and last line mode.
2. Explain commands to change word, change line.
3. Explain command to delete character, insert line, delete text.
(c
4. Define commands to delete current line, delete n lines, delete remainder of lines.
5. Explain command for copying and moving; saving and exiting.
e
7. Define Shell Scripting.
8. Explain the concept of concept of Vi help.
in
9. Define Running Shell Commands; Search and Replace Commands.
10. Define commands for changing and deleting text in shell commands.
nl
Learning Activities
1. Write a code to delete 20 lines in a single go.
O
2. How to delete a character in vi editor?
ty
1 b
2 a
3 a
si
4 a
5 d
6 d r
ve
7 d
8 c
9 b
ni
10 d
4. Linux Command Line and Shell Scripting Bible, by Christine Bresnahan and
Richard BLUM
5. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix, by
Arnold Robbins and Nelson H. F. Beebe
m
Bash Scripting, and Shell Programming, 2nd Edition, Andrew Mallett and
Mokhtar Ebrahim
8. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix,
Arnold Robbins and Nelson H. F. Beebe
(c
9. The Linux Command Line, 2nd Edition: A Complete Introduction, William Shotts
10. A Practical Guide to Linux Commands, Editors, and Shell Programming, Mark
Notes
e
G. Sobell
11. Learning the bash Shell, Book by Cameron Newham
in
12. Linux Command Line and Shell Scripting Bible, Christine Bresnahan and
Richard BLUM
13. How Linux Works: What Every Superuser Should Know, by Brian Ward
nl
14. UNIX: The Complete Reference, Second Edition, Rachel Klee, Douglas A.
Host, Richard R. Rosinski, Kenneth H. Rosen
O
ty
r si
ve
ni
U
ity
m
)A
(c
e
Learning Objectives:
in
At the end of this module, you will be able to understand:
nl
●● Shell Variables, Environment Variables and Keywords
●● Shell Script and Execution Methods
O
●● Redirection, Pipes
●● Pattern Matching, Command Substitution
●● Setting Positional Parameters (Set Command), Shift
ty
●● Assignment Statements, Arithmetic, Logical and Relational Operators, Read, Echo
●● Shift, Meta characters
●● Test Command: Numerical Test, File Test and String Test
si
●● for Loop
●● while and until Loop
●● case and switch
r
ve
Introduction
A shell can be accessed on a Mac or Linux machine via an application named
ni
“Terminal,” which is already installed. The Terminal is a command window where we’ll
type commands. To access the shell on Windows, you’ll need to download a separate
software.
U
fascinating things.
●● Your work will be less prone to errors thanks to the shell. When people repeat
the same action a hundred times (or even 10 times), they are more likely to
)A
make a mistake. Your computer can repeat the process a thousand times
without making any mistakes.
●● The shell improves the consistency of your work. When you work with the
command line (rather than a GUI), your computer retains track of every step
(c
you take, which you may refer to if you need to redo something. It also allows
you to clearly convey what you’ve done so that others can examine or apply
your method to new data.
●● Many bioinformatics tasks need a lot of processing power and can’t be done
Notes
e
on your own computer. Remote computers or cloud computing, which can
only be accessed through a shell, are ideal for these jobs.
in
Shell: A Command-Line Interpreter (CLI) that connects a user to an operating
system and allows them to run commands or write text scripts.
Process: A process is any task that a user does within the system. A process is a
nl
bit more complicated than a task.
File: A file is a piece of data that lives on a hard disc (hdd) and belongs to a user.
O
X-windows aka windows: X-windows, often known as windows, is a Linux mode
in which the screen (monitor) can be divided into little “pieces” called windows, allowing
a user to perform multiple tasks at once, go from one job to another conveniently, and
see graphics in a pleasing manner.
ty
Text terminal: A text terminal is a monitor that can only display text and has no
visuals or only a very basic graphics display.
Session: The time between logging on and logging off of the system is referred to
si
as a session.
Types of Shell
r
Bourne shell: The Bourne shell was one of the most common shells in early
ve
versions, and it quickly became the de facto standard. It was written at Bell Labs by
Stephen Bourne. Every Unix-like system contains at least one shell that is Bourne shell
compatible. The Bourne shell program’s moniker is “sh,” and it’s usually found at /bin/sh
in the file system hierarchy.
ni
C shell: Bill Joy created the C shell for the Berkeley Software Distribution. It has
a syntax similar to that of the C programming language. It’s mostly used for interactive
terminals, but it’s also used for scripting and operating system management. Many
U
The Bourne shell (sh) is the oldest shell still in use; Unix systems may also feature the
C shell (csh), Bash (bash), a Remote Shell (rsh), a Secure Shell (ssh) for SSL telnet
connections, and a shell called tclsh, which is a GUI-based Tcl/Tk shell.
The C and Tcl shells feature syntax that is quite close to that of the aforementioned
m
programming languages, while the Korn shells and Bash are extensions of the Bourne
shell, which is based on the ALGOL language with components from a number of
other languages thrown in for good measure. The Perl programming language, on the
)A
other hand, was influenced by numerous shells and tools such as awk, sed, grep, and
BASIC, Lisp, C, and so on.
on a machine or for download and/or purchase (tcsh). Shells based on Python, Ruby,
C, Java, Perl, Pascal, Rexx, and other languages are also widely accessible in various
formats. Old shell (osh) is another popular shell, according to its manual page, it’s “an
e
Edition UNIX.”
in
4.1 Introduction to Shell
A shell is a computer application that displays a command line interface that allows
you to control your computer with keyboard commands rather than using a mouse/
nl
keyboard/touchscreen combination to control graphical user interfaces (GUIs).
O
The shell serves as a communication channel between the user and the kernel.
When a user logs in, the login software validates the user’s username and password
before launching the shell. A command line interpreter is referred to as a shell (CLI).
ty
It evaluates the commands entered by the user and arranges for their execution. The
commands are programmes in and of themselves, and after they finish, the shell
prompts the user again ( 0% on our systems).
si
A skilled user can customise his or her own shell, and multiple shells can be used
on the same machine. The tcsh shell is installed by default on all staff and students at
the school.
r
Certain aspects of the tcsh shell assist the user in entering commands.
ve
Filename Completion - If you type part of a command, filename, or directory name
and then press the [Tab] key, the tcsh shell will automatically complete the rest of the
name. If the shell discovers that the letters you typed are used in more than one name,
it will beep, encouraging you to type a few more characters before clicking the tab key
ni
again.
The shell saves a record of all the commands you’ve entered. Use the cursor keys
to browse up and down the list or type history to see a list of past commands if you
U
The shell command interpreter is the user’s and operating system’s command line
interface. It’s the screen you’ll see after you’ve successfully signed into the system.
ity
You may use the shell to enter commands to run and to manage jobs that are
already executing. You can also change the commands you’ve requested using the
shell.
m
Bourne. This shell is the most ideal for building portable shell scripts and is available on
all Unix variants. The Shell Scripting Course goes over this in detail.
The Bourne-Again shell is the default shell that comes with most Linux-based
systems (“bash”).
(c
Other notable shells include the C Shell (csh), which was created at UCB and is
named after the C language’s syntax.
e
The Korn Shell is a popular shell on most commercial Unix variants. It was written
by Bell Labs’ David Korn and includes features from both the Bourne shell and the C
in
shell.
Finally, the Z shell is one of the most powerful and intriguing shells I’ve seen,
despite the fact that it hasn’t been standardised on any distribution I’ve seen. The zsh
nl
shell utility combines the finest features of the csh line of shell utilities with the greatest
features of the bourne or bash line of shell utilities.
Looking at the prompt is a reasonably simple approach to figure out what shell
O
you’re currently running. You’ll notice a dollar sign if you’re using a bourne shell or
derivative. At the present, this is most likely what you’ll see at your bash prompt. If
you’re using csh or tcsh, though, you’ll see a percentage indication. If you’re signed in
ty
as root or superuser, your prompt will almost always be a hash, regardless of your shell.
student@debian:~$ _
si
The tcsh prompt:
student@debian:~% _
Modern shells allow you to retrieve your command history, finish filenames
automatically, and even correct mistyped commands.
●● You can navigate through the history of commands you’ve entered in by using the
U
●● The backspace and delete keys, respectively, will erase the character behind and
beneath the cursor.
●● The insert mode is the default at your bash prompt, which means that if you input
a character anywhere on the line, it will be inserted there.
m
●● If you then press the insert key, it will toggle the mode to overwrite mode, which
means that whatever character is directly beneath your cursor will be overwritten.
●● Tab completion is another handy function. This means you can use the tab key to
)A
finish a command or be offered a list of command choices that match what you’ve
written so far.
Keyword
(c
The shell is a programme that gives the user a command-line interface to use the
operating system’s functionality. A shell script is a computer programme that performs
specified functions.
Shell scripts are usually used to avoid doing the same thing over and over again.
Notes
e
Instead of entering in commands one after the other n times, you can construct a script
to automate a series of instructions to be run one after the other.
in
A typical script might look like this:
#!/bin/bash
nl
echo “Welcome!! Please Enter Your Name”
read name
O
Let’s break down the above code:
#!/bin/bash
ty
The os is told which interpreter to use to execute the file by a line beginning with
#! Shell interpreters include ksh, zsh (macOS), csh (C shell), ssh (secure shell), and
others. The system is asked to use the bash interpreter in the line above.
si
echo “Welcome!! Please Enter your Name”
The echo keyword outputs the strings that are supplied to it as arguments. It sends
the output to stdout.
r
ve
read name
While the script is executing, the read command is used to receive input. When we
wrote read name, it created a variable called name and filled it with the input.
ni
Because keywords are reserved terms with reserved meaning, they cannot be
used as variable names.
fi else while do
done for until case
esac break continue exit
return trap wait eval
m
Control-flow Constructs
)A
If-else statements
Simple if statement
if [expression]
(c
statement
if
e
if [expression]
then
in
command1
else
nl
command2
if
O
if-else_if-else statement
if [expression]
ty
then
statement1
else [expression2]
si
then
statement2
else
r
ve
statement3
if
Nested if statements
ni
if[expression1]
then
U
if [expression2]
then
ity
statement1
else
statement2
m
if
else
statement3
)A
if
Switch-Case
A multiway branch statement is the switch statement. It makes it simple to route
(c
execution to different areas of code based on the expression’s value. For this, we have
the case command in shell scripting.
case “$var” in
Notes
e
#case 1
pattern_1) statement_1;;
in
#case 2
nl
patter_2) statement_2;;
case
O
Loops
Loops are strong tools that allow us to repeat a collection of commands.
ty
The commands are executed in a while loop until the condition is false.
while condition
si
do
statement
done
r
ve
For loop is an efficient way of writing loops. Syntax:
for i in range
do
ni
command
done
U
The keywords break and continue have the same meaning in any programming
language. A break command can be used to stop a loop from running, and a continue
ity
command can be used to make the loop run the next iteration.
Shell Variables
m
the same way they are in any other language. The rules for creating variable names are
fairly similar to those for creating filenames. To put it another way, variable names are
made up entirely of alphanumeric characters and the underscore (_) character, with the
first character being a letter.
(c
It’s important to note that the names are case sensitive. Shell variables are divided
into three categories. There are three types of variables: system variables, local
variables (sometimes known as user defined variables), and read-only variables.
Environment Variables
Notes
e
During the boot procedure or immediately after logging in, system variables are set.
The values of these variables have complete control over the working environment in
in
which a user works. Environmental variables are another name for these factors. In a
general sense, these are analogous to global variables. The values of these variables
can be adjusted if necessary to create a user-friendly environment. These variables are
nl
only written in capital letters by convention. System variables include PATH, HOME,
IFS, MAIL, SHELL, and TERM.
The Variable PATH A list of folders in a specific sequence is stored in the PATH
O
variable. Different directories are separated in this list by colons (:). The echo command
can be used to see the current value of this variable, as illustrated below.
$echo $PATH
ty
/usr/local/sbin:/usr/sbin:/usr/bin/X11:.
si
When a command is issued, the shell looks for its programme in each of the PATH
directories one by one. Because of the dot (.) at the end, it looks in the /usr/local/sbin
directory first, then the /usr/sbin directory, then the /usr/bin/X11 directory, and lastly the
r
current directory. The message command not found will be displayed if the programme
for the command is not found in any of these directories.
ve
As seen below, you can add more directories to the PATH using an assignment
statement (assigning values to variables is addressed in the following section).
$PATH=$PATH:/new/dir
ni
After all of the directories in the previous PATH variable have been searched, the
U
The HOME variable when a user logs in, he or she will be placed in the home
directory by default. This directory is determined by the system administrator when
ity
a user’s account is created and is stored in the /etc/passwd file. The variable HOME
stores the value of the home directory’s path. The echo command, as demonstrated
below, can be used to find out this value.
$echo $HOME
m
/usr/mgv
$
)A
When the command cd is run without any arguments, it defaults to the value
HOME.
The IFS Variable:Tokens used by shell commands to parse a string into substrings
such as a word (in the case of textual input) or a record into its component fields
are stored in this variable (in the case of structured type of data input). The three
(c
whitespace tokens—space, tab, and newline—are the default tokens. Because all of
these characters are non-printable, they can be read or validated with the od command,
Notes
e
as seen below.
in
nl
The first column of integers in the above output represents the relative offset from
the start of the input. The number 040 is the octal equivalent of the space character’s
O
ASCII value, 011 is the tab character, and 012 is the newline character. A newline
character has been added. The echo command is responsible for this.
The MAIL Variable: The absolute pathname of the file containing the user’s mail is
ty
stored in this variable. This file’s name is usually the user’s login name. /usr/spool/mail/
$LOGNAME is usually the value kept in this variable.
The SHELL Variable: The name of the user’s shell software is stored in this
si
variable as an absolute pathname. The echo command can be used to determine the
value of the variable SHELL, as illustrated below.
$echo $SHELL
/bin/bash
r
ve
$
The default shell is really set by the system administrator when a user’s account is
created. The user can alter his or her shell by changing the value of the SHELL variable
ni
if necessary.
The TERM Variable: This variable holds the information regarding the type of the
U
terminal being used. If TERM is not set properly, utilities like vi editor will not work. The
terminal type could be either ansi type or vt100.
There is no default value for TERM. It is the user’s responsibility to set it. Normally,
the terminal type is set in the user’s.profile file with a relevant statement. The username
ity
is stored in the LOGNAME variable. The current time zone information is stored in the
variable TZ. The system determines and uses its value. The primary prompt value ($,
in general) is stored in variable PS1, whereas the secondary prompt value (>, the right
chevron, in general) is stored in variable PS2.
m
Local Variables
Local variables in the shell are variables that are unique to a single user’s shell and
)A
that no other Unix process understands or needs to know about. These variables are
also known as user-defined variables because they are defined and utilised by specific
users. During the execution of a shell script, these variables are only present for a short
period. To put it another way, they are only available to the user’s shell environment and
not to other scripts or processes. For example, one could set the value of a variable
(c
Variable name construction rules Shell variable names, as previously stated, are
Notes
e
made up entirely of alphanumeric characters and the underscore (_) character, with
the first character being a letter. The shell is case-sensitive, as is customary. Variables
SUM, SUM, and SUM for example, are not the same.
in
Creating and defining a shell variable: The equal to (=) operator is used to define
a shell variable with no spaces on either side. A variable definition’s general format is
nl
shown below.
variable=value
O
ty
+
Because ps1 has been changed to + in the preceding examples, the last two lines
si
begin with +. Additionally, if the value to be assigned to a variable contains a space or
new lines, it must be quoted, as illustrated in the example below.
A shell variable’s kind The type of all shell variables is string. In other words, the
ni
ASCII format is used to record the values of variables. Naturally, all shell variables
are initialised as null strings by default. As a result, there is no need to type declare or
initialise shell variables while writing shell applications.
U
Taking a look at a shell variable By prefixing the variable name with a $, shell
variables are evaluated. Unless the $ is despecialised, all words preceded by a $
are identified and evaluated as variables when the shell reads a command line. The
ity
Notes
e
in
Shell variables can be valuable in speeding up the user’s interaction with the
nl
system, as seen in the example below. Instead of a long pathname, merely pn can be
used.
O
4.1.3 Shell Script and Execution Methods
Scripting languages have the advantage of working at a higher level than compiled
languages, allowing them to interact with things such as files and directories more
ty
readily. They have the disadvantage of being less efficient than compiled languages.
The trade-off is usually worth it; it can take an hour to write a simple script that would
take two days to code in C or C++, and the script will usually run quickly enough that
performance will not be an issue. Awk, Perl, Python, Ruby, and the shell are examples
si
of scripting languages.
Shell scripts can be written once and used across a variety of Unix systems
r
because the shell is universal among Unix systems and the language is standardised
by POSIX. The following are some of the benefits of using a shell script:
ve
Simplicity
The shell is a high-level language that allows you to express complex operations in
a simple and concise manner.
ni
Portability
You have a fair probability of being able to move your script to multiple systems
U
Ease of Development
ity
In most cases, you can develop a powerful, helpful script in a short amount of time.
A Simple Script
Let’s begin with a basic script. Assume you want to know how many people are
m
currently logged in. The who command displays who is currently logged in:
$ who
)A
e
On a large multiuser system, the listing may scroll off the screen before you can
count all of the users, which is tedious to do every time. This is an ideal situation for
in
automation. What’s missing is a means to keep track of how many people are using it.
The wc (word count) software, which counts lines, words, and characters, is used for
this. We’ll use wc -l to count only lines in this case:
nl
$ who | wc -l
Count users
O
6
The | (pipe) symbol establishes a pipeline between the two programmes, with the
output of one becoming the input of the other. The number of users logged in is printed
ty
by wcas a consequence.
The following step is to isolate this pipeline into its own command. You do this
by typing the commands into an ordinary file and then using chmod to make the file
si
executable:
$ cat >nusers
r
Create the file, copy terminal input with cat
ve
who | wc -l
Program text
^D
ni
Ctrl-D is end-of-file
$ chmod +x nusers
U
Make it executable
$ ./nusers
Do a test run
ity
The typical development cycle for short one- or two-line shell scripts looks like this:
you start by experimenting on the command line. Then, once you’ve figured out the
m
right incantations for the job, you’ll put them in a separate script and make it executable.
From now on, you can use that script directly.
When the shell executes a programme, it instructs the Unix kernel to create
)A
a new process and run the specified programme in it. For compiled applications,
the kernel knows how to achieve this. Because our nusers shell script isn’t a built
programme, it will fail to start when the shell requests the kernel to do so, returning a
“not executable format file” error. When the shell receives this error, it responds, “Aha,
it’s not a compiled programme; it must be a shell script,” and then proceeds to run the
(c
When there is just one shell, the “fall back to /bin/sh” method is very useful.
Notes
e
However, because most Unix systems now have several shells, a way to inform the
Unix kernel which shell to use when running a shell script is required. In fact, having
a universal method that allows you to directly execute any programming language
in
interpreter, not just a command shell, is advantageous. This is accomplished by using a
special first line in the script file, which starts with the characters #!.
nl
When the first two characters of a file are #!, the kernel looks for the entire
pathname of an interpreter to use to run the programme in the remainder of the line.
(Any whitespace in between is ignored.) In addition, the kernel looks for a single option
to send to that interpreter. The interpreter is invoked by the kernel with the supplied
O
option and the rest of the command line. Assume you have a csh script named /usr/ucb/
whizprog with the following initial line:
#! /bin/csh -f
ty
Furthermore, assume that the shell’s search path includes /usr/ucb (described
later). The command whizprog -q /dev/tty01 can be used by a user. The #! line is
interpreted by the kernel, and csh is invoked as follows:
si
/bin/csh -f /usr/ucb/whizprog -q /dev/tty01
Any interpreted language can be easily invoked using this approach. It’s an
r
excellent approach to run a standalone awk programme for example:
ve
#! /bin/awk -f
#! /bin/sh is a common starting point for shell programmes. If your /bin/sh isn’t
ni
POSIX-compliant, use the path to a POSIX-compliant shell. There are a few low-level
“gotchas” to be aware of:
standardese way of saying that a feature like this can be used as an extension while
still remaining POSIX-compliant.
Each subsequent script in this book begins with a #! line. The updated
Notes
e
nusersprogramme is as follows:
$ cat nusers
in
Show contents
nl
who | wc -l Commands to run
O
Shell Script Execution Methods
A shell script can be run in four different ways. As mentioned below, each way has
its own meaning.
ty
Execute Shell Script Using File Name
Use the relative or absolute path to execute the shell script file name, as illustrated
si
below.
$ cd /home/sathiya
$ ./scriptfile r
ve
(or)
$ /home/sathiya/scriptfile
Also, as seen below, you can run a unix shell script by specifying the interpreter in
the command line.
U
Regardless of what is used as shebang, the interpreter you provide will be utilised
to execute the code. Any interpreter will suffice (sh, ksh, bash, csh etc.,).
m
When you use “dot space dot slash” to run a shell script, it will run in the current
shell instead of forking a new one, as demonstrated below.
$ . ./scriptfile
In other words, this prepares the environment for you by executing the commands
(c
e
approach. i.e., after making changes to .bashrc or .bash profile, we can either logout
and login to see the changes, or we may use “dot space dot slash” to run.bashrc or
.bash_profile without having to logout and login.
in
$ cd ~
$ . ./.bashrc
nl
$ . ./.bash_profile
O
The. (dot) command is a synonym for the built-in source command. If you don’t
want to use the “dot space dot slash” approach, you can use the source command,
which is the same as the “dot space dot slash” method.
ty
$ source ~/.bashrc
si
Shells such as bash and korn in Linux allow programming constructs that can
be saved as scripts. Many Linux commands are scripted as a result of these scripts
becoming shell commands.
r
ve
To understand how their servers and applications are started, upgraded,
maintained, or uninstalled, and to comprehend how a user environment is constructed,
a system administrator should have a basic understanding of scripting.
ni
Redirection
U
A programme receives input from any source via standard input (the keyboard
is the default source) and outputs to any destination via standard output (the display
screen is the default destination).
It is possible to change the source, which is where a programme receives its input,
ity
as well as the destination, which is where the programme sends its output. Redirection
is a mechanism for changing the input source and/or output destination.
operators and > with the file descriptors 0 and 1, respectively. When the file descriptor
2 (which represents the standard error file) is necessary, it must be indicated explicitly.
A program’s output can be redirected using the > or >> operators. The usage of
Notes
e
> causes destination files to be overwritten. The current output will be appended to an
existing file when >> is used. If the destination file does not exist, it is created in either
scenario. It’s vital to remember that redirections are handled by the shell.
in
The input has been diverted from a file called sample in the following example. The
file name is not displayed because the shell (not the command) accesses the file.
nl
$wc <sample
320 103
O
$
The output has been routed to a file called newsample in the following example.
ty
$
In a single command line, you can use both the < and > redirection operators.
si
Despite the fact that such combinations are possible, the sequence in which the input
and output files are provided is not limited, as illustrated in the following example.
Or
r
ve
$wc>newsample< sample
Or
ni
$ >newsample< sample wc
When commands like the ones listed above are issued, the input redirection
operator will be handled first, followed by the > operator. This is because the < operator
U
Or
It should be noted that there should be no blanks between the file descriptor and
the redirection operator whenever file descriptors are used in a command line. As a
result, there is no blank between 2 and > in the first case and 2 and >> in the second
)A
Here are a few more illustrations. The concept of redirection can be better
understood by looking at these illustrations.
1. $wc sample
(c
$
Notes
e
The input file name has been specified manually in this case, while the output file
has been selected by default. The filename sample is presented as normal when the wc
in
command opens the file.
2. $wc
nl
I am the wc command
O
<ctrl-d>
3 18 100
ty
$
No unique input filename has been utilised in this example. As a result, it obtains
its input from the keyboard, which is the default standard input. The input has been
si
taken as whatever is written in at the terminal till a new line is entered in. By default, the
output file has been taken. The output does not include the filename. This is due to the
fact that the shell has already opened the file (not the command).
The contents of the conf file are copied into the meetings file in this example. The
meetings file is generated if it does not already exist. If a file named meetings already
ni
If the meetings file already exists, the contents of the conf file are appended to it.
The meetings file will be generated if it does not already exist.
ity
Pipes
One of Unix’s advantages is that each command completes a tiny task entirely and
quickly. To complete a lengthy task, a series of similar orders must be executed one
m
after the other. Consider a scenario in which a user wants to know how many people
are now utilising the system. Normally, this is done by first saving the who output to
a temporary file and then using the wc command to count the number of lines in that
temporary file, as shown in the following example.
)A
(c
Is it possible to send the output of the who command directly to the wc command
Notes
e
as input, bypassing the need for a temporary file? Yes, utilising the redirection notion
and an operator called the pipe operator, this is possible.
in
A pipe is a general technique for connecting or redirecting the output of one
programme as the input to another programme directly (without the use of any
temporary files in between). As depicted in Fig below, a pipe can be thought of as a
nl
pseudo file that connects two running applications (processes). The pipe, to be precise.
O
command1 command2
ty
si
acts as an output acts as an input
file to command1 file to command2
●● The programme to the left of the pipe (command1) treats the pipe as if it were an
ni
input file.
The pipe sign is represented by the special character | (vertical bar). The previous
illustration’s series of commands can now be written as follows.
ity
The output of the who command becomes the input to the wc command when the
above command line is run. The wc command’s output is displayed on the monitor.
m
Pipes can be used to connect more than two commands, as seen in the example
below. The grep command takes all lines in the output of the who command that contain
)A
In the preceding example, the command sequence counts the number of lines
containing the word murthy that are currently active users of the system.
A pipeline is a set of commands that are established using one or more pipes.
Notes
e
A long pipeline can be made more readable by typing one command per line, with
the pipe operator as the last character on each line, as shown in the example.
in
nl
The value of the shell’s default secondary prompt, PS2, is represented by the >
O
symbol, which is not the file redirection operator.
Actually, commands have no idea what a pipe or pipeline is. The shell is
responsible for establishing the necessary connections between instructions in a
ty
pipeline.
When the shell encounters a command pipeline, it starts all of the commands in
the pipeline practically simultaneously. Consider the following command: $who | wc –l.
si
Both the who and wc command programmes are active at the same time. Who will write
its output to wc’s standard input, while wc will read from who’s standard output. If the
pipe becomes full, who waits for wc to read from it, and if the pipe becomes empty, who
r
waits for wc to write into it. As a result, a pipe acts as an interprocess communication
agent.
ve
A close examination of the preceding material indicates that there is some
parallelism in the execution of distinct instructions in a pipeline.
Pattern Matching
We can consider notions like ‘all files with the suffix.c’ or ‘all parameters to the
command that are three characters long and begin with a digit’ using pattern matching
m
notation. The shell employs pattern matching in a variety of scenarios, which we will
discuss when we come across them. If the shell meets a word that contains any of
the symbols below (unless they are ‘escaped’ by being preceded by a backslash or
)A
?*[
The programme will then try to match that term to filenames in the current directory
or absolute pathnames (if they start with /). A? matches any single character, * matches
(c
anything, and [initialises a list of characters that it matches. If the term starts with a *
or a?, it will only match filenames that do not begin with a dot in the current directory.
When the shell has figured out which filenames the term corresponds to, it will replace it
Notes
e
with all of those names.
Try:
in
$ echo *
Because * matches anything, it will match any files in the current directory. NOTE
nl
that echo * will not format filenames into neat columns, and the output may be longer
than your terminal’s width, with the result looking identical to ls. Assume you have a file
called mycommand.
O
$ echo m*
Because m* matches all filenames in the current directory that begin with the letter
m, all of those filenames, including mycommand, will be displayed.
ty
Example:
use 1s –1d to list all ‘dot’ files in your home directory.
si
Solution: use 1s –1d, but instead of giving it argument ̅ or $HOME to list files in
your home directory, you must isolate only those whose names commence with a dot.
The ‘dot’ files in your home directory will each be matched with either ̅/.* or $HOME/.*
and one solution is therefore:r
ve
$ 1s –1d $HOME/.*
A * matches any number of characters, but a? matches only one character, but is
otherwise used in the same way as *.
ni
$ echo ????
will show all filenames containing four characters in their names in the current
directory (but do not commence with a dot). ??? would not match a/b since pattern
U
Example:
ity
m
for example, the files in /lib, which contain files of the form libsomething.a, which are
library files utilised by the C compiler. Pattern matching is useful for identifying files with
names that are known to be of a certain’shape.’
(c
Example:
Notes
e
in
nl
Between the symbols [and] is a list of characters or one or more expression
ranges, possibly preceded by the! (exclamation mark) character. A range, represented
by two characters separated by a hyphen, refers to any characters that are lexically
O
between (and including) those two. As a result, [m-q] matches any lower-case letter in
the range of m to q inclusive. It’s worth noting that in a range, the character to the left of
the hyphen must lexically precede the character to the right, else the range will match
nothing. The! signifies that any single character not specified between the brackets will
ty
match the term. Simple patterns are shown in the table below.
r si
ve
Example:
ni
U
ity
Example: Create a script that will delete all files in the current directory with the
suffix.o, prompting you to confirm that you want to delete each one and confirming that
it has been gone.
m
Solution: these files are matched by *.o, and we can pass the files one-by-one to
rm –1 using a for loop. rm yields exit status 1 if it fails to remove its argument.
)A
(c
Notes
e
in
nl
since we would then have been unable to generate the ‘confirmation’ message.
O
The case statement
Case is a statement that requires pattern matching. It works by beginning with
ty
case expression in
A statement that involves pattern matching is case. It works by starting off with
si
case expression in
where expression has a value (and would typically be a variable preceded by $).
Following that, there is a sequence of pattern) command-list; and the case statement is
terminated with esac. r
ve
The value of expression is calculated, and the first of the matching patterns is given
the command-list below. For instance, a very basic version of the command file that
only looks at the suffix of its first argument would look like this:
ni
U
ity
Patterns in case statements are matched with the expression at the beginning
of the case statement, not with any filenames. However, if a pattern appears within a
command-list in a case statement, it is matched to filenames just like previously. The
script below lists the files in the current directory; however, it asks if you want to include
the ‘dot’ files:
m
)A
(c
It’s worth noting the technique for asking the user a yes/no question here:
Notes
e
the answer is expected to begin with a Y or an N, in upper or lower case, and that’s
enough. As a favourable answer, this script would cheerfully take the input Yqwerty.
Instead of [Yy]*, the pattern would be [Yy][Ee][Ss] if the user had to input exactly the
in
word YES.
Example:
nl
Wtite a script named compile to take a single argument representing the name of
a program written in a high-level language, and then compile that program using one of
cc (for C), pc (for Pascal) or f77 (for FORTRAN). File suffixes. c, .p and .f respectively
O
are assumed to indicate the language type.
ty
r si
ve
ni
Instead of duplicating the line (or lines) of instructions, you can combine the two (or
more) patterns if the same command is required for both. As a result, the pattern
U
sam|chris
Example:
Write a script to read in a string representing a telephone number as dialled from
the UK, and indicate whether it is an oversease number (commencing 00 or 010), a
value added number (commencing 0898 or 0891), a freephone number (commencing
m
0800), a service number (three digits commencing with a 1) or a national code (ten
digits commencing 0 or eleven digits commencing 01).
Solution: We could use many if statements, but the script would be very mesay.
)A
Notes
e
in
nl
O
ty
Command Substitution
It’s possible to run a command within a command on Unix. The date command for
si
example, can be used within the echo command by writing the following command line.
r
ve
The command to be run (in this case, date) within another command (in this case,
echo) must be written within a pair of backquotes (“), as demonstrated in the preceding
ni
example. While parsing the echo command’s parameters list, the shell treats the
backquoted words as a command, runs it, and replaces the result of that execution at
the corresponding location in the parameters list. Command substitution is the term for
U
this procedure.
The command substitution in Korn shell is done by using a $ sign followed by the
command enclosed in parenthesis, as shown below.
ity
m
actually saved in special shell variables. There are nine variables like this that capture
Notes
e
and store values from a command line. These are $1, $2, $3, $4, $5, $6, $7, $8, and
$9. The first argument is stored in the $1 variable, the second in the $2 variable, and so
on. The arguments are referred to as positional parameters because they are assigned
in
as values to the special variables $1, $2, $3, and so on, depending on their actual
places in the command line.
nl
O
ty
si
Fig: Positional Parameters
The $0 Variable
r
ve
$0 is a special shell variable that stores the programme name and the parameter
number 0 (zero).
ni
total amount of arguments, or parameters. The list of all arguments is kept in the $*
variable. It’s worth noting that the $# variable is equivalent to argc in C, while the $*
variable is identical to argv[].
The $@ variable, like $*, stores a list of all the arguments in the command line.
ity
When these variables $* and $@ are used within quote marks, the contents of $* are
treated as a single string, whereas each of the arguments in $@ is quoted separately
and treated as a separate string argument.
m
as the positional parameters can be better understood by studying this script and its
results.
(c
Notes
e
in
nl
O
The following is the output of a sample run of the above programme pos par.sh.
ty
r si
ve
ni
When the preceding script is run with the existing filenames fa, fb, and fc as
parameters, the following output is obtained.
)A
$sh pos_par1.sh fa fb fc
fa fb fc
(c
Because of the special variable “$*” in the preceding sample, the first ls command
Notes
e
sees all of the command line parameters as a single argument “fa fb fc,” which
obviously does not exist, and so an error message is displayed.
in
The second ls command, however, treats the command line arguments as
independent arguments “fa”, “fb”, and “fc” due to the special variable “$@”. As a result,
there is a report on the existence of these files.
nl
The set Command: Assigning Values to Positional Parameters
The values of a script arguments are automatically assigned to positional
O
parameters. It should be noted that the equal to (=) operator cannot be used to set
values to positional parameters. The set command, on the other hand, can be used to
assign values to positional arguments, as demonstrated in the following example.
ty
$
Friends are assigned to the parameter $1, in to the parameter $2, need to the
si
parameter $3, and so on when the above command line is run. The echo command, as
seen below, can be used to verify these assignments.
$echo $1 $4 $6
r
ve
friends are indeed.
In some cases, more than nine parameters can be given in a command line. As
a result, the nine positional parameters are assigned extra values. The following is an
example of how to allocate surplus arguments to positional parameters.
U
ity
In the above example an attempt to echo the value of $10 has resulted in
Everyone0 ($1’s value and a zero) and the value of $11 has resulted in Everyone1 ($1’s
value and a 1). Such situations are handled using the shift command.
m
Notes
e
in
nl
O
The set Command with Options
This command accepts a variety of options, including –x, –v, – –, and others.
ty
Debug shell programmes with the –x and –v arguments.
si
In some cases, the set command’s arguments are passed on using command
substitution. This method can be prone to errors at times. Using the special option –
–, you can avoid such a predicament (double hyphen). The following two examples,
r
together with the discussion that follows, will help you better grasp the unique
ve
conditions in which - – is utilised.
Example 1
$is –1 myfile
ni
$
ity
Example 2
$set ‘grep murthy student.1st’
CDPATH=:/users/mgv:/user/spool
m
EDITOR=/bin/vi
HOME=/usr/msv
)A
––––
––––
The assignment of the first field of the long listing to the parameter $1, the second
(c
field of the long listing to the parameter $2, and so on is anticipated in the first example
above. However, the - (hyphen) character appears as the first character in the output of
the ls –l myfile command. This hyphen character is interpreted by the set as indicating
Notes
e
the presence of an option. However, the character that comes after – (the hyphen) is
not an acceptable option in this circumstance. As a result, the report on bad options is
provided.
in
The goal in the second example was to assign different positional parameters to
the various fields of the selected record and then use them as needed. The search fails
nl
in this situation because the pattern murthy does not exist in the student.lst database
file. There will be no arguments given to the set. As mentioned in the preceding section,
this causes the set to display the contents of all system variables in alphabetical order.
Clearly, this was not anticipated.
O
The – – option was created to avoid the two specific scenarios mentioned above.
The two instances above can be written correctly as shown below.
ty
si
shift Command
Due to the fact that there are only nine positional parameters, a command line
can only have a maximum of nine arguments. If a command line contains more than
r
nine arguments, no error is shown. However, as seen in the preceding section, the
ve
behaviour will be equivocal. The shift command is used to deal with situations like this.
The shift statement, when used, shifts the values supplied to positional parameters
to the left by an integer value specified as the shift statement’s argument. As a result,
the statement $shift 5 shifts the argument values five positions to the left.
ni
The first five values are shifted out, and the sixth value is assigned to $1, the
seventh value to $2, and so on. It should be noted that when using the shift command
to shift particular numbers, the shifted values are lost. The following is an example of
U
how to utilise the shift command. This example should be studied in conjunction with
the previous one.
ity
The shift command, when used without an argument, moves the contents of the
m
e
Table: Arithmetic and logical operators
in
nl
O
ty
Arithmetic operators such as +, -, and * are used to write arithmetical expressions,
bitwise operators (<>, &) are used to operate on bits of specified integers, and logical
si
operators (AND, OR) are used to combine logical expressions.
Example:
r
The script below checks if the two words are present in the given file, with the
words and file being supplied as command line arguments.
ve
ni
U
ity
m
We must remember the following two points when utilising logical expressions
)A
containing instructions associated with the logical AND operator (&&) or the logical OR
operator (||):
●● If the two commands are linked by the logical AND operator, the shell will only
run the second command if the first command returns a true value.
(c
●● If the two commands are linked by the logical OR operator, the shell will only
run the second command if the first one returns a false value.
Relational Operators
Notes
e
The relational operators are mostly used for object comparison. The operators
indicated in the table below can be used to compare two numerals.
in
nl
O
Example:
ty
The test command is used in the following shell script to see if the command line
arguments are provided to the script.
r si
ve
ni
To see if any command line arguments are supplied to the script, the parameter $#
U
is compared to the relation operator -eq. The value of the command line parameters will
be 0 if none are supplied to the shell script, resulting in the message No command line
arguments are passed.
ity
Read
The read command is used to give interactive input to a shell programme (script).
This command only reads one line before assigning it to one or more shell variables.
The script below is found in a file called readname.sh. This script simply reads a name
m
$cat readname.sh
)A
read name
$
(c
When a read command is encountered, the shell waits or pauses for the values to
be input and a carriage return. Just before a read statement, an echo command is used
to provide a request prompt for providing the required information. Otherwise, the user
Notes
e
may be unaware of the reason for the system’s pause.
The file readname.sh is run after it has been granted execute permission, as seen
in
below.
nl
O
echo
Messages are shown using the echo command. It comes particularly handy
ty
when creating interactive shell programmes. It accepts zero, one, or more arguments.
Arguments can be specified as a string within a pair of double quotes or as a series of
individual symbols (“ “). The following are some examples.
r si
ve
ni
U
The echo command without an argument prints a blank line (Example 1). As
ity
can be seen from Examples 2 and 3 above, if there are extra spaces between the
arguments, they are adjusted and the output is printed in a standard form, with just one
blank between the different arguments. However, when the message is given in the
form of a string argument, the message is printed as it is. In other words, when a string
is given as an argument, it is printed without the adjustment of the blanks as shown in
m
Example 4 above. As seen from the last example, if an evaluatable argument is given, it
is first evaluated and its value is printed along with the other arguments. This command
is chiefly used to write output statements in shell programs. It is important to note that
)A
extract lines or records that start with a particular pattern. If you want to search and
extract all the lines or records that start with the word Murthy for example, the search
pattern will be ‘^Murthy.’ It’s worth noting that quote marks are used to surround search
Notes
e
patterns with more than one word or metacharacter.
$—The Dollar Persona: This metacharacter is used to find and extract lines or
in
records that have a specified pattern at the end. For example, if you want to search
and extract all lines or records that end with the word Murthy, the search pattern will be
‘Murthy$’.
nl
Knowing how to extract a line or a record with a specified word at the start and
finish makes it simple to extract lines with only the desired word or phrase. If the user
wants to find and extract all lines that contain only Murthy for example, the search
O
pattern will be ‘^Murthy$’. It’s worth noting that the search pattern ‘^$’ returns all blank
or empty lines—lines that contain no characters.
.—The Character of the Dot Except for a newline character, the dot is used to
ty
match any single character. For example, if the user wants to extract all lines or records
with the name Murthy or Murthi spelled either way, the search pattern will be ‘Murth.’ Of
course, names like murtha, murthb, and so on will be picked up by this pattern. Despite
the fact that names like murtha and murthb are not spelled this way, murth. is one of
si
the options in this situation. If necessary, the character class can be used to avoid any
potential problems with the. (dot) matching with characters other than y and i.
r
*—The Asterisk Symbol Multiple characters are represented with an asterisk.
This metacharacter denotes the presence of one or more instances of the preceding
ve
character. For example, if you want to find all the lines that include the pattern
constructed with the letter M followed by zero or more other characters, the regular
expression, or search pattern, is ‘M.*’.
ni
The shell’s use of asterisk has already been explored. The metacharacter *
(asterisk) matches with zero or more characters at that location in the shell. It’s
important to observe the difference in behaviour between this metacharcter and the
shell, as well as regular expressions used as search patterns with the grep family of
U
programmes. Binding the search patterns using quote marks is one of the approaches
used to signify regular expressions.
Shell metacharacters are characters that have specific meaning for the shell and
ity
are not letters or numerals. They can’t be used in filenames because of their specific
purpose. Shell metacharacters, like wildcards, can be used to indicate multiple
files in several directories in a single command line. Knowing how to use them in
commands will save users a lot of time in the future. When these characters are used in
m
commands, no space is necessary before or after them. Using spaces before and after
a shell metacharacter, on the other hand, will be obvious.
Some shell metacharacters and their functions are listed in the table below.
)A
(c
Notes
e
in
nl
O
ty
r si
ve
ni
U
ity
m
command-line interpreter. [1] Shell scripts come in a variety of dialects, which are
referred to as scripting languages. Shell scripts can execute a variety of tasks, including
file manipulation, programme execution, and text output. A wrapper is a script that sets
up the environment, runs the programme, and performs any necessary cleaning or
logging.
(c
Batch files (MSDos-Win95 stream, OS/2), command procedures (VMS), and shell
scripts (Windows NT stream and third-party derivatives like 4NT—article is at cmd.exe)
are all terms used to describe the automated mode of running an operating system
Notes
e
shell; each operating system uses a different name for these functions, and mainframe
operating systems are associated with a number of terms.
in
4.3.1 Test Command: Numerical Test, File Test and String Test
The test command is included in the Bourne shell since the if statement was
nl
meant to function with commands, and many scripts require value comparison. The
test command serves as a one-stop shop for testing files, values, and just about
anything else. Test can compare data, check for file permissions, and even check for
O
the existence of files, depending on the command-line variables you supply. The test
command is usually implemented as a built-in command by the shell. However, test is
also available as a disc application, usually in /usr/bin.
You must figure out the required command-line options for the test command
ty
to build up an expression to test in order to govern what kind of test is conducted.
The command-line arguments are used by the test command to determine which
comparisons to make. The test command then exits with a 0 if the expression tested
si
was true and a nonzero value (1) if it was false. If test encounters an error, it returns a
value that is larger than one.
You use the command-line options to specify how to compare values when
creating a test. r
ve
Consider the following scenario:
test $x –x –eq $y
Using the -eq command-line option, this example checks whether the values of x
ni
and y are equal. It’s presumed that this is a numeric equality check. If necessary, the
shell will attempt to convert strings to integers.
U
Numerical Test
The numeric test possibilities are listed in the table below. Obviously, the variables
x and y should have a numerical value.
ity
m
)A
Example:
Notes
e
in
nl
O
ty
r si
ve
ni
U
ity
m
)A
(c
Notes
e
in
nl
O
You should get the following output after running this script:
ty
r si
ve
The test num script has a large number of if statements that demonstrate various
ni
Prior to each if statement, the script uses the echo command to produce a
message. By printing the test on the same line as the result, it should be easier to
U
recognise the test. The test’s predicted outcome, true or false, is also shown in the
script. Consider the following scenario:
ity
Because x has been assigned the value 5, the variable x is actually less than or
m
equal to 5. The word Expected appears in the echo statement that outputs the expected
result, making it very clear which result you should get when you run this script. The
echo statements also provide a shorthand to show the comparison, such as X = 5 for X
)A
equals 5, X!= Y for X does not equal Y, and X= 5 for X is less than or equal to 5. For the
sake of brevity, this abbreviated text does not employ shell syntax in the output.
String Test
(c
Text strings can also be compared using the test command. The test command
compares text strings one by one, character by character. For the comparison to be
valid, the characters must be same.
For some reason, text has been called strings or text strings in just about every pro-
Notes
e
gramming language. The usage comes from the idea of a text string as a string of
characters (ofeten of bytes), one after another. The analogy doesn’t really hold up, but
in
the terminology has.
You can compare text string values or determine whether or not a string is empty.
The text string test choices are listed in the table below.
nl
O
ty
si
Example:
#!/bin/sh
r
ve
a=”abc”
b=”efg”
ni
if [ $a = $b ]
then
U
else
fi
if [ $a != $b ]
m
then
else
fi
(c
if [ -z $a ]
then
Notes
e
echo “-z $a : string length is zero”
else
in
echo “-z $a : string length is not zero”
fi
nl
if [ -n $a ]
O
then
ty
else
fi
if [ $a ]
then
r si
ve
echo “$a : string is not empty”
else
ni
fi
U
Output:
ity
The test strings script, like the previous one, comprises a number of if statements,
each of which performs a different type of test. This test makes use of two variables:
string, which is set to the beginning of a poem, and notset, which is not set and hence
(c
has no value.
Examine the tests to ensure you grasp the information, like in the prior example.
Notes
e
Some of the examinations are challenging. The following for example, differs just by a
space:
in
nl
When comparing text strings, each space might be crucial. It’s also worth noting
that empty strings have no length:
O
ty
File Test
You can test files with the test command in addition to the numeric and string tests.
si
In fact, most scripts utilise the test command primarily to test files.
The file options for the test commands are listed in the table below.
r
ve
ni
U
Example:
ity
m
)A
(c
Notes
e
in
nl
O
ty
Unless there is a problem, there is no output when you run this script with the
si
following command:
$ sh lockdown
r
The lockdown script’s outer for loop loops through all of the files in the current
ve
directory. The script utilises the test command (with the [shorthand syntax) to determine
which permissions are currently accessible for each file. The script then executes the
chmod command in long letter format. The chmod command’s options remove the
group’s and other users’ permissions. The options also keep any permissions that the
ni
For each value in the supplied set, loops are used to execute a command or a set
of commands.
Syntax
ity
In this syntax, the variable will be assigned one of the list_of_values, and the
commands between do and done will be executed on that variable. This is repeated
for each value supplied to the variables until all commands between do and done have
been executed.
m
Examples
(a) The script below prints the number sequence from 1 to 5.
)A
(c
Notes
e
in
nl
As can be seen in this output, the variable x is first given a value of 1, after which
the body of the loop, i.e., the echo command, is run. Following that, the variable x
O
will be given a new value of 2, and the body will be run. This is done until the loop is
completed for each variable’s value.
(b) The script below prints all filenames that start with the letter b in the current
directory.
ty
r si
ve
Variable k will be assigned a file (if one exists) from the hard disc with the initial
character b, followed by any number of characters or numerals in the aforementioned
script. One by one, the filenames will be set to variable k.
ni
Output
File name is b.lst
U
(c) The following script outputs one word per line from the file’s contents. As a
command line argument, the filename will be supplied.
ity
m
)A
(c
e
while
in
While loops are used to repeat a group of statements for as long as a logical
expression is true.
nl
O
ty
Examples
(a) The script below displays the number sequence from 1 to 10.
#!/bin/bash
si
n=1
do r
ve
echo $n
(( n++ ))
done
ni
Using the echo command, extra spaces can be readily deleted. If the echo’s
argument is quoted, it is printed with tabs or multiple spaces between the arguments.
The excess spaces are automatically deleted from the arguments if the parameter is
used without quotes in the echo command.
ity
(b) The script below removes any excess spaces from the file’s lines and shows
them on the screen. The filename is specified on the command line.
m
)A
(c
Notes
e
in
nl
Through the while loop, the lines of the file school.tst will be assigned to the
variable k one by one, and then echoed on the screen.
O
until
The till loop is used to repeat a collection of statements until the logical expression
specified is false. The control will exit the loop as soon as the logical expression
ty
becomes true.
r si
ve
Example
ni
U
ity
m
)A
(c
In the file d.bat, the following script saves the series of numbers from 1 to 100.
Notes
e
Note: The redirection symbol>>is used for appending the contents in the file (without
affecting the earlier data). The redirection sysmbol>creates the new file (the previous
in
data, if any, will be erased).
nl
In shell scripts, the switch case statement is a more efficient alternative to the
previously learnt if-elif-else statement. The idea behind switch case statements is
that we give the statement several cases (conditions) that, when met, would execute
O
specific sets of commands.
ty
r si
ve
ni
U
When the shell encounters a case construct, the control flow will behave as follows.
Each pattern is compared to the string value that follows immediately after the keyword
case. When a match is detected, any commands that follow that pattern are executed
until the next double semicolon characters (;;), at which point the control moves beyond
m
the esac (that is, all other remaining patterns will be skipped and the control goes out of
the scope of the case).
wildcard characters like *,?, and others can be used to create case patterns. Within the
scope of a case statement, a default pattern with simply a * character can be used as
the last pattern, as seen below.
(c
Notes
e
in
nl
O
ty
It is clear from the above description that the case construct acts similarly to the
switch construct in c.
si
A Simple Menu Script
A sample script called menu.sh is provided below that accepts values from 1 to
6 and displays (a) a list of files in the current working directory if option 1 is selected.
r
(b) the current date if option 2 is selected, (c) the process status if option 3 is selected,
ve
(d) all current system users if option 4 is selected, and (e) displays the current working
directory if option 5 is selected. If the choice is 6, (f) exits, and if the choice is anything
else, (g) displays an invalid option message and exits.
ni
U
ity
m
)A
(c
Note: It may be observed that certain characters such as \e,\n have been jused in the
Notes
e
above program. TGhese sequences of characters have been used not to have a car-
riage return as well as to have a new line respectively. However, these do not behave
in
as expected under linux. In Linux all the excape characters will be effective only when
used with the -e option and the white space characters will be effective when used with
the -n option.
nl
Summary
●● The shell offers programming capabilities in addition to being an interface between
O
users and the kernel and command processor. To put it another way, a shell can
be thought of as a programming language. Variables are defined and used using
a shell in the same way they are in any other language. The rules for creating
variable names are fairly similar to those for creating filenames.
ty
●● During the boot procedure or immediately after logging in, system variables
are set. The values of these variables have complete control over the working
environment in which a user works. Environmental variables are another name for
si
these factors.
●● Local variables in the shell are variables that are unique to a single user’s shell
and that no other Unix process understands or needs to know about. These
r
variables are also known as user-defined variables because they are defined and
ve
utilised by specific users. During the execution of a shell script, these variables are
only present for a short period.
●● A programme accepts standard input from any source (the keyboard is the
default source) and standard output from any source (the display screen is the
ni
default destination). It’s feasible to change both the source and the destination
of a program’s input and output. Redirection changes the input source or output
destination.
U
●● If the two commands are linked by the logical OR operator, the shell will only run
Notes
e
the second command if the first one returns a false value.
●● Shell metacharacters are non-letter characters that have special meaning for
in
the shell. Because of their purpose, they can’t be used in filenames. Shell
metacharacters, like wildcards, can be used to specify numerous files in various
directories. Using them in commands will save users time in the future.
nl
●● The test command is included in the Bourne shell since the if statement was
meant to function with commands, and many scripts require value comparison.
The test command serves as a one-stop shop for testing files, values, and just
O
about anything else.
●● Test can compare data, check for file permissions, and even check for the
existence of files, depending on the commandline variables you supply. The test
command is usually implemented as a built-in command by the shell. However,
ty
test is also available as a disc application, usually in /usr/bin.
●● Text strings can also be compared using the test command. The test command
compares text strings one by one, character by character. For the comparison to
si
be valid, the characters must be same.
●● You can test files with the test command in addition to the numeric and string
r
tests. In fact, most scripts utilise the test command primarily to test files.
ve
●● In shell scripts, the switch case statement replaces the if-elif-else sentence. This
statement has numerous cases (conditions) that, when met, execute various
sets of actions. This command allows for multi-way decision-making. It works by
matching patterns. It only works with string tests.
ni
Glossary
●● Redirection: Redirection is a mechanism for changing the input source and/or
output destination.
U
●● Pipes: A pipe is a general technique for connecting or redirecting the output of one
programme as the input to another programme directly
●● The $0 Variable: $0 is a special shell variable that stores the programme name
ity
●● Read: The read command is used to give interactive input to a shell programme
(script).read command. his command only reads one line before assigning it to
one or more shell variables.
●● String Test: The test command compares text strings one by one, character by
character.
(c
●● File Test: You can test files with the test command in addition to the numeric and
string tests.
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 229
●● while loop: While loops are used to repeat a group of statements for as long as a
logical expression is true. Notes
e
●● until loop: The till loop is used to repeat a collection of statements until the logical
in
expression specified is false. The control will exit the loop as soon as the logical
expression becomes true.
●● Switch-Case: A multiway branch statement is the switch statement. It makes
nl
it simple to route execution to different areas of code based on the expression’s
value.
●● The MAIL Variable: The absolute pathname of the file containing the user’s mail is
O
stored in this variable.
●● The SHELL Variable: The name of the user’s shell software is stored in this
variable as an absolute pathname.
ty
Check Your Understanding
1. _ _ _ _ _ _ _ _ _ command can be used to make the loop run the next iteration.
si
a. while
b. break
c. continue
r
ve
d. for
2. POSIX Stands for:
a. Portable Operating System Interface
ni
b. programme
c. Case
d. none of the above
4. Using the _ _ _ _ __ operator, the input source is redirected (to something other than
m
b. greater than
c. equal to
d. twice greater than
5. The _ _ _ _ _ _ _ _ _ operators are used to redirect the output destination
(c
a. Less than
b. Equal to
Amity Directorate of Distance & Online Education
230 Unix Operating System and Shell Programming
c. greater than
Notes
e
d. None of the above
6. A_ _ _ _ _ is a general technique for connecting or redirecting the output of one
in
programme as the input to another programme directly (without the use of any
temporary files in between).
a. Pipe
nl
b. input redirection
c. Standard input
O
d. output redirection
7. The_ _ _ _ __ operators are mostly used for object comparison.
a. Conditional
ty
b. Relational
c. Arithmetic
si
d. Logical
8. The _ __ _ _command is used to give interactive input to a shell programme (script).
a. read r
ve
b. Echo
c. Case
d. None of the above
ni
c. chmod
d. exit
ity
10. _ _ _ _ _ _loops are used to repeat a group of statements for as long as a logical
expression is true.
a. if
b. for
m
c. While
d. If else
)A
11. The_ _ _ _ _ _ loop is used to repeat a collection of statements until the logical
expression specified is false.
a. For
b. While
(c
c. If
d. Till
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 231
e
specific sets of command.
a. While
in
b. do while
c. Switch case
nl
d. If else
Exercise
O
1. Define Shell as an Interpreter.
2. What do you mean by shell variables?
3. Define environment variables and keywords.
ty
4. Define Shell Script and its execution methods.
5. Define the term Redirection and Pipes.
si
6. What do you mean by pattern matching and command substitution?
7. What do you mean by setting positional parameters (set command)?
8. Define following items:
i. Assignment Statements
r
ve
ii. Arithmetic Operator
iii. Logical Operator
iv. Relational Operators
ni
v. Read
vi. Echo
U
Learning Activities
m
2. a
3. c
4. a
Notes
e
5. c
6. a
in
7. b
8. a
nl
9. a
10. c
O
11. d
12. c
ty
1. Introduction to Unix and Shell Programming, by M. G. Venkateshmurthy
2. The Linux Command Line: A Complete Introduction, by William E. Shotts Jr.
si
and William E. Shotts, Jr.
3. Linux Command Line and Shell Scripting Bible, by Christine Bresnahan and
Richard BLUM
4. r
Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix, by
ve
Arnold Robbins and Nelson H. F. Beebe
5. Unix Power Tools, Tim O’Reilly
6. Mastering Linux Shell Scripting: A Practical Guide to Linux Command-line,
Bash Scripting, and Shell Programming, 2nd Edition, Andrew Mallett and
ni
Mokhtar Ebrahim
7. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix,
U
G. Sobell
10. Learning the bash Shell, Book by Cameron Newham
11. Linux Command Line and Shell Scripting Bible, Christine Bresnahan and
Richard BLUM
m
12. How Linux Works: What Every Superuser Should Know, by Brian Ward
13. UNIX: The Complete Reference, Second Edition, Rachel Klee, Douglas A.
)A
e
Learning Objectives:
in
At the end of this module, you will be able to understand:
nl
●● Disk Management, Starting and Shutting Down the System
●● File System Mounting and Unmounting
O
●● Monitoring System Usage
●● Ensuring System Security
Introduction
ty
A superuser on a Unix system is a user who has full access to all files and
commands. The superuser’s account has the username root. Many administrative
operations and commands necessitate superuser privileges. You can become a
si
superuser in one of two methods. The first option is to log in as root to the console. The
second method is to use su after logging in with a different username, as demonstrated
below.
$/bin/su –
r
ve
password: #not echoed >
#
ni
The system requests for the superuser’s password when the su command is
submitted. If the user enters the proper password, the system displays a pound sign
(#), indicating that the user has successfully become the superuser. The su command
U
line’s dash character (-) switches the shell environment to that of the superuser. To put
it another way, the home directory is changed to / (the root), and the PATH variable is
reset to include the directories containing administrative commands. The exit command
or the keys can be used to return to the original user’s account.
ity
Even if a user knows the superuser’s password, it is recommended that they only
employ superuser status when absolutely necessary. This is due to the fact that, unlike
many other operating systems, a Unix superuser has complete access to all files and
commands at all times. A single blunder or lapse of judgement on his or her behalf
m
In fact, if one knows the passwords of the other users, one can use the su
command to become any user (not necessarily the superuser). As a result, the su
)A
command is better known as the substitute user command rather than the superuser
command.
e
passwords are granted or revoked, and a basic working environment is provided
via the system-wide profile file, among other things.
in
●● Disk space management: The system administrator monitors how much memory
a certain user is using or requires on a regular basis and decides whether to limit
memory consumption or reallocate memory space by compressing crucial but
nl
infrequently used files for example. For this, commands like du, df, compress and
uncompress, and others are utilised.
O
●● Taking backups and restoring files: Backups of all critical files are taken at regular
intervals, depending on the relevance of the data. Occasionally, a full backup of
the file system is taken. This can be done manually or with the use of special corn-
related instructions and scripts. Cpio, tar, and dump are some of the commands
ty
used for taking backups and restoring necessary files or entire file systems.
●● Responsible for the installation of the software: The system administrator is in
charge of installing new software, as well as updating existing software in the
si
event of a system failure.
●● Responsible for all the local events: The system administrator is in charge of
keeping all users informed about their and the system’s activity.
r
The system administrator wields enormous authority. There are a number of
ve
commands that are only available to him. Also, when used by the system administrator,
several commands function differently. In the following paragraphs, we’ll go through
some of the privileges a system administrator has and some of the commands that
behave differently when used by him.
ni
One of the most significant capabilities that a system administrator has is the
ability to change the properties of any file regardless of its permissions. Another crucial
permission that an administrator has is the ability to use the rm command to remove a
U
file as well as start or stop any process. In other words, the ownership limitation on a file
or a process does not apply to him.
Even if the system administrator does not know the old password, the passwd
ity
command can be used to provide a new password to any user. As a result, a system
administrator can change anyone’s password without knowing it. Naturally, if a
user forgets his or her password, there is no way to recover it. However, even if the
superuser is unaware of the original password, he or she can assign a new one.
m
Another power a system administrator has is the ability to use the date command
to reset the system time. To set the time, use the date command with an eight-character
numeric parameter that indicates the month, day of the month, and time in the format
)A
#date 12031934
A system administrator also has the ability to communicate with all users,
(c
regardless of whether or not the terminals have write access. He can for example, use
the wall command to send an emergency message to all currently logged-in users. A
system administrator can also set a limit on the maximum file size that a user is allowed
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 235
to create (using the ulimit command). He can also grant or prohibit particular users
Notes
e
access to commands like at, batch, and corn.
A system administrator can limit a user’s activity by giving a limited shell (rsh) or by
in
permitting the user to run just a specified script by adding a relevant entry to the.profile
file.
nl
5.1 System Administration
The regular operation of a system is directly dependent on how well the system’s
resources are used and how the varied activities of the various users are monitored
O
and controlled. The system administrator is in charge of the system’s overall health
as well as its use. This chapter begins by defining the privileges and responsibilities
of a system administrator. There’s also a quick look at system security, disc space
management, backup and restore, the implementer’s perspective on file systems, and
ty
mounting and unmounting file systems.
Because Unix is a multiuser system, multiple users will be using it at the same
si
time, completing different types of tasks. Users and their activities must be effectively
regulated or administered from the standpoint of security and system functionality.
A system administrator is in charge of this task. A system administrator’s obligations
(duties) and privileges are numerous. As a result, a superuser is often known as a
system administrator. r
ve
5.1.1 Adding and Removing Users, Changing Password
To create a new user account, we use the useradd command. We can specify the
ni
home directory, group name, shell, and username when creating a new user account.
Syntax useradd -d homedirectory -g groupname -s shell userid
-g groupname It refers to the group name or ID (GID) to which the new user will
belong.
ity
Examples
# useradd -d /home/john -g it -s /bin/sh johny123
This command creates by user ID johny123 who is a member of the group it. The
)A
user’s home directory is named /home/john. By default, the user will utiliseBourne shell.
Each user ID should be distinct. If we try to create a user using an already-existing user
ID, we will receive an error message stating that the user already exists.
Note: The group name should already exist before its use in the earlier command.
(c
This command creates the user mce and the group bank with a UID of 200. The
Notes
e
user’s home directory is /home/mce, and the Korn shell will be used. The -m option
guarantees that the user’s home directory is created if it does not already exist, as well
as transfers a sample,.profile, and.kshrc to it. In the /etc/passwd file, it additionally adds
in
the following line:
mce:x:200:100:Microchip:/home/mce:/bin/ksh
nl
The username is the first field mce. The password encryption is no longer stored in
the second field(x). The UID and ID are stored in the third (200) and fourth (100) fields,
respectively. The fifth field (Microchip) is used to record the user’s information, such as
O
name and address. The home directory (/home/mce) is shown in the sixth field, and the
shell (/bin/ksh) is determined in the last field.
The new user’s password must be set. The command passwd mce makes this
ty
simple. The user mce account is now ready for use.
Except for password encryption, all user information is now saved in /etc/passwd.
/etc/shadow stores the encryption, i.e. the encrypted password. Passwd uses this
si
control file to verify the user’s password’s authenticity.
Note: The last field in /etc/passwd is actually the command that has to be executed
when a user logs in
Remove User:
r
ve
To delete/remove an existing user account, we use the userdel command:
Example
U
$ userdel -r johny777
Here, the -r option is used to remove the user’s home directory as well
ity
Changing password:
It is recommended that login passwords be changed at regular intervals to avoid
the potential of unwanted access to files.
m
Syntax $passwd
)A
We will be requested to provide the old password before supplying the new
password when we run the passwd command (to confirm that only authorised people
are changing the password). Furthermore, the new password should be very distinct
from the old one. It should have at least six characters, two alphabets, one numeric
character, and one special character. We might get the output given in the following
(c
Example:
Notes
e
$passwd
in
Old password: *********
nl
Re-enter new password: **********
We may obtain the following error if the new password and the old password are
O
not substantially distinct from each other:
If the two passwords supplied in New password and Re-enter new password are
ty
not the same, the following error will occur:
si
Try again
If the two passwords entered in New password and Re-enter new password
are identical, the user’s password will be changed, and we will receive the following
confirmation message: r
ve
Password updated successfully.
There will always be a need for extra disc space, no matter how much one
possesses. One can attribute such a need to a variety of factors, including the ones
listed below.
U
Many files that are neither important nor used may just be present and need to be
erased or saved as backups.
A file system’s or a user’s allowed space may be underutilised, and someone else
ity
may require greater space. In this case, the space must be re-appropriated.
The space is available, but owing to the lack of inodes, it is not feasible to create
new files.
It is vital to know how much space is available or how much space is actually being
)A
used before the space is re-appropriated. Unix programmes such as df and du can be
used to find out how much free space is available and how much space is being used.
The next paragraphs provide a quick overview of these commands.
The df Command
(c
The df (disc free) programme is used to determine how much disc space a file
system has available. If no file system is supplied, this command displays the total
amount of free space on all file systems. The free space available on all file systems is
Notes
e
displayed in the example below.
sdf
in
/ (/dev/dsk/c0d0s2), 14534 blocks 2965 inodes
nl
$
This system has two file systems, as shown in the output above. It informs you of
the number of free blocks and inodes available. Each block is typically 512 bytes long.
O
On other systems, however, the block size may be 1024 bytes.
As illustrated in the following example, using the –t option returns information about
the total amount of blocks and inodes in each file system.
ty
The du Command r si
ve
The du (disk usage) programme is used to determine how much disc space each
subdirectory and file in a current directory have consumed. This command generates
reports in terms of the blocks used by default.
ni
When the du command is used with the current working directory set to /usr/mgv,
the following report will appear.
$du
U
4 ,/notice
12 ,/src
ity
20 ,
In the preceding report, the dot (.) character denotes the current or current working
directory. The data also reveals that./notice takes up 4 blocks,./src takes up 12 blocks,
m
This command provides a large number of options to choose from. The –a (all),
–b (bytes), and –s (summary) options are the most common. The use of the options is
)A
Notes
e
in
nl
O
ty
One can see from the examples above that
●● The –a option shows the total size of all the directories in blocks, as well as
si
the space used by each of the files within the directory.
●● The –b option shows the space used by each file in bytes, and
●● the –s option just shows the total size of all the directories in blocks.
r
ve
File Compression
Compressing files can be done with a variety of commands. Compressing files
is one of the ways to save space on your hard drive. Compressed files can be sent
over vast distances for little money. One or more compression utilities are included with
ni
every Unix system. The compressed files can also be decompressed using commands.
$ compress sample
The extension.z will be applied to the names of all files compressed with this
m
$ uncompresssample.z
The extension.z will be applied to the names of all files compressed with this
command. As a result, sample.z is the compressed version of sample. The uncompress
(c
$ gzipsales.dbf
Notes
e
$
in
Also available in Linux is the DOS compression programme zip. This command’s
main benefit is that it merges numerous files into a single file. The zcat command can
nl
be used to inspect compressed files created with the compress or gzip commands.
For.z and.gz files, zcat is the counterpart of cat. When this command is performed, it
uncompresses the compressed file, shows its contents, and then compresses it again.
In other words, the compressed file visible on the screen is still compressed on the hard
O
disc.
The following two facts can be deduced from the discussion in this section.
ty
●● The du and df commands provide information about the amount of memory
space available and used.
●● With compression commands, memory space is actually saved directly on the
si
storage location.
Disk space management can be defined as the process of reducing storage space
by using any technology. There are several sophisticated utilities in Unix that may be
r
used to move files in and out of a system.
ve
Starting and Shutting Down the System
Bootstrapping is the process of starting a computer. The computer does a self-
test and loads a boot programme from the boot device into memory during the
bootstrapping phase. The boot software instals the kernel and provides control to it,
ni
which then configures the devices, verifies hardware status, identifies new hardware,
initialises existing devices, and starts the system processes. The kernel locates the
root, swaps and dumps devices, and performs a variety of duties such as process
U
also does memory page swapping, which entails shifting memory pages in and out.
When the system is running, the init process runs as process 1 and is constantly in
the background. It switches to single-user mode, which is essentially a minimal system
setup.
m
Single-user Mode
Administrative and maintenance tasks are best performed in single-user mode. In
)A
order to start single-user mode, init forks to establish a new process, which then runs
the default shell, Bourne shell, as root user. A # prompt appears, indicating that we
have root access and may perform administrative activities. We can change the date,
verify the file systems, and so on. There are no daemons running in single-user mode,
and the system is not connected to the network. Other file systems can be mounted
(c
manually as needed, with the exception of the root partition, which is automatically
mounted.
Multi-user Mode
Notes
e
Init is the parent of user login shells and the progenitor of all future Unix processes.
It gets the system ready for users and checks the integrity of the local file systems,
in
starting with the root and other critical ones.
nl
●● Examining the file systems’ integrity.
●● Local discs are mounted.
●● Starting daemons for printing, mail, logging, and cron, among other things.
O
●● The system initialization scripts specify and carry out certain operations,
such as enabling user logins. We can say that the Unix booting procedure
is complete and the machine is in multi-user mode after these activities are
completed. As a result, users will be able to access the system.
ty
Shutting Down the System
The way UNIX and Linux machines were shut down used to be quite sensitive.
si
Modern systems are less sensitive, especially when using a strong filesystem, but it’s
always a good idea to shut down the machine gracefully if possible. Improper shutdown
can cause a wide range of issues, from little annoyances to catastrophic disasters.
r
Databases that aren’t properly terminated are prone to corruption and integrity
problems.
ve
Rebooting the operating system is an appropriate initial course of treatment for
many problems with consumer-oriented operating systems. Because UNIX problems
are more nuanced and sophisticated, restarting is only beneficial in a limited number of
ni
circumstances.
You should reboot whenever you change a startup script or make significant
system modifications only to make sure the system boots up successfully. You’re
U
unlikely to remember the details of your most recent adjustments if you don’t uncover a
problem for several weeks.
The shutdown command is the safest, most courteous, and thorough way to
bring the system to a halt, reboot, or return to single-user mode. Its method is a little
antiquated on desktop machines because it dates back to the days of time-sharing
systems.
m
Regrettably, nearly every vendor has chosen to meddle with the shutdown
arguments. We go over the command in general, then break down the syntax and
arguments for each platform.
)A
You can tell shutdown to hold off on shutting down the system for a bit. Shutdown
provides notifications to logged-in users at progressively shorter intervals during the
waiting period, notifying them of the imminent outage. By default, the warnings merely
state that the system is being shut down and the amount of time until the event;
(c
however, you can add your own short message. Your message should explain why the
system is being shut down and give an estimate of when users will be able to log in
again (for example, “back at 11:00 a.m.”). When a shutdown is approaching, users will
Notes
e
not be able to log in, but they will see your message if you specified one.
Most shutdown programmes let you choose whether the computer should halt, go
in
into single-user mode, or reboot. You can sometimes indicate whether or not you wish
to fsck the drives following a reboot. A thorough fsck on a contemporary system with a
large disc can take a long time; you can usually bypass the tests if you shut down the
nl
system cleanly. (When filesystems are properly unmounted, most systems skip the fsck
checks automatically.)
On our test systems, the shutdown command-line options are listed in the table
O
below.
ty
si
Table: The many faces of shutdown
r
Red Hat and SUSE, but not Ubuntu
ve
A Linux shutdown command for example, that warns users of scheduled
maintenance and shuts down the system at 9:30 a.m. would look like this:
A relative shutdown time can also be specified. The following command for
example, shuts down the system 15 minutes after it is run:
U
The halt command performs the necessary tasks to shut down the system.
Shutdown -h invokes it, however it can also be used on its own. halt logs the shutdown,
kills non-essential programmes, runs the sync system call (which is similar to the sync
command), waits for filesystem writes to finish, and then halts the kernel.
m
The sync call is prevented by using pause -n. After fsck has repaired the root
partition, it uses it. If fsck did not use -n, the kernel may replace fsck’s repairs with old
superblock versions cached in memory.
)A
While reboot is nearly identical to halt, it causes the machine to reboot rather than
halt. Shutdown -r causes a reboot.
A file system is a tree-like hierarchy of directories with / (forward slash) as the root
and /usr, /tmp, /etc, /bin, and other directories as its branches. Each directory has its
own set of subdirectories. To access a file or file system on a storage device, it must be
Notes
e
mounted on the available file system, or mounted on a mount point. The mount point is
normally an empty directory to which the storage device’s extra file system is mounted.
The additional file system’s root directory will be the empty directory.
in
Any device’s file system, whether it’s a partition of a hard disc drive, a CDROM,
a pen drive, or something else, can be mounted on a mount point to gain access to
nl
it. When we mount a CDROM to a directory, the CDROM’s file system appears in that
directory.
In practise, more than one file system will reside on distinct partitions on the same
O
disc or on multiple discs. At startup time, all of these file systems combine to form a
single file system. The primary file system is then created from the root file system.
Mounting is the process of attaching a file system to another file system, either
automatically or manually. Unmounting is the process of removing a file system from
ty
another file system. Attaching and detaching file systems are done with the mount and
umount commands, respectively.
r si
ve
ni
empty directory point of the parent file system, the root directory of the new file system
(the file system to be attached) is attached. The mount point is the location where the
attachment takes place. On the primary file system, Unix provides a default mount point
named /mnt. In the root (/) directory, there is a directory called mnt.
m
As parameters, this command requires both the device name and the mount point.
As an example, as shown below, the directory /dev/fd0 is attached to the existing file
system at the mount point /mnt.
)A
The device name is /dev/fd0, and the mount point is /mnt in the example above.
You can mount a file system to a directory on the root file system on some systems,
even if the directory isn’t empty. If such an attachment is created, any files that were
(c
present at the mounting point will be inaccessible for the duration of the attachment.
Without any parameters, the mount command displays information about file
Notes
e
systems that have previously been mounted on the system.
in
nl
O
ty
Figure:Unified file system after the mount
si
The command umount Unmounting is the process of separating a file system from
a unified file system. Unmounting is done with the umount command. This command
only takes one argument.
Syntax
r
ve
umount {file system to unmounted} {mount directory} {options}
Forcibly unmounting a file system is not recommended since the data on the file
system may be corrupted.
Examples
ity
The first example removes the CDROM drive from the /mnt mount directory. In
the same way, the second example unmounts the hda3 hard disc partition from the /
m
framework mount directory. We can set up our Unix system to mount storage devices
automatically when they are inserted. This, however, may result in security issues and
should not be used in networked systems.
)A
e
A key part of system management is staying on top of what’s going on with your
machine. On a daily basis, the two most crucial items to keep an eye on are:
in
(1) disk space and
(2) load on the processor(s).
nl
When you run out of disc space, many programmes will shut down since they won’t
be able to create temporary data. Allow rogue software to eat up too much processing
time, and everything else suffers.
O
There is never a time in the life of a UNIX administrator when a system does
not need to be watched. At some point and in some way, every system that an
administrator touches must be monitored. It could be for a variety of reasons, including
security, performance monitoring, troubleshooting, disc usage, or hardware issues. The
ty
system must be monitored for any purpose.
One of the most typical reasons is to collect system utilisation data in order to
develop management reports and pretty graphs. The data gathered should also provide
si
a solid indicator of the system’s future path. On most systems, a pattern emerges after
a period of time. The requirement for additional memory, faster CPUs, and more disc
space grows when programmes and other pieces of software are upgraded and the
environment expands with users and other devices. r
ve
In a perfect world, you could go to management and request more disc space or
memory, and they would just say, “OK.” However, in the real world, every corporation
adheres to a budget. If it’s not in the budget, it won’t be bought. Which is useful only
when a vendor is pressuring you to buy his wares. Tell him straight up that no money
ni
has been set aside for his product. He usually doesn’t call again until the first day of the
next fiscal year.
variety of ways. A rendering system can have 18 CPUs, be 98 percent occupied all of
the time, and yet be normal. Many systems with low load averages may see a gradual
increase or even moments of sudden spikes. During typical business hours, load
ity
averages might substantially increase or decrease. The drops become fewer with time,
and the CPUs become increasingly overworked.
When you’re keeping an eye on your memory, you can notice a few things.
Memory grows over time as applications are upgraded, new services are added, and
m
user productivity rises. The system slows down as memory grows and more information
is swapped out. Another scenario is that a new user utilises the system for a different
purpose or that a new application is installed, necessitating the installation of new
memory.
)A
Keep an eye on the disc space as time passes. It frequently performs an intriguing
action. When you need extra disc space, you can notice quite quickly. Disk use
fluctuates at first, but it always climbs far more than it ever reduces. Most users are
unconcerned about the percentage of available disc space; they appear to believe
(c
there is an infinite quantity until it runs out. When it does run out and the drives are full,
customers typically only delete 5–10% of the space because everything is important to
them. When they refuse to accept other options (archiving, recordable CDs, etc.), all
Amity Directorate of Distance & Online Education
246 Unix Operating System and Shell Programming
you can do is sit back and watch the consumers battle for disc space in the remaining
Notes
e
10%. By this time, it’s too late, and users begin to vent their frustrations on you,
demanding that you perform miracles and create more space where none exists.
in
Monitoring disk space
A key system-administration function is anticipating the need to add disc space
nl
before you run out.
For the entire system or any directory in the file system, you may see both
available and utilised space.
O
To obtain a summary of the complete system’s disc utilisation, go to:
ty
bdf -[k] [filesystem]
Depending on the flavour you’re using, the df command has several variations. The
BSD form of df is used to display the available space in the examples that follow:
si
% df -kl
/dev/dsk/c0t1d0s0 r
1203886 896255 187251 83% /
ve
/dev/dsk/c0t1d0s6 634354 194243 376681 34% /usr
many informational echo statements. If you want more information while the server is
booting, you can add more echo statements to the various boot scripts, /etc/rc.boot, /
etc/rc.local, and /etc/rc. From beginning to end, these files are well-documented. Simply
change the comments to statements, and the console will be populated with information
ity
# vi /etc/rc.local
#
m
#
)A
fi
(c
This is a section in /etc/rc.local that can be changed to explain what happens next.
To better understand what is happening during the boot process, change the remark to
an echo statement that is displayed when the machine boots:
Amity Directorate of Distance & Online Education
Unix Operating System and Shell Programming 247
#
Notes
e
echo “Trying to add a default route…”
in
if [ ! -f /sbin/route -a -f /etc/defaultrouter ]; then
nl
fi
O
Every type of monitoring requires a starting point. Start capturing snapshots of your
system when it has been developed or rebuilt and is ready to go into production. If you
wait until the users enter the system, you’ll never have a solid set of numbers to work
ty
with when determining whether the system is overburdened. There are no two systems
that have the same set of numbers. When several systems are compared using the
upload command, this can be seen. Each one yields a different number.
si
Platform: HP—K460, 500MB memory, 2 CPUs, multiple databases
applications, 62 users
BASE: 7:35am up 1 day, 2:52, 1 user, r
ve
load average: 0.53, 0.29, 0.14
e
Platform: Sun Sparc 20, 192MB memory, 1 CPU, Web server, 15 users
in
BASE: 7:27am up 1 day(s), 14:59, 1 users,
nl
PRODUCTION: 15:20pm up 4 day(s), 15:22, 15 users,
These are values obtained from various platforms when the systems were first
O
established, when there were no users and everything was pleasant and quiet.
The systems are in full production, but not overburdened, in the second set of load
averages. You can see how drastically the values differ. Because many various
ty
combinations of apps and system configurations can be put up and executed, your
values will differ as well.
When there are users on the system, the values increase. Keep an eye on these
si
numbers and see how they relate to disc I/O and RAM utilisation (with vmstat and sar
commands). These are the numbers you need to pay attention to and track.
The uptime load average values should grow in magnitude if and when the system
r
peaks and begins to slow down. Keep in mind that, as shown in the preceding cases,
ve
an overloaded system with a load average of 4 on one system may be underused
with the same value of 4 on another system. When the system peaks and you have a
baseline to compare it to, you’ll be able to tell when it’s on its way to being overloaded
or underused.
ni
Syntax:
When seeing the last x number of lines in a huge file, always use tail. When you
ity
only have 24 lines to deal with on a VT100 type terminal, a nonscrolling X terminal, or a
window session, it comes in useful.
e
SU 10/12 20:08 - ttyq3 baday-root-bad
in
Cut the Log in Half
It is sometimes necessary to reduce the size of log files. This is one method for
nl
halving the log file.
Syntax:
O
bc
tail
-number
ty
wc [-l] file
si
% wc -l SYSLOG
1234567
617283
tail can give the last portion of the file in one of two ways: by counting the distance
ni
in lines down from the top of the file or up from the bottom.
Mail a Process
U
One of the simplest types of notice when processes or tasks are being watched is
email.
Syntax:
ity
You can use the mail command to send the output of a command or script. The
m
# mail admin@rocket.ugu.com
~r /tmp/sulog
# rm /tmp/sulog
The following command can be piped into mail to replace the entire process:
Notes
e
# tail -20 /var/adm/sulog | mail -s “Root Access”
admin@testmail.com
in
The last 20 lines of the file, which record who signed in as superuser and mail the
information to the system administrator, are the outcome.
nl
Find the Disk Hog
When disc space is at a premium, it’s critical to rapidly identify what’s hogging the
O
most space.
Syntax:
du -s [dirname|filename]
ty
sort -rn
The du command can be used to get a disc usage report in many different sections
si
of a system. This command should be used in concert with sort to get a comprehensive
list of offenders in reverse numerical order. The highest-ranking users are displayed
first.
# du -s * | sort -rn r
ve
Log files expand over time, users don’t clear up their files, spooling folders load up,
and system files and processes go crazy and fill up a disc. On a system disc, there are
a few particularly useful spots to keep an eye on.
●● Spooling directories—/var/spool
ni
partitions:
●● Temporary areas.
●● Spooling areas that are linked from /var/spool.
●● Fully compiled source directories—/local/src.
m
To keep track of problems that might be lurking in huge log files, use grep with diff.
Syntax:
grep [pattern]
(c
This simple script watches a log file for specified problems or messages and sends
Notes
e
an email to a predefined address when they occur.
#! /bin/sh
in
touch /tmp/sys.old
while [ 1 ]
nl
do
O
FOUND=`diff /usr/tmp/sys.new /tmp/sys.old`
if [ -n “$FOUND” ]; then
ty
mv /tmp/sys.new /tmp/sys.old
else
si
sleep 10
fi
done r
ve
Monitoring Core Files
It’s critical to keep an eye out for core files if you don’t want to squander disc
space. Get rid of them if the user doesn’t require them.
ni
Syntax
file] expression
There are several choices available when using the locate command. One can
prevent the search command from stretching across other filesystems, including those
ity
mounted through NFS. Check your man pages to find which one your flavour is using.
The -x, -xdev, -local, and -mount parameters are allowed for this function.
By using standard out, find all the core files on the local root filesystem that haven’t
been read in three days and show them.
m
If it is decided that removing any and all core files is safe in your environment, find
)A
Find looks locally on the system for the file named core in this version of the
programme. If one is discovered, it is saved in a buffer, and the rm command is then
(c
run on the file in the buffer. This will go on until the find command has finished its
search. This can be added to the crontab and executed nightly. The crontab entry would
be as follows:
Amity Directorate of Distance & Online Education
252 Unix Operating System and Shell Programming
e
002 002
Core files can be stored in a variety of locations across the system. Each location
in
where the core files are stored can be examined individually to determine whether the
file should be destroyed immediately. When memory is spilled to a core file, it might be
placed in the user’s home directory, the application’s directory, or the root directory.
nl
User Home Directory:Some programmes bind themselves to the directory of the
user who runs the programme. When the core file is dumped, it is saved in the user’s
home directory. Before removing the core file, double-check that the user no longer
O
needs it. She could be having issues with the application and dealing with a vendor
who requires the core file to fix the issue. In truth, 9 out of 10 users will never require
the core file, and half of them will have no idea what it is or how it was developed. They
ty
believe it is a core file that the system uses as part of their account, therefore they leave
it in their personal folders.
Application Directory:When daemons crash, the application can trap the signal
si
and write the core file to a preset directory established by an environment variable
the application is aware of. Vendors occasionally do this so that they can notify users
where they may find the essential maintenance and support files to assist them solve
r
problems. Users of the programme may be collaborating with the vendor to remedy
any issues they are experiencing. Before deleting these files, you might wish to double-
ve
check with them.
Root Directory:The essential files that wind up in the root directory should either
be moved and evaluated, or they should be eliminated entirely. When an application
ni
running as root exits with a QUIT signal, or when the operating system crashes hard
due to a memory parity problem or other hardware failures on specific BSD systems,
core files are saved here.
U
You might wish to use the strings command to extract any helpful information from
these core files because they are binary files. Because these can be quite large, you
should pipe the command to a larger number of them.
ity
multiuser system, such as Unix. The two most significant factors that are thought to be
necessary for system security are
files.
Unix has a variety of security features built in. The password security method is
Notes
e
the most basic, oldest, and most extensively used security mechanism today. Another
option is to limit individual users’ capabilities by forcing them to use a modified version
of the normal shell. Important and highly sensitive individual files can be made secure
in
using a command known as the crypt command. File permissions, user and group IDs,
and other security features are also available. Taking backups of all critical files and
storing them in a secure location is sometimes regarded a security measure, despite
nl
the fact that it appears to be non-technical. In the next sections, we’ll go through a few
of these techniques.
O
Password Security: /etc/passwd and /etc/shadow Files
Passwords are just keys that can be used to log into a system. Even now, the
concept of guaranteeing security by issuing unique passwords to each individual user
ty
is one of the oldest and most frequently accepted procedures. No one is allowed to
use a system without knowing and recording all of his or her personal information.
These days, two unique files called /etc/passwd and /etc/shadow are used to maintain
password security. The next paragraphs provide an overview of these two files.
si
/etc/passwd File during the creation of a user account, the whole information
about the user is gathered and recorded in a separate file called the /etc/passwd file.
r
Any user can view this file, but only the superuser can edit it. On this file, each user will
have a line dedicated to him or her. These lines are made up of seven fields separated
ve
by colons on either side (:). The line after that shows the basic format of each line,
followed by an explanation of each field.
password: The password in encrypted form is stored in this field. An asterisk (*)
in this field indicates that this user or login name is not allowed to log in to the system.
The presence of an x in this column indicates that the encrypted password is stored in a
different file known as the shadow file, which will be located in the same directory as the
ity
etc directory.
GID: The user’s default group ID is stored in this field. A user can belong to many
groups.
m
comment: This field is used to store descriptive comments (such as the user’s full
name, address, and so on). This information is used by several commands, such as
)A
home: The absolute pathname to the user’s home directory is stored in this field.
shell: The information about the user’s shell or command interpreter is stored in
this field. When a user logs in, he or she is automatically logged into this shell.
(c
Two typical examples are given below, each with a brief explanation.
mgv is the user in this case. The x in the second field indicates that the password
Notes
e
is stored in the /etc/shadow file, which is a different file. The GID is 254 and the UID
is 110. The fifth field contains further details on the user mgv. As previously stated,
commands like finger make advantage of this information. Mgv’s shell is /usr/bin/sh,
in
and his login or home directory is /home/mgv.
2. agk:*:120:254:A geethakiran:/home/agk:/usr/bin/rsh
nl
agk is the user in this case. The * in the second field denotes that anyone with this
username will be unable to access the system. If necessary, the system administrator
can utilise this feature to prevent particular usernames from being used. Clearly, the
O
UID is 120, the GID is 254, the fullname is A geethakiran, the home directory is /home/
agk, and the users shell is rsh.
The file /etc/shadow As previously stated, the second field of a /etc/ passwd
ty
file stores an encrypted version of a user’s password. Previous experience has
demonstrated that any user may readily obtain these encrypted passwords (as /etc/
passwd is publicly accessible) and decode them with moderate effort. As a result,
storing a password in the /etc/passwd file, even in encrypted form, is not a secure
si
practise. The encrypted password is now commonly stored in a separate file called /
etc/shadow, which can only be read and updated by the superuser. The /etc/shadow file
will also contain some additional password information. It for example, keeps track of
r
password expiration dates. Every user has a line in this file that corresponds to him or
ve
her, just like in the /etc/passwd file. As demonstrated below, a line in the /etc/shadow file
comprises eight fields. These fields are also separated by a colon (:).
user:coded_passwd:last_change:min_days:max_days:warn_days:disable_in:e
xpire_date
ni
A standard shell gives all users the ability to navigate the file system, execute as
U
many commands as they like, change environment variables, and so on. If a user’s
privileges are to be restricted for some reason, the user is given a version of the regular
shell with limited or restricted capabilities. The restricted shell (rsh) is a sort of shell that
is accessible with practically all Unix versions. rsh is an executable application that can
ity
be found in the /bin directory, just like sh. The rsh grants a user the bare minimum of
privileges on the system. The constraints that anrsh user is subjected to are as follows:
●● Only the user’s HOME directory is accessible to him or her. To put it another
way, the user is unable to change the directory to any other directory on the
m
system.
●● The PATH variable’s value cannot be changed by the user. As a result, the
user can only access files that are located in the system administrator’s
)A
specified path. If the PATH variable is set to /bin, the user will be unable to run
commands whose executables are located in /usr/bin or /etc.
●● The redirection operators > and >> cannot be used to create new files or
append to existing ones.
(c
Users that are either beginners or new to the organisation are granted rsh. The rsh
command is used to reduce a user’s privileges for security concerns. A user’s rights are
truly limited by using the rsh shell as his or her working shell. This is accomplished by
adding anrsh entry to the last field of the corresponding line in the user’s /etc/passwd
Notes
e
file during account creation, as illustrated below.
agk:x:120:254:A geethakiran:/home/agk:/usr/bin/rsh
in
When the above line is added to the user agk’s /etc/passwd file, the restricted shell
becomes the user’s primary shell as soon as he or she logs in.
nl
The crypt Command: File Encryption
This command is used to keep particular files safe from everyone, even the
superuser. This command reversibly modifies each character in the target file, allowing
O
the original file to be recovered at a later time. Simple substitution is used in the
encoding process. For example, in a file, the letter A is converted to ~, the letter B to
^, and so on. Actually, this command employs a key to scramble its standard input and
send it to the standard output as unreadable text.
ty
The crypt command can be used to encrypt and decrypt data. In fact, to decode
an encrypted file, one must utilise the same key that was used to encrypt it in the first
si
place.
As previously stated, this command encrypts and decrypts data using a key, similar
to how a password is used. This crucial key might be provided interactively when the
r
crypt command asks for it, or it could be used immediately on the command line. Some
ve
instances are provided below. The following command encrypts a file named sample
with the key abc and saves the encrypted version of the input file in a file called sample.
crypt.
After encrypting a file as shown above, the original file must be removed using
a programme like rm. The crypt command is used to decrypt the encrypted file, as
indicated in the following command line.
U
The way of explicitly providing the key in the command line has a disadvantage.
Although this method is not often utilised, reviewing the history of instructions used can
ity
reveal the key. As seen in the following example, the crypt command can also be used
without explicitly providing the key on the command line. When used in this manner, the
crypt prompts the user for the key. At this time, the key input will not be repeated.
key
$
)A
Summary
●● A superuser on a Unix system is a user who has full access to all files and
commands. The superuser’s account has the username root. Many administrative
(c
e
permissions, is crucial. An administrator can also use the rm command to delete
files and start or stop processes. That is, he is exempt from file or process
ownership restrictions.
in
●● The df (disc free) programme is used to determine how much disc space a file
system has available. If no file system is supplied, this command displays the total
nl
amount of free space on all file systems.
●● The du (disk usage) programme is used to determine how much disc space
each subdirectory and file in a current directory have consumed. This command
O
generates reports in terms of the blocks used by default.
●● The following two facts can be deduced from the discussion in this section.
a) The du and df commands provide information about the amount of memory
ty
space available and used.
b) With compression commands, memory space is actually saved directly on the
storage location.
si
●● Bootstrapping is how a computer starts. On startup, the computer does a self-
test and loads a boot programme from the hard drive. Initiates, initialises, and
initiates the system processes after installing the kernel and providing control to it.
r
The kernel also schedules processes, manages physical and virtual memory, and
ve
performs hardware diagnostics.
●● UNIX and Linux shutdown used to be very sensitive. Modern systems are less
susceptible, especially with a sturdy filesystem, but it’s still best to gracefully
exit. Incorrect shutdown can cause little annoyances to major catastrophes.
ni
Glossary
●● Remove User: To delete/remove an existing user account, we use the userdel
)A
command
●● Changing password: It is recommended that login passwords be changed at
regular intervals to avoid the potential of unwanted access to files. The command
for changing the password is passwd.
(c
●● The df Command: The df (disc free) programme is used to determine how much
disc space a file system has available
e
much disc space each subdirectory and file in a current directory have consumed.
●● rsh: The Restricted Shell
in
●● The crypt Command: This command is used to keep particular files safe from
everyone, even the superuser.
●● Bootstrapping: Bootstrapping is the process of starting a computer
nl
●● -d homedirectory: It refers to the home directory for the new account.
●● -g groupname: It refers to the group name or ID (GID) to which the new user will
O
belong.
●● -s shell: It refers to the default shell for the new account.
●● Userid: It refers to the unique user ID (UID) of the new user.
ty
●● Umount: The command umount Unmounting is the process of separating a file
system from a unified file system.
si
Check Your Understanding
1. _ _ _ _ _ is often known as a system administrator.
a. Naïve user
b. New user
r
ve
c. Old user
d. superuser
2. The _ _ _ _ _ _command is used to create a new user account.
ni
a. useradd
b. exit
U
c. del
d. none of the above
3. To delete/remove an existing user account, we use the _ _ _ _ _command:
ity
a. useradd
b. userdel
c. both a and b
m
d. passwd
4. The command for changing the password is_ _ _ _ _ _ _.
)A
a. userdel
b. useradd
c. remove
d. passwd
(c
a. Bootstrapping
Notes
e
b. Shut down
c. Reboot
in
d. Sleep mode
6. The_ _ _ _ _ __ command can be used to encrypt and decrypt data
nl
a. Rsh
b. Crypt
O
c. Passwd
d. Umount
7. The command _ _ _ _ _ _ is used for separating a file system from a unified file
ty
system.
a. Umount
b. Rsh
si
c. Crypt
d. None of the above
8. r
_ _ _ _ It forcibly unmounts the file system.
ve
a. –f
b. –a
c. –u
ni
d. –m
9. Which command is used by the user to change their login password in Unix?
U
a. cp
b. man
c. reset
ity
d. passwd
10. Which command is used to change the permissions of a file?
a. chmod
m
b. ch
c. chown
)A
d. chgrp
Exercise
1. What do you mean by system administrator?
2. How to add and remove users?
(c
e
6. How to mount and unmount the file system?
7. How to Monitor System Usage?
in
8. How to Ensure System Security?
Learning Activities
nl
1. What is the role of system administrator according to you?
2. How you do disk management in linux and windows?
O
Check Your Understanding - Answers
1. d
ty
2. a
3. b
4. d
si
5. a
6. b
7. a r
ve
8. a
9. d
10. a
ni
Han_5, Evi Nemeth, Garth Snyder, Ben Whaley, Trent Hein, Dan Mackin
2. Linux Administration: The Linux Operating System, by Jason Cannon
3. Linux Administration Handbook, Evi Nemeth, Garth Snyder, and Trent Hein
ity
7. Linux Command Line and Shell Scripting Bible, by Christine Bresnahan and
Richard BLUM
8. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix, by
)A
Mokhtar Ebrahim
11. Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix,
Notes
e
Arnold Robbins and Nelson H. F. Beebe
12. The Linux Command Line, 2nd Edition: A Complete Introduction, William Shotts
in
13. A Practical Guide to Linux Commands, Editors, and Shell Programming, Mark
G. Sobell
14. Learning the bash Shell, Book by Cameron Newham
nl
15. Linux Command Line and Shell Scripting Bible, Christine Bresnahan and
Richard BLUM
O
16. How Linux Works: What Every Superuser Should Know, by Brian Ward
17. UNIX: The Complete Reference, Second Edition, Rachel Klee, Douglas A.
Host, Richard R. Rosinski, Kenneth H. Rosen
ty
r si
ve
ni
U
ity
m
)A
(c