Beruflich Dokumente
Kultur Dokumente
A
n operating system (OS) is a collection of programs used by a computer to manage its own
operations and resources. To control the hardware and allow applications to do something useful
with it, the computer loads a master program at startup (boot) time called the operating system.
A typical operating system gives users a way to create and manage files as well as run application
programs. Operating systems perform basic tasks, such as recognizing input from the keyboard, sending
output to the display screen, keeping track of files and directories on the disk, and controlling peripheral
devices such as disk drives and printers.
For large systems, the operating system makes sure that different programs and users running at the
same time do not interfere with each other. The operating system is also responsible for security, ensuring
that unauthorized users do not access the system.
Operating systems can be classified as follows:
l multi-user : Allows two or more users to run programs at the same time. Some operating systems
permit hundreds or even thousands of concurrent users.
l real time: Responds to input instantly. General-purpose operating systems, such as DOS and
Unix, are not real-time.
Due to the popularity of personal computers, there are many versions of Unix available for Intel
platforms. Choosing from the versions and trying to find software for the version chosen can be a tricky
business because the Unix industry has not settled on a complete binary standard for the Intel platform.
There are two basic categories of UNIX systems on Intel hardware, the SVR4-based systems and the
older, more established SVR3.2 systems. SVR4 vendors include NCR, IBM, Sequent, SunSoft (which
sells Solaris for Intel), and Novell (which sells UnixWare). The Santa Cruz Operation (SCO) is a major
vendor for the SVR3.2 system. Efforts are being made to clean up the standards to gain full “shrink-wrap
portability” between their versions of UNIX. This will make UNIX-on-Intel applications available, shrink-
wrapped for any version of UNIX, similar to the availability of applications for MS-DOS or Microsoft
Windows. SCO UNIX currently has a much larger base of available applications and is working to
achieve binary compatibility with UnixWare.
Several versions of Unix and Unix-like systems have been made that are free or very cheap and
include source code. These versions are useful to the modern-day hobbyist, who can now run a Unix
system at home for little investment and an opportunity to experiment with the operating system or make
changes to suit particular needs.
A popular source version of Unix is Linux. Linux was designed from the ground up by Linus Torvalds
to be a free replacement for UNIX, and it aims for POSIX compliance. Efforts are on to make Linux
reliably run both SVR3.2 and SVR4 binaries.
AT&T was not allowed to market computer systems and Bell Labs. could not sell Unix. Nevertheless,
it became more and more popular through internal use at AT&T and licenses were given to universities
for educational use. By 1977 commercial licenses for Unix were granted, and Unix systems were sold for
office automation. Later versions developed at AT&T included System III and several releases of System
V. The two most recent releases of System V, Release 3 (SVR3.2) and Release 4 (SVR4; the most
recent version of SVR4 is SVR4.2) remain popular for computers ranging from PCs to mainframes. All
versions of UNIX based on the AT&T work require a license from the present owner, UNIX System
Laboratories.
While AT&T through its research group, developed Unix, the universities that had acquired educational
licenses were coming out with their own releases. . Most notably, the Computer Science Research Group
at the University of California at Berkeley (UCB) developed a series of releases known as the Berkeley
Software Distribution, or BSD.
Because of the multiple versions of UNIX and frequent cross-pollination between variants, many
features have diverged in the different versions of UNIX. With the increasing popularity of UNIX in the
commercial and government sector there was a need to standardize the features of UNIX so that a user
or developer using UNIX could depend on those features.
The Institute of Electrical and Electronic Engineers created a series of standards committees to create
standards for “An Industry-Recognized Operating Systems Interface Standard based on the UNIX
Operating System.” Of these, the standards set up by two of the committees are important for the
general user and developer. The POSIX.1 committee standardizes the C library interface used to write
programs for UNIX and the POSIX.2 committee standardizes the commands that are available for the
general user.
l account names have only lower case letters and numbers. uppercase letters and punctuation’s
should be avoided
The system administrator also gives the account an initial password, which can be later changed by the
4 Chapter 1 - Introduction to UNIX and its Background
user. To start a unix session, the user has to login using the account name. The account name is also
referred to as the login name or login. When a user logs into a system, the password has to entered to
prevent unauthorised access.
The system maintains a password file that contains one entry for each user (login, ID, encrypted
password, home directory, …). When a user logs in, password is encrypted and is compared with the
encrypted password in the password file
A login prompt on a Unix terminal indicates that the terminal is ready for a user to login. The user
enters a login name and presses [enter]. The system prompts the user to enter the password. The password
entered is not visible. This is a security feature in Unix to ensure that the password remains known only
to the user. If the login name does not exist or the password entered is incorrect then the system displays
a ‘login incorrect’ message. When both the login name and password are correct, the system displays the
Unix prompt ($) and the cursor waits beside the prompt for the user to enter a command.
login: username
Password:
The part of UNIX that manages the hardware and the executing processes is called the kernel. The
kernel is a collection of programs written in C which directly communicate with the hardware. Application
programs (user programs) communicate with the hardware by using the services of the kernel. Along
with the memory management, the kernel also schedules processes and decides their priorities.
In the Unix system, each hardware device is viewed as a file and is called a device file. This allows
the same simple method of reading and writing files to be used to access each hardware device. The file
system manages read and write access to user data and to devices, such as printers, attached to the
system. It implements security controls to protect the safety and privacy of information. In executing
processes the kernel allocates resources, including use of the CPU and mediates accesses to the hardware.
The user commands are translated into action by the shell which acts as an interpreter. The shell forms
the outer part of the operating system and forms the interface between the user and the kernel. For each
user logged in, there is shell in action. When a command is given by the user, it is examined by the shell
and communicated to the kernel for execution. The functions used to communicate with the kernel are
BSIT 43 UNIX and Shell Programming 5
called system calls and are built into the kernel. The system calls are in all the flavors of UNIX are the
same. Hence, software developed on one UNIX system can be easily run on another UNIX system.
user
user user
Shell
Kernel
ps
Application Software
Compilers
sort
Hardware
grep
cat
cp
ls
who
Database Systems
Application portability is the ability of a single application to be executed on various types of computer
hardware without being modified. This can be achieved if the application uses the UNIX interface to
manage its hardware needs. UNIX’s layered design insulates the application from the different types of
hardware. This allows the software developer to support the single application on multiple hardware types
with minimal effort. The application writer has lower development costs and a larger potential customer
base. Users not only have more applications available, but can rely on being able to use the same applications
on different computer hardware.
applications that developers and users can use. This standard interface allows application portability and
facilitates user familiarity with the interface.
The UNIX system is supported by the file and the process. Directories and devices are treated as
files and there are many text manipulation tools to edit these files. When a file is executed as a program,
it is called a process. There are tools to control processes like sending a processes into background or
even terminating it.
Unix is a multitasking operating system, which allows the computer to run several programs at the
same time. By going quickly from one task to another and performimg a little bit of each task every time,
the operating system gives an impression of doing many things at the same time. Unix uses this technique
of time-sharing. Unix is also able to interact with more than one user at a time. This feature of Unix
makes it a multitasking system.
1.7 CONCLUSION
Unix is an open system and has been adapted to run on a wide variety of computers. It is a portable
operating system, which can run on everything from personal computers to mainframes more than a
billion times as powerful as the first system.
EXERCISES
1. Why does a computer require an operating system?
Chapter 2
M
ost of the Unix command names are short, single words in lowercase. The commands are
basically programs written in C. Traditionally, Unix system programs are stored in directories
called /bin and /usr/bin, with additional programs usually used only by system administrators in
/etc and /usr/etc. Many versions of Unix also have programs stored in /usr/ucb There may be other
directories containing programs. Users may also have their own directories where custom commands and
scripts are kept, such as /usr/local/bin.
A command can be located by using the type command. For example to get the location of the cat
command in the Unix system, the following command is entered-
$ type cat
cat is /bin/cat
When the cat command is entered, the shell searches for the command in the search path.
The search path is stored in an environment variable called PATH. The path is searched in order, so if
there are two commands with the same name, the one that is found first in the path will be executed. The
search path isn’t built into the shell; it is specified in the shell setup files
Example
$ echo $PATH
/bin:/usr/bin:/college1/sibns/bin:.
That is, the shell interprets the command and changes the current directory. On the other hand, cat
command is an external program stored in the file /bin/cat.
The shell doesn’t start a separate process to run internal commands. External commands require the
shell to run a new subprocess; this takes some time, especially if the system is busy.
where command is the name of the command. Command names are typically very short, and are often
cryptic in their meaning.
The options modify the action of the command and are generally single characters There may be no
options or there may be several acting on the same command. Options are generally preceded by a
hyphen (-) character. Grouping of options have no consistent rule and some commands allow a list of
options with just a single hyphen at the beginning of the list. Some other commands may require a hyphen
before each option.
There are some options which allow a value, maybe a filename, to be given following the option. The
value may be placed immediately following the option letter or a space may be required between the
option letter and the value. This depends on the options.
The arguments are values upon which the command is to operate. These are very often filenames,
and depending on the command there may be several arguments or none.
Unix being case-sensitive, the exact combination of upper and lower case letters used in a command,
option or filename are important.
Long command lines can be split and entered in several lines. The shell recognises an incomplete
command and displays a second prompt (>).
BSIT 43 UNIX and Shell Programming 9
$ man man
To find out more about the ls command, the following command can be used:
$ man ls
The manual pages are displayed one screenful at a time with a pause after each page. The man
command uses the pager called more to do this. Other pagers can also be used by setting it in the PAGER
variable. The pager used, controls the man pages display and at the end of each page, a prompt (colon)
is displayed. Keys can be pressed to navigate or search the pages. Typing q at the prompt quits man and
returns the user to the shell.
key Action
f or spacebar displays the next page
b displays the previous page
/ Searches for the string following the /
There is also a limited help facility built into the man command for searching subject lines by keyword.
When the –k option is used, man searches a summary database for the argument and displays all the lines
that contain the argument. For example, to get a one-line description of the command uname, the following
command can be given:
$ man -k uname
uname (C) - print the name of the operating system
2.7 CONCLUSION
In this chapter, we learned about Unix commands and how they can be used with arguments and
options. The on-line documentation available in the Unix system is helpful in looking up commands and
their descriptions.
EXERCISES
1. How are commands located in Unix ?
5. How do you stop and resume the scrolling of the screen display ?
Chapter 3
General-Purpose Utilities
Syntax:
cal [ [ month ] year ]
where year is a number between 1 and 9999 and month is a number between 1 and 12 :
$ cal
$ cal jan
$ cal 1
$ cal 2005
BSIT 43 UNIX and Shell Programming 13
Example 1: without arguments, date displays the current date and time
$ date
Thu Dec 16 13:25:00 IST 2004
Example 2:
$ date +%D
12/27/04
Example 3:
$ date +”%d %h %y”
27 Dec 04
Syntax:
echo [-n] text
The -n option prints a line without the newline.
Example:
$ echo Hello
Hello
printf formats the given arguments according to the format string and writes them to the standard
output.
$ printf “hello”
hello
Syntax
bc
It is an interactive tool for performing calculator-like computations. It is commonly used in computation
with large integers, computations accurate to many decimal places and conversions of numbers from
one base to another base.
BSIT 43 UNIX and Shell Programming 15
i.e., addition, subtraction, multiplication, division, modulo (remainder) and exponentiation. Division by
zero produces an error message.
In more complex expressions with several operators and with parentheses exponentiation (^) performed
first, then multiplication (*), division (/), modulo (%), and finally, addition (+), and subtraction (-). The
contents of parentheses are evaluated before expressions outside the parentheses. All of the above
operations are performed from left to right, except exponentiation, which is performed from right to left.
When invoked, bc expects input from the keyboard. An arithmetic expression is typed in and bc
displays the result. Ctrl-d is used to exit bc.
Example 1:
$ bc
scale=5 specify the number of digits after the decimal point using scale
10/3
3.33333
^d
Example:
$ script start a script session. If no filename is given, script will use the default
filename ‘typescript’.
$ script scriptfile start a script session and record terminal session to file scriptfile
$ script –a scriptfile start a script session append to an existing script file
The contents of the script file can be viewed using cat after ending a script session.
l Passwords can be combinations of numbers, upper-case and lower-case letters, and puctuations
l Obvious passwords like common names, words from the dictionary, names of places, etc. should
not be used
l There should be a significant diference between the old pasword and the new password
It is advisable to change the password frequently to prevent unauthorized access to a login account. .
Syntax:
passwd
The system will prompt the user for the old password. Once this is entered, the new password is
requested. The system once again prompts the user to retype the new password, to make sure that there
is no mistake in the entry. If the new passwords do not match or the old password is not entered correctly,
the password change will be rejected otherwise. It is to be noted that, whenever passwords are entered,
they are not visible.
BSIT 43 UNIX and Shell Programming 17
Example:
$ passwd
Setting password for user : usr1
Old password:
New password:
Re-enter password:
Syntax
Who
Who am i
Examples:
Lists the user’s name, terminal line and login date and time
$ who
$ who am i
$ who -q
uname [ options ]
Examples:
Displays all the information
$ uname -a
SCO_SV sco5 3.2 5.0.5 i386
$ uname -s
SCO_SV
$ uname -v
5.0.5
Displays the information about system name, node name, operating system release number, kernel ID,
processor type, bus type, serial number, number of users license, OEM number, origin number and number
of CPUs.
$ uname -X
System = SCO_SV
Node = sco5
Release = 3.2v5.0.5
KernelID = 98/07/02
Machine = PentII
BusType = ISA
Serial = 5GE048977
BSIT 43 UNIX and Shell Programming 19
Users = 5-user
OEM# = 0
Origin# = 1
NumCPU = 1
Syntax
tty
Example
The terminal file name which is present in the /dev directory is displayed
$ tty
/dev/ttyp0
Syntax
stty
Examples:
Displays all the terminal settings like the speed of the terminal (baud rate), interrupt character, erase
character, end-of-file character, etc.
$ stty -a
speed 38400 baud; ispeed 38400 baud; ospeed 38400 baud; line = 0(tty);
intr = DEL; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = ^@;
swtch = <undef>; susp = <undef>; start = ^Q; stop = ^S;
-parenb -parodd cs8 -cstopb hupcl cread -clocal -loblk
-ortsfl -ctsflow -rtsflow
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc
20 Chapter 3 - General-Purpose Utilities
$ stty –echo
To restore keyboard input
$ stty echo
To reverse the above setting and do a backspace without erasing the character
$stty –echoe
To change the interrupt character from [delete] to ^c. To use a control character in an stty setting, a
literal caret is preceded by a backslash.
$ stty sane
Syntax
lock [ -v ] [ -number ]
The lock command requests a password from the user, requests it again for verification, then locks the
terminal until the password is reentered. If a number is specified in the lock command, the terminal is
automatically logged out and made available to another user after that number of minutes has passed.
Examples:
$ lock
BSIT 43 UNIX and Shell Programming 21
Password:
Re-enter password:
Terminal locked by usr 0 minutes ago
The -v option specifies verbose operation.
$lock –v
Password:
Re-enter password:
Terminal locked by usr 0 minutes ago
The terminal is logged out after 10 minutes.
$ lock –10
Password:
Re-enter password:
Terminal locked by usr 0 minutes ago
Syntax
spell [ file ]
where file is the name of the file to be checked; if no file name is given, input is take from the
keyboard.
Spell collects words from the named file and looks them up in a spelling list. Words that neither occur
among nor are derivable (by applying certain inflections, prefixes, and/or suffixes) from words in the
spelling list are printed on the standard output. If no file is named, words are collected from the standard
input.
Example:
Syntax
ispell filename
22 Chapter 3 - General-Purpose Utilities
The spell checking program, spell works well for quick checks of spelling in a short document. ispell is
a real spell checker, which not only displays the misspelled words in context, but offers to change them
too. The syntax is the same as that for spell.
l In the case of ispell, each word that does not appear in its dictionary will be displayed at the top
of the screen allowing the user to change it.
l If the dictionary contains any near misses (i.e., words which differ by only a single letter, a
missing or extra letter, a missing space or hyphen or a pair of transposed letters,), ispell will
display them.
l ispell may display guesses at ways to make the word from a known root, with each guess
preceded by question marks.
l ispell prints the line containing the word and the previous line at the bottom of the screen. If the
terminal can display in reverse video, ispell highlights the word itself. The user can then replace
the word completely, or choose one of the suggested words.
Some of the basic commands used in ispell are listed below:
command action
ispell commands are not case sensitive (commands can be typed either in lowercase or uppercase);.
The tput command uses the terminfo(F) database to make the values of terminal-dependent capabilities
and information available to the shell.
Example 1: Clears the Screen
$ tput clear
Example 2: Print the number of columns for the current terminal.
$ tput cols
80
3.15 CONCLUSION
All the Unix commands discussed in this chapter were used as single commands. The output of these
commands can be used by other Unix commands (filters) for further processing. Combining commands
to perform complicated tasks is a feature unique to Unix.
EXERCISES
1. Use the cal command to find out whether the year 1900 was a leap year.
2. Use the echo command to display the message ‘Good Morning’. The cursor should move to the next line after
displaying the message.
24 Chapter 3 - General-Purpose Utilities
Chapter 4
E
very item in a UNIX file system can de defined as belonging to one of four possible types:
Ordinary files: Ordinary files can contain text, data, or program information. An ordinary file cannot
contain another file, or directory. An ordinary file can be a text file or a binary file. A text file contains
lines of printable characters where every line is terminated with a newline character. A binary file can
contain any of the ASCII characters. Most of the UNIX commands are binary files
Directory files: Directories are containers that can hold files, and other directories. A directory is
actually implemented as a file that has one line for each item contained within the directory. Each line in
a directory file contains only the name of the item, and a numerical reference to the location of the item.
The reference is called an i-number, and is an index to a table known as the i-list. The i-list is a complete
list of all the storage space available to the file system.
Special files Special files represent input/output (i/o) devices, like a tty (terminal), a disk drive, or a
printer. Because UNIX treats such devices as files, some of the commands used to access ordinary files
will also work with device files. This allows for more efficient use of software. Special files can be either
character special files, that deal with streams of characters, or block special files, that operate on larger
blocks of data. Typical block sizes are 512 bytes, 1024 bytes, and 2048 bytes.
Links A link is a pointer to another file. Since a directory is a list of the names and i-numbers of
files, a directory entry can be a hard link, in which the i-number points directly to another file. A hard link
to a file cannot be distinguished from the file itself. When a hard link is made, then the i-numbers of two
different directory file entries point to the same inode. Hence, hard links cannot span across file systems.
A soft link or a symbolic link provides an indirect pointer to a file. A soft link is implemented as a directory
file entry containing a pathname. Soft links are distinguishable from files, and can span across file systems.
Soft links are not supported in all versions of UNIX.
3. Numbers (0 to 9)
The Unix command names should also not be used for naming files. While naming files, it is better to
choose names that can give the user an idea of its contents. It is also good practice to use extension to file
names. Extensions are a few characters separated from the file name by a dot (.). For example, if there
are files containing the marks list of students in the first, second and third years, they could be named
marks.yr1, marks.yr2 and marks.yr3. Here yr1, yr2 and yr3 are extension names to the file name marks.
Since Unix is case sensitive, a filename with uppercase letters will be different from a filename with
lowercase letters. For example, the file MARKS.YR1 will be different from the file marks.yr1.
user1 user2
cprog
pay.lst cprog
In the file system tree shown above, the directory. user1 is the parent of the file pay.lst and the
directory cprog, users is in turn the parent of user1 and user2 and / is the parent of users Thus, in a parent-
child relationship, the parent is always a directory
Example:
$ pwd
/users/user1
$ cd cprog
$ pwd
/users/user1/cprog
l An absolute pathname starts at the / (root ) directory and lists each directory along the path to
the destination file (or directory).
l An absolute pathname uses a slash ( / ) between each directory name in the path to indicate
different directories.
Example:
$ cd /user/user1/cprog
A full pathname can become very lengthy and tedious to enter.
Example
Change to the directory set1 which is in cprog located in the working directory.
$ cd cprog/set1
$ pwd
/user/user1/cprog
$ cd ../..
$ pwd
/user
30 Chapter 4 - The File System
option action
-R recursive list
Examples:
$ ls -a
..
arith.lst
exp.lst
oper.c
sqlscripts
All files including those beginning with a dot (.) does not list current directory (.) and directory above
(..)
$ ls -A
arith.lst
exp.lst
oper.c
sqlscripts
Marks directories with a /, executables with a * and symbolic links with a @
$ ls -F
arith.lst
exp.lst
imp*
oper.c
sib5*
sqlscripts/
Lists in long format, giving mode, number of links, owner, group, size in bytes, the time that each file
was last modified.
$ ls -l
-rw-r—— 1 sibns group 643 Sep 22 11:31 arith.lst
-rw-r—— 1 sibns group 103 Sep 22 11:31 exp.lst
32 Chapter 4 - The File System
$ ls -r
sqlscripts
sib5
oper.c
imp
exp.lst
arith.lst
Lists files in columns with entries sorted across
$ ls -x
arith.lst exp.lst imp oper.c sib5
sqlscripts
Combining the l and F options
$ ls -lF
-rw-r—— 1 sibns group 643 Sep 22 11:31 arith.lst
-rw-r—— 1 sibns group 103 Sep 22 11:31 exp.lst
-rwxr—r— 1 sibns group 17 Feb 12 13:09 imp*
-rw-r—— 1 sibns group 348 Sep 22 11:31 oper.c
-rwxr—r— 1 sibns group 15 Feb 12 13:08 sib5*
drwxr-xr-x 2 sibns group 512 Feb 12 13:17 sqlscripts/
root (/)
The very top of the UNIX directory structure is called the root directory referred to as “slash.” (/).
This directory is full of other directories. Most of these directories are created at the time of installation
and contain critical user functions.
/bin and /usr/bin are directories where all the commonly used commands are located. Since most of
the command files are binary files, the directory is called bin.
/sbin and /usr/sbin are directories which contain commands that only the system administrator can
execute.
/etc directory has the configuration files of the system. The login names and passwords are stored in
the file /etc/passwd and /etc/shadow.
/dev contains all the device files. These files do not occupy any system space.
/lib and /usr/lib contain library files in a binary form. Programs written in C use these library routines.
/var contains variable data like print requests, incoming and outgoing mail.
34 Chapter 4 - The File System
4.12 CONCLUSION
In this chapter, it was observed that Unix has different file types. Each file type is handled by its own
set of commands. Commands like cd, pwd, mkdir are used exclusively with directories. The set of commands
required to handle ordinary files are discussed in the next chapter.
EXERCISES
1. What are the rules to be followed while naming files in Unix ?
2. Which of the following file names are not valid and why?
a) Books
b) file?
c) cat
d) file name
e) book.lst
f) rank*
g) slno#
3. Suppose the working directory is user1 and the parent is user, what is the absolute pathname of the file list.txt
in the working directory user1.
6. Create a new directory in your home directory and change to it. Display your absolute path.
Chapter 5
Syntax
cat [-v] filename
The command cat is normally used to display the contents of text files. With the –v option, non-printing
characters can also be displayed.
Example: displays the contents of the file sample.txt
$ cat sanple.txt
The cat command can also be used to create small files without using an editor.
Example: To create a file called file1
When the enter key is pressed, the Unix prompt disappears and cat waits for the user to type in the
contents. After each line the enter key is pressed and finally ctrl-d is pressed to terminate the cat command.
Syntax:
Source and target are both files. The contents of file infile1 are copied to target file infile2.
$ cp infile1 infile2
When target is a directory, cp copies each named file to a destination of the same name within the
target directory. Files file1 and file2 are copied to the directory myfolder.
$ cp – i infile1 infile2
The –R option copies all files and subdirectories in the directory cprogs to the directory cprogback
$ cp –R cprogs cprogsback
Syntax
Examples:
$ rm –i file1
$ rm –f infile1
With –r, rm recursively deletes the contents of the directory specified and the directory itself
$ rm –r cprogs
Syntax
Examples
$ mv file1 file2
Syntax
The command more displays a text file one screenful at a time. After each screenful, more pauses and
displays the name of the text file being viewed at the bottom. Pressing the ENTER key will continue the
display line by line and pressing the SPACEBAR will scroll the display by another screenful.
Some of the commands that can be given to more when it pauses are listed below –
38 Chapter 5 - Managing Ordinary Files
command action
! invoke a shell command
H gives a list of all the more
commands
[ctrl-g] displays the current line number
[#]b moves # lines backwards or a
default of one screenful
[#]f move # lines forward with a
default of one screenful
[#]g goes to the beginning of the file or
to line #
[#]G goes to the end of the file or to line
#
q quit from more
$ wc -lw sib0.cob
2378 sib0.cob
Count characters in the file sib0.cob
$ wc -c sib0.cob
22706 sib0.cob
option action
-x displays in hexadecimal
Example
$ cat f1
carriage return ^M
next page ^L
BSIT 43 UNIX and Shell Programming 41
000000 143 141 162 151 141 147 040 162 156
040
c a r r i a g e r e t u r n
0000020 040 015 012 156 145 170 164 040 160 141 147 145 040 040 014
012
Each line in the file f1 is displayed in two lines. The first line shows the octal representation preceded
by the position, within the file, of the first byte in the line. The second line shows the printable characters
and the escape sequences.
Assuming that file.txt has 200 kilobytes, this will output five 40 kilbyte files: fileaa, filetab, fileac, filead
and fileae
$ split -b 40k file.txt file
Syntax
Particular columns in the output can be dropped using the following options-
option action
-1 drop the output column showing lines unique to file1.
-2 drop the output column showing lines unique to file2.
-3 drop the output column displaying lines common to both
file1 and file2.
Example:
fruits2
mango
$ comm -12 fruits1 fruits2
apple
pineapple
orange
The diff command displays the line numbers of the changed lines in the two files. Special symbols are
used as instruction to indicate changes to be made to make the files identical. Each instruction is a
combination of the line address and action to be taken in the first to make the differing lines identical. For
example, the instruction 1a1,2 indicates that 2 lines are to be appended after line 1 in the first file. The two
lines to be appended are in file2.
Example:
< mango
—
> grapes
5.14 CONCLUSION
Some of the commands in this chapter use input from the keyboard. Some others take the output of
another command as their input. Commands like wc, cat and cmp can also send their output to a file. This
will be discussed in later chapters.
EXERCISES
1. Create a file called lesson1 in your home directory using the cat command.
2. Create a directory called lessons and copy the file lesson1 to this directory
4. Create another file called lesson2 and move it to the directory lessons.
5. Change to directory lessons and count the number of characters and lines in the files lesson1 and lesson2.
8. Create a file with at least 100 bytes and split it into files of 20 bytes each
BSIT 43 UNIX and Shell Programming 45
Chapter 6
File access
File type permissions
Group id File size File name
Link count
in bytes
Date & time of
User id
modification
The command ls –l lists seven attributes of all the files in the working directory. The top the list gives
the number of blocks occupied by the files (a block is 512 bytes). The first column gives the file mode,
which consists of a string of 10 characters. The first character indicates the file type:
The nine characters after the file type are the file permissions, which are displayed in three sets of
three characters each. The first set refers to the owner permission, the second set to the group permission
and the third to permissions given to other users. In each set of permissions the three characters can be
r (read), w (write), x (execute) or – (no permission is set). The x permission for a directory means it can
be searched.
In Unix, a single copy of a file can be referred to by several filenames. The second column gives the
number of filenames or links associated with the file. If the link count is greater than one it indicates that
the file has more than one name.
The third column gives the file ownership. When a user creates a file, the file automatically belongs to
the user. The third column displays the owner of the file. The owner has the right to change file permissions,
alter the contents of the file and remove the file.
The fourth column shows the group ownership of each file. At the time of user account creation, the
system administrator assigns the user to a group. All the users belonging to this group will have same
privileges to the files with the same group id.
The fifth column displays the file size in bytes. This does not mean the amount of disk space occupied
by the files. The space occupied by files is measured in blocks of 1024 bytes each. Directories show
smaller sizes because a directory maintains the list of filenames and their identification number for each
file within the directory. The size of the directory is therefore the size of this list of filenames.
The sixth column gives the date and time of modification of the files. The last column gives the name
of the file and the list is arranged in the alphabetical order.
A file permission string has 10 characters, in which the first character gives the type of file and the
remaining 9 characters form three groups of three characters each.
BSIT 43 UNIX and Shell Programming 47
$ ls –l sal
-rwxr-xr— 1 sibns sibgrp 366 Sep 22 11:31 sal
The first character is a hyphen and indicates that sal is an ordinary file. The remaining nine character
are grouped as follows –
The first group of characters, rwx shows that the user (owner) has all permissions, ie., read, write and
execute. In the second group of three characters, r-x the second character is a hyphen which means that
only read and execute permissions are given to all the users who belong to the group sibgrp. Finally, in the
last group of three characters, r—the second and third characters are hyphens which indicates that only
read permission is given to others.
Operator action
+ to add permission
- to remove permission
letter mode
Multiple symbolic modes can be given on a single command line, separated by commas.
Examples
Gives execute permission to the user for the file siblst
$ chmod u+x siblst
Gives write permission to user, group and others
$ chmod ugo+w siblst
Using multiple modes in a single command
$ chmod g+w,o+r siblst
In absolute mode, each type of permission is an octal number (base 8). For each category, the permissions
are assigned by adding the octal numbers representing the permissions as shown below-
Examples
$ chmod 764 siblst
The chmod command can also be used recursively with the –R option.
Example
$ chmod -R a+x cprogs
assigns execute permission to all the users for all the files in the cprogs directory.
50 Chapter 6 - Basic File Attributes
$ ln siblst sibrep
BSIT 43 UNIX and Shell Programming 51
display the inode numbers and link counts of the two files
It is observed that the two files have the same inode number and the link count displayed in the second
column of the file attributes is two. If another hard link is created to the file siblst, then the link count
increases to three. The link count gives the number of links created for a file.
More than one file can be linked in a single command line. In this case the destination is a directory.
Example
links are created to each one of the files file1, file2, file3 and file4 in the directory filelnk.
$ ls -il filelnktotal 56
4014 -rw-r—r— 2 sibns group 10240 Apr 5 12:34 file1
only has one parent directory, and that means the special name “..” (dot dot) in a sub-directory
always refers unambiguously to its unique parent directory.
l Hard links cannot be created across file systems. ie., a file in the /user1 file system cannot have
a link in another file system /user2.
The ln command along with the –s option is used to create a symbolic link
$ ln -s o2 o2lnk
$ ls -il o2 o2lnk
If the symbolic link file, o2lnk, is deleted, it will not matter because it contains only the pathname of the
file to which it linked. On the other hand, if the file o2 is deleted, the data is lost and the symbolic link
becomes a dangling symbolic link. This is because the data file it is linked to, no longer exists.
To put further restrictions on the permissions assigned by a program when it creates a file or directory,
a user mask is specified with the umask command.
The user mask is a numeric value that determines the access permissions when a file or directory is
created. Consequently, when a file or directory is created, its permissions are set to the permissions
specified by the creating program minus the permission values denied by the umask value.
Example
The command umask without arguments gives the octal value of the user mask.
$ umask
022
The octal number 022 is subtracted from the system default permissions to get the actual default
permission. Therefore, by default, an executable file will have the default permission set to 755 (777-022)
or rwxr-xr-x. All other files will have the permission 644 (666-022) or rw-r—r—.
The umask command can be used by the user to set a new default permission mask. The syntax is:
umask octalnumber
Setting the umask, actually means specifying which permissions are not to be granted regardless of
the permissions requested by the file creating program.
A suitable user mask value to be set for a user’s files and directories depends upon how freely
information resources are shared on the system. In an open computing environment, the umask value
maybe set to 000, which allows no restrictions on file and directory access. In such a case, when a
program creates a file and specifies permission codes for it, the user mask puts no restrictions on what the
creating program has specified.
54 Chapter 6 - Basic File Attributes
In a more secure computing environment, the user mask value 066 maybe specified. This allows the
owner complete access, but prevents all others from being able to read or write the owner’s files. Therefore,
when a file is created, its permissions are set to what the creating program specifies, subtracted by the
user mask value. This denies read and write access to everyone but the owner.
In a very secure computing environment, the user mask value will be 077. This allows allows access
only to the owner. When a file is created, its permissions are set to what the creating program specifies,
minus the user mask value which prevents anyone else from reading, writing, or executing the owner’s
files.
Example
$ umask 037
If the file is executable, this umask value will set a permission code of 740 (777-037) or rwx-r——,
which means the owner is allowed all permissions, group members are allowed only read permission and
others are not allowed any permissions.
Syntax
Example
Find and display all the files called a.out in the current directory. The dot (.) after find indicates the
current directory. The –print action displays the location of the file file1.lst in the current directory (dot).
Find and display all the directories in the file system /user1
find all the directories in the home directory with permissions 777. An implied and condition between
perm and type is applied by find here.
6.9 CONCLUSION
Every directory and file on the system has an owner, and also an associated group. It also has a set of
permission flags which specify separate read, write and execute permissions for the ‘user’ (owner),
‘group’, and ‘other’ (everyone else with an account on the computer) The ‘ls’ command shows the
permissions and group associated with files when used with the -l option. In this chapter, all the file and
directory attributes have been discussed. The use of inodes to store file attributes, hard links and their
limitations and symbolic links have also been discussed.
EXERCISES
1. How do you list all the attributes of a file in your home directory ?
2. Create a file called sample.txt. How do you assign all permissions to the owner, read and write permissions to
the group and no permissions to others using relative permissions and absolute permissions.
5. What are the changes seen in the file’s inode number when a link is created ?
56 Chapter 6 - Basic File Attributes
6. How are inode numbers displayed ? If two files have the same inode numbers, what do you conclude ? what
happens when you delete one of these files ?
9. For the file sample.txt, create a symbolic link. What is a dangling symbolic link ?
10. How do you order the list of files by their modification time ?
12. Find all the files having the same inode number in the /usr directory.
13. Find all the files called core in youe home directory and remove it.
BSIT 43 UNIX and Shell Programming 57
Chapter 7
The Vi Editor
V
i is a full screen text editor which can be used to create and edit text files. To invoke vi, the user
types vi followed by the filename at the Unix shell prompt. For example to create a file called
sample.txt, vi is invoked with the following command –
$ vi sample.txt
If the file sample.txt already exists, vi opens it for editing. vi makes use of 24 lines of the terminal
display for editing.
The input mode is used to enter text. Every key pressed after entering this mode is displayed on the
screen. After completing text entry, the cursor is positioned on the last character of the last line. This line
is called the current line and the cursor position is called the current cursor position. The [backspace] key
moves the cursor backwards and wipes out the character entered. To return back to the command mode,
[Esc] key is pressed.
The ex mode or the last l;ine mode is used to enter and run commands like saving files, reading files,
text replacing, exiting vi, etc. The text entered in the input mode resides in a temporary storage called the
buffer. To save text to a file, from the command mode, the user has to change to the ex mode which is
done by entering a colon followed by the save command and the [Enter] key. After the command is
executed, vi returns to the command mode.
Many commands in the command mode and the input mode have the repeat factor. A command is
executed as many times as the prefix given to the command. For example, if a character is deleted with
the command x, with a repeat factor of 4, the command deletes the character below the cursor as well as
the next three characters. This helps in speeding up operations.
a,i,I,A,o,O,
R,r,s,S and C
Command
mode
shell
BSIT 43 UNIX and Shell Programming 59
k cursor moves up
w word forward
b word backward
e end of word
^ first character
$ end of line
0 beginning of line
The screen display can be controlled by scrolling the display forward and backward using the following
keys:
The current line number of the cursor position can be obtained by pressing [ctrl-g]. The cursor can be
positioned to the end of the file or to any line in the file by a single keystroke as follows:
60 Chapter 7 - The vi Editor
nG line number n
command Function
dw delete a word
:1,$s/sales/market/gp
vi searches for the pattern sales in the entire file and replaces it by the text market.. The flag g
indicates that the substitution is to be done globally and p displays the number of substitutions done.
J join lines
Y yank lines ie. Copy lines (same as yy)
7.10 CONCLUSION
vi is a very powerful screen editor. Some of them have been featured in this chapter There are a lot of
other features available with vi. It requires practice to master the vi editor.
EXERCISES
1. How do you do the following using vi:
a) delete a line
b) overwrite text
e) remove two lines from the end of a text and add it to the beginning of the file.
3 How do you replace all the occurrences of the word printf with fprintf in a file ?
4 What are the different ways of coming out of vi after saving the file ?
Chapter 8
The Shell
A
shell is a program which is run every time a user logs in and is known as a command interpreter.
All the commands entered are run by the shell. The prompt that appears when a user logs in is a
shell and waits for the user to enter a command.
When a command is entered, the shell scans the command line for metacharacters, ie., characters that
have a special meaning to the shell. After processing the metacharacters, the shell send the command line
to the kernel for execution. The shell waits for the kernel to complete execution and then displays the
prompt for the next command. This interpretive cycle continues for the next command
The C shell (/bin/csh), written at Berkeley, is an improvement over the bourne shell. The C shell has
some additional features like filename completion, command aliasing, history substitution, job control, a
number of built-in commands, and an improved programming language. The tcsh is a derivative of the C
shell. This shell has a number of built-in features, the most useful being the command line editing, and the
use of the arrow keys for browsing the command history.
? a single character
The question mark, ‘?’, stands in for any single character. For example, suppose there are three files,
file1, file2 and file12 in a directory. The ls command with file? As argument will display the following:
$ ls file?
file1 file2
file12 will not be listed because the question mark can stand in for either ‘1‘ or ‘2’, but not both at the
same time.
Since, the asterisk, ‘*’, stands in for any number of characters—including zero characters, the following
command lists all the files that begin with file.
$ ls file*
file1 file2 file12
The command ls * will list all files except those that begin with a . (dot). If such files also have to be
listed, the . (dot) has to be explicitly matched. For example:
$ ls .*
66 Chapter 8 - The Shell
will list all files, including the ones that begin with a . (dot).
Matching filenames can be restricted by using the character class. The character class is a set of
characters enclosed by the rectangular brackets ( [ ] ).
Example:
Matches all filenames beginning with file and ending with characters 1, 2 and 5
$ ls file[125]
file1 file2
Matches all filenames beginning with file and ending with the range of characters 1 to 5
$ ls file[1-5]
file1 file2
$ ls [a-zA-Z]*
file1 file2 file12
Matches all files
$ ls *
file1 file2 file12
remove all files with filenames ending with .txt
$ rm *.txt
remove all files having filenames starting with file, any character and then ending with .lst
$ rm file?.lst
Each special character has a special meaning to the shell But, if a file name does contain any of these
characters, the shell should take the special character literally. There are two ways of doing this –
precede the special character by a backslash (\) or enclose the file name in single quotes (‘) in which
case, any special character in the file name will be taken literally..
Examples
If there are four files with file names file1, file2, file3 and file*, to delete the file with the name file*,
the escape character \ is used.
$ rm file\*
Without the escape character (\), the rm command would have interpreted * as a wildcard and deleted
all the files with filenames beginning with file.
display an asterisk
$ echo \*
$ echo “***”
delete a file with filename file*
$ rm ‘file*’
The use of single quotes protects all the special characters, except single quotes. Double quotes are
not so powerful and do not protect the $, backquote (‘) and the double quote itself.
1. standard input (stdin), the file representing input from the keyboard
The command expects to read its input from the standard input. The file representing standard input
can have three sources:
1. the default source which is keyboard
The program writes its error messages to standard error, which by default is the screen.
A command usually reads input from the keyboard (standard input) and writes its output to the display
(standard output). If a commnd needs to read its input from a file or write its output to a file, or both, the
input and output files for a command can be selected using the following notations:
notation action
< Read standard input from a file
> Write standard output to a file
>> Append standard output to the end of a file
All shells allow a user to redirect the standard input of a process to read input from a file, instead of
from the keyboard. Any command that accepts input from the keyboard can use input redirection. Some
commands like who, that do not accept input cannot have redirection for input. The < character is used to
redirect input to a command from a file.
Example
many commands allow the input file to be specified without the redirection notation.
If no argument is given, wc reads its input from the keyboard. The end of input is indicated by the
pressing [ctrl-d].
$ wc
The wc command
[ctrl-d]
However, a few commands like mail make use of < for special functions.
Example
The shell can redirect the standard output of a process from the screen (default), to a file. Hence, the
output generated by a command can be stored in a new file using > or appended to an existing file using
>>. If >> is used to write output to a file that does not exist, the shell creates the file containing the output
of the command.
Example
The output of ls goes to the file named file.lst If file.lst already exists, the shell overwrites its contents
with the output of ls. If file.lst does not exist, the shell creates it.
$ ls >file.lst
The output of ls is added to the end of the file named file.lst: If file.lst does not exist, the shell creates
it.
$ ls >>file.lst
concatenate all the list files and redirect it to the file allfiles.lst
In addition to the standard output, a command can also produce error or status messages known as
diagnostic output. When a command is executed successfully, the results are displayed on the standard
output. But, when a command executes unsuccessfully, an error message is displayed on the screen
which is the default standard error file. The shell, however allows the error to be redirected to a file.
70 Chapter 8 - The Shell
The three standard files are represented by a number called the file descriptor digit. The kernel
maintains a table of file descriptor digits for each one of the commands being executed on the system.
The file descriptors are:
When the ls command tries to list a file which does not exist, an error is redirected to the file errorfile
$ cat errorfile
The process writes the standard output to outfile. The 2> symbol redirects the error output to errorfile.
Example
The output of the shell script execution is redirected to result.txt and the error to err.lst
The 1> symbol redirects the standard output to outfile. The 2>&1 symbol tells the shell to write the
standard error in outfile.
$ trial.sh > result..txt 2>&1
Example
$ trial.sh > result..txt 2> /dev/null
The special file /dev/tty is associated with the terminal. This file can be used for redirection and as an
argument in some Unix commands.
Every terminal in the Unix system has a different terminal number. However, users logged into the
system can refer to their respective terminals with the same file name /dev/tty. This is useful in programs
or shell scripts where the output has to be explicitly redirected to the terminal, /dev/tty.
8.7 PIPES
A pipeline is the simplest and most used inter-process communication (IPC) mechanism in Unix. A
pipe is a communication channel defined by two file descriptors, one open for output and one for input
The output from one process goes as an input to another process, which means data goes into one end of
the pipe an comes out of the other. The data flowing in a pipe are managed directly by the kernel. The
shell connects the standard output and standard input using the special pipe character ( | ).
Pipes in Unix are commonly used to avoid writing temporary files to communicate between programs.:
Example
$ ls -la | more
which takes the standard output (stdout) from the ls command and transfers it to the standard input
(stdin) of the more command The more command list of the files one page at a time.
$ rm filelst
The output of the ls command is redirected to a file called filelist. The contents of filelist are then
displayed using the more command. By running two separate commands, it is observed that:
1 When the first command takes a long time to execute, the second command will not run until the
first command has completed execution.
2 An intermediate file is used which, when large can take up disk space. This file has to be
removed after the command execution is complete.
Another example:
Count the number of users currently logged into the system Using a pipe eliminates the use of an
intermediate file.
$ who | wc –l
Count the number of words in all the files ending with .txt.
$ cat *.txt | wc –w
There is no restriction on the number of commands used in a pipe. However, proper placing of commands
in the pipeline is important.
Example:
Get a directory listing on the terminal, while also redirecting the output to a file named filelist.:
$ ls -al | tee filelist
BSIT 43 UNIX and Shell Programming 73
When this command is run, the output of ls –al will not only appear on the terminal as expected, but will
also be saved in the file named filelist.
The cat command displays a copy of the contents also sent to the file.
$ cat filelist
The tee command is very useful in a situation like this, where the user would like to see the output on
screen and also capture it into an output file.
The ls command lists all .htm files in the current directory, count the number of lines and the output is
displayed to the /dev/tty (terminal) also redirected to the hcount.txt.
Example
date is a Unix command whose output is given as an argument to echo. The command is enclosed in
a pair of backquotes.
$ echo today is ‘date‘
The shell executes the date command and replaces the enclosed command with the output of the
command.
two commands are used in a pipeline and the output used as an argument to echo
The environment shell variables tell the shell about the way an user account is set up. Some of the
commonly used environment variables are listed as follows:
74 Chapter 8 - The Shell
Some of the environment shell variables like HOME and SHELL are set automatically when the user
logs in. Other variable like TERM maybe set by the user.
Example
If the user is working on a terminal of type vt100, the variable TERM is set to vt100.
$ TERM=vt100
$ export TERM
Users can define their own variables by giving the variable a name and value.User-defined variable
names should begin with a letter and can contain letters, numbers and the underscore (_) character.
Variable names are case-sensitive and are all of type string. Numbers are also stored as strings, The
default value of a shell variable is always a null string.
Variable=value
There should be no space on either side of the = character. To display the value assigned to a variable,
the name of the variable is preceded by a $. In a command line, any word preceded by a $ is recognized
as a variable by the shell and replaces the word by its value.
Example
$ abc=100
$ echo $abc
$ xyz=$abc
$ echo $xyz
$ abc=
$ unset abc
Example
Variables can also be concatenated by placing them one beside the other.
Example
$ mesg1=’This is a ’
$ mesg2=’sample concatenation’
$ mesgfull=$mesg1$mesg2
$ echo $mesgfull
$ mesgfull=$mesg1’sample concatenation’
8.11 CONCLUSION
In this chapter we have learned about how a command line is interpreted by the shell. The command
line words are parsed and variables are evaluated. We have also learned how to redirect standard output
and standard error separately. Command substitution and setting up of pipelines were discussed. The
shell’s programming features will be discussed in a later chapter.
EXERCISES
1. What is the file descriptor for standard out? standard in? standard error?
3. Write a command to pipe the output of cat into the more command?
76 Chapter 8 - The Shell
4. Write a command to append the file txt1 to the end of the file txt2.
6. How can you remove a file named ?ops without removing the file wops ?
7. How do you display the value of the shell variable PATH? What is this variable used for?
Chapter 9
The Process
A
process is born when a program starts execution and exists as long as the program is running.
After execution the process is said to die.The name of the process is usually the name of the
program being executed.
In Unix, the kernel is responsible for the process management. It determines the time and priority
allocated to each process so that multiple processes can share the CPU resources. A process executes
for a finite period of time and then gives control to another process. Sometimes the kernel stores pages of
the processes in the swap area of the disk before calling them again for execution.
Processes also have attributes. Some of the attributes are maintained in a structure called the process
table by the kernel. The process table is stored in the memory. The important attributes of a process are:
1. The process-id (PID). Each process is identified by a unique number called the process-id
which is alloted by the kernel when the process is born.
2. The parent PID (PPID) The PID of the parent is also a process attribute. When many processes
have the same PPID it is easier to kill the parent process rather than the children separately.
As soon as a user logs into the system, the kernel starts a process called the shell. The command
representing the process could be any one of the shells, namely, sh, ksh, csh or Bash. Any Unix command
typed at the shell prompt, is an input to the shell process. The shell process remains alive until the user logs
out of the system.
The login shell process has a set of environment variables which can be displayed by the env
command.
$ env
HOME=/college1/sibns
HUSHLOGIN=FALSE
HZ=100
LOGNAME=sibns
MAIL=/usr/spool/mail/sibns
PATH=/bin:/usr/bin:/college1/sibns/bin:.
SHELL=/bin/sh
TERM=vt100
TZ=IST-5:30
The value of environment variables can also be determined with the echo command. These variables
are given default values or are set at login from the .profile. Each process can have different values for
these variables. PATH determines the location and the order in which the shell will search for commands.
TERM controls the emulation type of the current terminal session. The pathname of the shell is stored in
the variable SHELL and the PID is stored in a special variable called $$. The shell’s PID can be
displayed by typing
$ echo $$
2814
The PID of the shell remains the same throughout the login session. The shell process gets killed once
the user logs out. If the user logs in again, the shell PID changes.
Every process has a parent which in turn is another process. For example when the command cat
filename is run, the cat process is started by the shell process. In this case the shell is said to be the parent
of cat and cat is called the child of the shell process.The multitasking feature of Unix allows a process to
have one or more children. For example in the command
Since, the shell itself is a process, there can be multiple login shell processes running (for every new
terminal session a new shell process is opened). Any process in Unix may begin as a single process and
then multiply into many processes. Shell processes, by default, run in the foreground. This means that the
shell process (the parent) cannot do anything until the new process (the child) is completed. Processes
can also run in the background which permits the shell process to continue working while the child
process is running.
BSIT 43 UNIX and Shell Programming 79
$ ps
The PID column gives the process ID, the TTY column names the terminal where the command was
started, the TIME column shows the amount of CPU time used by the command and the CMD column
shows the name of the command..
option explanation
-f full list showing the PPID of each process
-e or -a All user and system processes
-u user1 Processes of only user1
-a Processes of all users
-l Long list displaying information related to memory
-t term Processes running on the term terminal
Examples
Get a full listing of all the processes with the parent PID for each process
$ ps -f
$ ps -u sibns
$ ps -a
$ ps -e
6 ? 00:00:00 strd
1942 tty01 00:00:00 login
43 ? 00:00:00 syslogd
47 ? 00:00:00 ifor_pmd
48 ? 00:00:03 ifor_pmd
36 ? 00:00:00 htepi_daemon
74 ? 00:00:00 strerr
87 ? 00:00:13 agent
53 ? 00:00:00 sco_cpd
55 ? 00:00:01 ifor_sld
On a Unix system with many users, a large number of processes will be listed. System processes
display a ? in the TTY column because they are not associated with any terminal. These processes are
called daemons. A daemon process is usually defined as a background process that does not belong to a
terminal session. Daemons cannot be interrupted and become active only when they receive input. There
are many system services performed by daemons, namely, network services, printing etc.
The wait system call is executed by the parent process to wait for the child process to get completed.
The exit status of the child process is picked up by the parent process. When a process is forked, the
parent and child have different PID and PPID but the child inherits the following attributes from its
parent:
To create a Unix shell, init forks and execs a getty for every active communication line in a multiuser
system. The getty shows the login prompt on each terminal and sleeps. When the user logs in to the
system at the terminal, the sleeping getty wakes up and does a fork and exec on the login program to
check the login name and the password entered. If the login is successful, the login shell is created. The
parent-child processes are shown below:
Fork fork-exec fork-exec
init getty login shell
It is observed that the init process is an ancestor of the shell process. The init process again goes to
sleep until the user logs out. When the user logs out, the shell gets killed and init wakes and another getty
is born for the terminal until the next user logs in.
Because background processes increase the work load on the system, they may also slow down the
rest of the system.
Most processes, including background processes, send their output to standard output, ie., the terminal
screen. Since, this may interfere with the other commands being run at the terminal, it is usually good
practice to redirect the output of a background process to a file or to a printer. The output can then be
examined whenever required.
Examples
The find command runs in the background (by entering an ampersand [&]) and redirects its output to
a file named ofile
BSIT 43 UNIX and Shell Programming 83
The status of the process can be checked with the ps command. A process can be terminated with
the kill command.
A background process gets terminated when a user logs out. This is because the shell gets killed and
when the parent dies, all the child procsses also get killed. This is taken care of by the nohup (no hangup)
command.
Example
$ nohup find / -type f -print &
350
The shell dsplays the PID and the results of the find command are stored in the file nohup.out. The
user can safely logout of the system without disturbing the process running in the background.
Syntax
When a process needs to be terminated the kill command is used to send a signal to the process to kill
it. The kill command is an internal command The kill command can be used with or without a signal id
Syntax
kill [options] pid
kill [options] -signal pid
where pid is the process’ identification number (given by the ps command), and signal (which is
optional) is the signal to be sent to the process. The default signal is number 15, the SIGTERM signal,
which tells the process to terminate. The signal can be either a number or its symbolic name. The option
–l with kill lists all the signal names.
The kill command allows you to kill one process ID or multiple process IDs.
Example
Kill a process with PID 2984
$ kill 2984
If all these processes have the same parent, then the parent PID can be killed to kill all the child
processes. Killing a user’s login shell is the same as logging the user out.
Some processes ignore the kill signal, SIGTERM. In such case the following command can be used:
$ kill –9 5841
Processes running in the background can also be killed. Without using the ps command to find out the
PID of a background process, it can be killed by the following command:
$ kill $!
BSIT 43 UNIX and Shell Programming 85
Example
When the command is entered with a scheduled time as its argument, the at prompt is displayed. The
name of the program to be executed at the time mentioned is typed in and [ctrl-d] is entered to exit at. The
job is put in a queue and is executed at the time given as argument to the at command.
$ at 17:30
at>sample.sh
[ctrl-d]
The standard output and standard error of the shell script, sample.sh will be sent to the mail box of the
user. This can be opened later by mailx. The output can also be redirected to a file as follows:
$ at 17:30
at>sampl.sh > sample.out
[ctrl-d]
The keywords now, noon, midnight, today and tomorrow can also be used with the command at.
Example
$ at tomorrow
at>sample.sh
[ctrl-d]
The command at with the option –l, displays a list of all the jobs. The –r option can be used to remove
a job.
The batch command is also used to schedule jobs for later execution. Unlike at, time is not specified
here and jobs are executed as soon as the system load reduces. The batch command does not need any
arguments. It uses an internal algorithm to determine the execution time. Jobs that require a lot of CPU
time are not allowed to run at the same time, which could slow down the system.
Example
$ batch < sample.sh
A job scheduled with the batch command goes to a special at queue. The job cn be removed from the
queue by using the –r option with at.
86 Chapter 9 - The Process
A user can execute crontab if the user’s name appears in the file /usr/lib/cron/cron.allow. If the
cron.allow does not exist, the cron.deny file in /usr/lib/cron is checked. If the user’s name is not in this file
the user is allowed to use crontab. If only cron.deny exists and is empty, all users can use crontab. If
neither file exists, only the root user can use crontab. The allow and deny files contain one user name per
line.
Each line in a crontab file has five fields for specifying day , date and time followed by the command
to be run at that interval.
* * * * * command to be executed
Example
The following line in the crontab file removes all core files /user/user1 everyday at 7:30 PM.
30 19 * * * rm /user/user1/core
The time schedule can be changed as follows to run the command at 5:30 PM on the 1st of Mar, June
and September.
BSIT 43 UNIX and Shell Programming 87
30 17 1 3,6,9 rm /user/user1/core
By default cron jobs send an e-mail to the user account executing the cronjob. If this is not needed put
the following command
Example
The time taken to perform a find operation can be displayed by preceding the find command with
time.
real 0m28.815s
user 0m2.390s
sys 0m10.890s
The real time shows the time elapsed between start of exection to end of execution. The user time
shows the time taken by the program to execute. The sys time indicates the time used by the kernel to the
work on behalf of the user process. The CPU time is the sum of the user time and the sys time. This could
be appreciably less when compared to the real time on a very busy system
9.11 CONCLUSION
In this chapter the discussion was about the relationship of files and processes. The creation of a
process by using fork and exec, kernel communication with the process through a signal, scheduling a
process for a one-time execution using at or running it with batch were also discussed. The use of cron
to run a process repeatedly at regular intervals and timing of a process execution by using the tool time
were discussed.
EXERCISES
1. What command will give you a listing of all running processes that are owned by you?
88 Chapter 9 - The Process
6. Open another terminal session. Find the pid of this process and kill it. What command did you use?
Chapter 10
W
rite and talk are communication programs that allow users to talk to each other in a network.
The write command copies lines from one user’s terminal to that of another user. The command is
used by typing the following
$ write username
Where username is another logged in user. The recipient gets a message from the sender along with
the sender’s username. If the recipient of the message now writes back, communication will start between
the two users and continues until an end-of-file is read from the terminal or an interrupt is sent.
Communication terminates and write displays (end of message) on the other terminal and exits.
Conventionally, each user should end every message with a distinct signal (o) for “over” and end the
conversation with (oo) for “over and out” .
If the recipient user is logged on to more than one terminal, the ttyname is used to indicate the
appropriate terminal.
The program talk is a visual communication program which copies lines from a user’s terminal to that
of another user. It allows text-based communication between two users.
For example, if user1 wishes to talk to user2 who should be logged into the the system, user1 types the
following:
$ talk user2
The screen on user1’s terminal immediately splits into half with a “waiting for response “ message at
the bottom of the screen. A message with the sender’s name also appears on user2’s terminal. To have
a conversation with user1, user2 should also type the following
$ talk user1
the following command
$ talk user1
The screen on user2’s terminal also splits into half and the conversation in real-time starts. Both user1
and user2 can send messages simultaneously. All incoming messages are displayed in the bottom half and
all the outgoing messages in the top half of the screen. Either user1 or user2 or both by typing ctrl-c
terminate the talk program. The terminal screen is restored to its previous state.
To talk to a user who is logged in to more than one terminal, the ttyname is used to indicate the
appropriate terminal name.
The personal mailbox is a file called mbox in the user’s home directory. Any mail message that is read
and not saved in another file or deleted, is saved in the mbox file when the user quits the mail program.
BSIT 43 UNIX and Shell Programming 91
The Unix shell checks the mailbox at regular intervals (set in the MAILCHECK variable) for the
arrival of new mails. If there are any new e-mail messages and the user logs in, the message ‘you have
mail’ is displayed. The incoming mail can be viewed, answered, forwarded, saved, deleted or printed. The
address of the sender can also be saved in the address book.
An e-mail message when viewed, displays the header information and the message body. The header
information consists of the following-
The message body is displayed after the header information. The message body contains the text
message and attachments, if any.
If the mail address of the person to whom the mail is being sent does not exist, the e-mail is returned
with an error message.
$ mailx user1
will first prompt the user for the subject of the message. The message body is then entered. Pressing
[ctrl-d] indicates the end of the message. In some systems, mailx may ask the user for carbon copies to
be sent. If so, the mail address of the person to whom a carbon copy of the message is to be sent is
entered. Finally the mail is sent to user1’s mailbox.
mailx can also be used to send mails non-interactively with the -s option. For example,
The message body, mesg.txt is entererd separately using the vi editor and mailed to user1 non-interactively
with the –s option.
To receive mails using mailx, the command is used without any arguments. In the list of messages
displayed, the first column gives the message status. All unread old messages are marked with a U. All
new messages are marked with a N. The messages are numbered and the third column shows the login
92 Chapter 10 - Communication and Electronic Mail
name of the sender. The fourth column displays the date and time the message was received. The fifth
column gives the number of lines and characters in the message.
The ampersand (&) in the last line is the mail prompt. mailx now waits for the user’s instructions. A
help facility gives you the list of instructions that can be entered at the mail prompt. Some of the commands
are listed below:
command action
+ or [Enter] next message
- previous message
N go to message number
Z display next screenful of messages
display previous screenful of
z- messages
w filename save message without header
information to filename
sn filename
save message number n to file
dn delete message number n
un undo delete message number n
m user forward message to user
reply to sender of message number n
Rn
rn reply to sender and every user
who received the original
message
[ctrl-d] send a message
q quit mailx
!cmd run Unix command
Example
$ mailx
SCO OpenServer Mail Release 5.0 Type ? for help.
“/usr/spool/mail/sibns”: 2 messages 2 new
N 2 user1 Thu Nov 13 14:23 14/612 hello
>N 1 root Fri Jul 25 15:38 14/521 Welcome to sco5
&2
BSIT 43 UNIX and Shell Programming 93
Message 2:
From sibns Thu Nov 13 14:23:04 2003
Return-Path: sibns
Received: (from sibns@localhost) by sco5.sjce.ac.in (8.8.8/SCO5) id OAA00740 for sibns; Thu, 13
Nov 2003 14:23:04 +0530 (IST)
Message-Id: <200311130853.OAA00740@sco5.sjce.ac.in>
From: sibns@sco5.sjce.ac.in (sibns)
X-Mailer: SCO OpenServer Mail Release 5.0
To: sibns
Date: Thu, 13 Nov 2003 14:23:04 IST
Status: R
$ pine
To compose and send a message, the key C is pressed. The compose message screen appears as
shown below:
To :
Cc :
Attchmnt :
Subject :
--------Message Text --------
The mail address of the recipient is entered. The address of the person to whom a copy is to be sent
is also entered. Files can be attached to the mail message by entering the name of the file to be attached.
The message subject which is optional should be short. The body of the message is finally entered and
sent by pressing [ctrl-x] at the end of the message. A confirmation is asked to which a ‘Y’ or a ‘N’ is
entered. Pressing ‘Y’ will send the message, a copy of which is saved in the sent-mail file. [ine now
returns to the main menu.
In pine, a new user has three mail files – the inbox which holds messages received, sent-mail which
has copies of messages sent and saved-messages which stores the mails saved.
Pressing the I key in the main menu of pine will display the folder index, which is numbered list of
message headers of all messages received. The mail header has the following structure:
The first column shows a + or a blank. The + says that no carbon copies were sent to any other user.
The secon column gives the status of the message- N for new, A for answered and S for saved. The
remianing columns show the message number, the date , the name of the sender, the size of the message
and the subject.
To read a message, the message to be read is first selected by moving the cursor to the message
header. Once the cursor is positioned, typing the V key will open the message viewer and the body of the
message is displayed. To view an attachment the V key is pressed again which shows a list of items. The
first item will be the message body and the second will be the attachment, if there is any. The cursor is
moved to select the attachment and the V key is pressed yet again. The attachment opens and the text is
displayed which can be saved to a file by typing the S key followed by a file name. Pressing the E key
twice will exit both the attachment viewer and the attachment index and the user is returned to the
message body.
To reply to a message, in the message body, the R key is typed. The original message can be optionally
included in the reply. The compose message screen appears and the reply can be entered and message
sent by typing [ctrl-x].
Messages can be saved to the saved-messages file by typing the S key in the message text. The
saved-messages file is selected by default and it be optionall changed to any other file name.
Every pine screen has a help option and pressing the ? key will display the help screen. The help
screen can be scrolled forward and backward by the spacebar and the hyphen (-) key respectively.
10.6 CONCLUSION
In this chapter the online communication tools write and talk were discussed. E-mails and the structure
of a mail message and the functioning of the two character-based mailers mailx and pine function were
also discussed.
EXERCISES
1. What is the difference between write and talk ?
2. If a user is logged into more than one terminal, how do you send the user a message using write ?
3. Find a user who has an account on the system and send a message using maix.
BSIT 43 UNIX and Shell Programming 97
6. Using the redirection operator on the command line, mail the file mesg.txt to user1.
7. Include the subject ‘my message’ in the command line, in exercise no. 6.
Chapter 11
Simple Filters
C
onsider the following employee database of an organisation, from here onwards, you’ll be learning
the features of several UNIX command filters with reference to a file emp.lst. The following is
the emp.lst file of an XYZ institution.
$ cat emp.lst
This is a text file designed on fixed format containing employee records in XYZ institution. There are
10 lines, where each line has six fields separated from one another by the delimiter | (pipe). The details of
an employee are stored in one line. A person is identified by the emp-id, name, designation, department,
date of birth and salary (totally six fields in emp.lst database), as indicated by the fields (in the same
order). You’ll be using different UNIX filters (commands) on this file to see the powerful feature of
UNIX filters.
You can use the –n option to display the first ‘n’ number of lines.
100 Chapter 11 - Simple Filters
You can use the –n option to display last ‘n’ number of lines.
Example: $ tail -n 3 emp.lst Displays last 3 lines
You can also display lines from the beginning of the file instead of the end. The +count option allows
you to do that, where count represents the line number from where the selection should begin.
Note the use of the tee facility that saves the output in shortlist and also displays it on the terminal.
We can extract both columns and fields from this file with the cut command. Columns are specified with
–c option and fields with –f option.
T.N.Raju Professor
D.S.Raghu Lecturer
S.K.Anantha Asst.Prof.
M.P.Rajendra Sr.Lecturer
Mallu Lecturer
Note that there should be no whitespace in the column list. Moreover, cut uses a special form for
selecting a column from the beginning and up to the end of a line.
$ cut -c -4,6-23,25-37,59- shortlist column number must be in ascending
order.
The expression 59- indicates column number 59 to the end of the line. Similarly, -4 is the same as 1-4
i.e. from column numbers 1 to 4.
T.N.Raju |Professor
D.S.Raghu |Lecturer
S.K.Anantha |Asst.Prof.
M.P.Rajendra |Sr.Lecturer
Mallu |Lecturer
(Note: tee command is used to store the result in a file cutlist1 and also display the result on screen).
The | was escaped to prevent the shell from interpreting it as the pipeline character; alternatively, it can
also be quoted (-d “|”). To cut out fields numbered 1,4,5,6, and save the output in cutlist2, following is the
command.
$ cut -d “|” -f 1,4- shortlist > cutlist2 here | is quoted
1001|Information Science|14/06/61|30000
1004|Information Science|05/12/75|17000
1005|Information Science|20/07/63|24000
1009|Computer Science |13/03/66|20000
1002|Information Science|20/07/74|15000
cut can be used to extract the first word of a line by specifying the space as the delimiter. cut can be
used to extract user list from who command output. The following is the command.
$ who | cut -d “ “ -f1
root
snn
mallu
project
jayanthi
cut is a powerful text manipulator often used in combination with other commands or filters.
The original contents have been restored to some extent, except that the fields have different relative
locations, and pasting has taken place on whitespace. Like cut, paste also uses the tab as the default
delimiter, but you can specify one or more delimiters with –d:
$ paste -d “|” cutlist1 cutlist2
Joining Lines (-s) paste is more useful than you might think. Consider this address book that contains
details of three persons, with three lines for each:
$ cat addressbook
Mallu
Mallu@yahoo.com
2546131
Maharaj
Maharaj@rediffmail.com
2512657
Nagendra
san@sjce.ac.in
2121134
The –s option joins lines in the same way vi’s J commands does. Using this option on this file (with
paste –s addressbook) would join all these nine lines to form a single line. This probably won’t be of much
use, but if we use the –d option with multiple delimiters (say, the string ||\n), then we can join three lines at
a time. The delimiters then are used in a circular manner; the first and second lines would be joined with
the | as delimiter, and the same would be true for the second and third line. The third and fourth line would
be separated by a new line. After the list is exhausted it is reused. The following is the command.
Mallu|Mallu@yahoo.com|2546131
Maharaj|Maharaj@rediffmail.com|2512657
Nagendra|san@yahoo.com|2121134
$ sort shortlist
By default, sort reorders line in ASCII collating sequence i.e. the whitespace first (blanks), then
numerals, uppercase letters and finally lowercase letters. This default sorting sequence can be altered by
using certain options. You can sort on one or more keys (fields).
Sorting on primary key (-k): Let’s now use the –k option to sort on the second field (i.e name). The
option should be –k 2:
$ sort -t “|” -k 2 shortlist
The sorting order can be reversed with –r (reverse) option. The following sequence reverses a previous
sorting order:
$ sort -t “|” -r -k 2 shortlist
Sorting on secondary key: You can sort on more than one key i.e. you can provide a secondary key
to sort. If the primary key is the third field, and the secondary key is the second field, then you need to
specify for every –k option, where sort ends. This is done in this way:
$ sort -t “|” -k 3,3 -k 2,2 shortlist
This sorts the file by designation and name. –k 3,3 indicates that sorts starts on the third field and ends
on the same field.
Sorting on columns: You can also specify a character position within a field to be the beginning of
sort. If you are to sort the file according to year of birth, then you need to sort on the seventh and eight
columns position within the fifth field.
$ sort -t “|” -k 5.7,5.8 shortlist
The –k option also uses the form –k m.n, where ‘m’ is the character position in the mth field. So, 5.7,
5.8 mean that sorting starts on column 7 of the fifth field and ends on column 8.
Numeric sort (-n): When sort acts on numerals, strange things can happen. When you sort a file
containing only numbers, you get a curious result:
$ sort numfile
10
2
27
4
This is probably not what you expected, but the ASCII collating sequence places 1 above 2, and 2
above 4. That’s why to 10 proceeded 2 and 27 proceeded 4. This can be overridden by the –n (numeric)
option.
$ sort -n numfile
2
4
10
27
Other sort options: Even though sort’s output can be redirected to a file using redirection operators
(> or >>), we can use its –o option to specify the output filename. Curiously enough, the input and output
filenames can even be the same.
106 Chapter 11 - Simple Filters
To check whether the file has actually been sorted in the default order, use the –c (check) option.
You can also add the –k option to the above to check whether a specific field is sorted.
When sort is used with multiple filenames as arguments, it concatenates them and sort them collectively.
The –m (merge) option can merge two or more files that are sorted individually.
Option Description
-t char Uses delimiter char to identify fields
-k n Sorts on nth field
-k m,n Starts sort on mth field and ends sort on nth field
-k m.n Starts sort on nth column of mth field
-n Sorts numerically
-r Reverses sort order
-m list Merges sorted files in list
-c Checks if file is sorted
-o filename Places output in filename.
Table 11.1: Options used by sort
$ cat dept.lst
01|Civil Engineering
02|Mechanical Engineering
02|Mechanical Engineering
03|Electronics & Communication
04|Computer Science
04|Computer Science
05|Information Science
05|Information Science
BSIT 43 UNIX and Shell Programming 107
uniq simply fetches one copy of each line and writes it to the standard output.
$ uniq dept.lst
01|Civil Engineering
02|Mechanical Engineering
03|Electronics & Communication
04|Computer Science
05|Information Science
Since uniq requires a sorted file as input, the general procedure is to sort file and pipe its output to uniq.
The following pipeline also produces the same output, except that the output is saved in a file uniqlist:
$ sort dept.lst | uniq - uniqlist
uniq is indeed unique; if provided with two filenames as arguments, uniq will read the first file and
write its output to the second. Here, it reads from the standard input and writes to uniqlist
Note that tr takes input only from the standard input; it doesn’t take a filename as argument. By
default, it translates each character in expression1 to its mapped counterpart in expression2. The first
character in the first expression is replaced with the first character in the second expression, and similarly
for the other characters.
Here is a command that converts all the upper case letters in a file dept.lst into lower case letters. The
following is the command:
108 Chapter 11 - Simple Filters
01|civil Engineering
02|mechanical engineering
02|mechanical engineering
03|electronics & communication
04|computer science
Note that the lengths of the two expressions should be equal. If they are not, the longer expression will
have unmapped characters. Single quotes are used here because no variable evaluation or command
substitution is involved.
It’s generally safe to quote the pattern; if the string contains only one word quoting is not required
where as if the string contains more than one word quoting is mandatory.
Deleting Lines (-v) grep can play an inverse role too; the –v (inverse) option selects all except lines
containing the pattern. Thus, you can create a file otherlist containing all but Lecturer.
The above UNIX command stores the result in a file called otherlist. The content of the file is as
follows:
Displaying Line Numbers (-n) The –n (numbers) option displays the line numbers containing the pattern,
along with the lines:
$ grep -n “Lecturer” emp.lst
2:1004|D.S.Raghu |Lecturer |Information Science |05/12/75|17000
4:1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
5:1002|Mallu |Lecturer |Information Science |20/07/74|15000
8:1007|Sanjay |Lecturer |Civil |19/04/73|15000
9:1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
10:1003|B.S.Ashok |Lecturer |Information Science |01/01/78|15000
The line numbers are shown at the beginning of each line, separated from the actual line by a :.
Counting Lines Containing Pattern (-c) How many Lecturers are there in the file? The –c (count)
option counts the number of lines containing the pattern (which is not the same as number of occurrences).
The following shows that there are six of them:
$ grep -c “Lecturer” emp.lst
6
Matching Multiple Patterns (-e) With the –e option, you can match the two Mahanand by using grep
like this:
$ grep -e “mallu” -e “MALLU” emp.lst
Note: If the emp.lst file contains two mahanand’s like above pattern (Mahanad and
MAHANAND) the grep command displays those two lines also.
110 Chapter 11 - Simple Filters
Option Description
-i Ignores case for matching
-v Doesn’t display lines matching expression
-n Displays line numbers along with lines
-c Displays count of number of occurrences
-e exp Specifies expression exp with this option. Can use multiple times.
Also used for matching expression beginning with a hyphen.
11.11 CONCLUSION
This chapter presented some of the commonly used filters available in the UNIX system. You can use
above filters in pipelines to perform tasks that apparently seem to difficult to achieve by conventional
means. Finally you have seen grep command that is used to search for a pattern in a file with different
options.
11.12 EXERCISES
1. Use head and tail to select lines 5 to 10 of a file.
5. How will you find out the number of times the character ? occurs in a file?
6. Mail a sorted list of users currently logged in to root, taking account of the possibility that a user may be
logged in more than once.
BSIT 43 UNIX and Shell Programming 111
Chapter 12
Shell Programming
U
ntil now, you have been giving commands to the UNIX shell by typing them on the keyboard.
When used this way, the shell is said to be a command interpreter. The shell can also be used as
a high –level programming language. Instead of entering commands one at a time in response to
the shell prompt, you can put a number of commands in a file, to be executed all at once by the shell. A
program consisting of shell commands is called a shell script .
cal
date
who
The first line in this file begins with a # symbol, which indicates a comment line. Anything following the
#, up to the end of the line, is ignored by the shell. The remaining three lines are shell commands: the first
produces a calendar for the current month, the second gives the current date and time, and the third lists
the users currently logged onto your system.
We can get the Bourne Shell (sh) to run these commands by typing
$ sh < commands
The redirection operator (<) tells the shell to read from the file commands instead of from the standard
input. It turns out, however, that the redirection symbol is not really needed in this case. Thus, you can also
run the commands file by typing
$ sh commands
Is there any way to set up commands so that you can run it without explicitly invoking the shell? In
other words, can you run commands without first typing sh? The answer is yes, but you first have to make
the file executable. The chmod utility does this:
$ chmod u+x commands
The argument u+x tells the file name commands that assign execute permission to owner of the file
commands. Now all you need do is enter the file name
$ commands
If your search path is set up to include the current directory, the shell will run the commands in the file.
If it is not, the shell will complain that it cannot find the file you want it to execute:
$ ./commands
12.2 SUBSHELLS
When you tell the shell to run a script such as the commands file, your login shell actually calls up
another shell process to run the script. (Remember, the shell is just another program, and UNIX can run
more than one program at a time.) The parent shell waits for its child to finish, then takes over and gives
you a prompt:
$
Incidentally, a subshell can be different from its parent shell. For example, you can have ksh, csh,
tcsh or bash as your login shell, but use sh to run your shell scripts. Many users in fact do this. When it
comes time to run a script, the login shell simply calls up sh as a subshell to do the job.
We will always use sh for running shell scripts. To make sure that sh is used, we will include the
following line at the top of each shell script file.
#!/bin/sh
BSIT 43 UNIX and Shell Programming 113
#!/bin/sh
# A simple shell script
cal
date
who
l Variables
l Input/output function
l Arithmetic operations
l Conditional expressions
l Selection structures
l Repetition structures
12.4 VARIABLES
There are three types of variables commonly used in Bourne Shell scripts:
l Environment Variables: Sometimes called special shell variables, keyword variables, predefined
shell variables, or standard shell variables, they are used to tailor the operating environment to
suit your needs. Examples include PATH, TERM, HOME, and MAIL.
l Positional Parameters: These are used by the shell to store the values of command-line
arguments.
114 Chapter 12 - Shell Programming
Of these, the environment variables and user-defined variables have been introduced already. The
positional parameters, however, are new, and since they are very useful in shell programming, we will
examine them in some detail now.
The positional parameters are also called read-only variables, or automatic variables, because the
shell sets them for you automatically. They “capture” the values of the command-line arguments that are
to be used by a shell script. The positional parameters are numbered 0, 1, 2, 3,…,9. To illustrate their use,
consider the following shell script, and assume that it is contained in an executable file named display.args:
#!/bin/sh
# Illustrate the use of positional parameters
echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
Suppose you run the script by typing the command line
The shell stores the name of the command “display.args” in the parameters $0: it puts the argument
“I in the parameter $1; it puts “LOVE” in the parameter $2, and “INDIA.” in parameter $3. Since that
takes care of all the arguments, the rest of the parameters are left empty. Then the script prints the
contents of the variables:
#!/bin/sh
# Illustrate the use of positional parameters
echo $*
The parameter $# contains the number of arguments that the user typed. We can modify the script
display.args once again to use this parameter:
#!/bin/sh
# Illustrate the use of positional parameters
echo You typed $# arguments : The arguments are: $*
You typed 6 arguments: The arguments are : Welcome to the world of UNIX
#!/bin/sh
# Illustrate the use of positional parameters, user-defined
# Variables and the read command.
echo ‘What is your name?’
read name
echo “Well, $name, you typed $# arguments:”
echo $*
In this script, name is a user-defined variable. The read command obtains the user’s response and
stores it in name. With this modification, the script display.args works something like this:
#!/bin/sh
# Demonstrate the set command
set ‘date‘
echo “Time: $4 $5”
echo “Day: $1”
echo “Date: $3 $2 $6”
Assuming that setdate has been made executable with the chmod command, we can run the script by
typing the command
$ setdate
set ‘date‘
The backquotes run the date command, which produces output something like this:
This does not appear on the screen. Instead, the set command catches the output and stores it in the
positional parameters $1 through $6:
$1 contains Fri
$2 Contains Aug
$3 contains 20
$4 contains 10:56:08
$5 contains EST
$6 contains 2004
in shell scripts.) To use it in a shell script, you simply surround the expression with backquotes. For
example, let’s write a simple script called add that adds two numbers typed as arguments:
# /bin/sh
# Shell Script to Add two numbers
sum=‘expr $1 + $2‘
echo $sum
Here we defined a variable sum to hold the result of the operation. (Note the spaces around the plus
sign, but not around the equals sign). To run this script, we might type the following line: (assume the script
add is executable)
$ add 4 3
The first argument (4) is stored in $1, and the second (3) is stored in $2. The expr utility then adds
these quantities and stores the result in sum. Finally, the contents of sum are echoed on the screen:
7
$
The expr command only works on integers (i.e., whole numbers). It can perform addition (+), subtraction
(-), multiplication (*), integer division (/), and integer remainder (%).
l while
l until
118 Chapter 12 - Shell Programming
if conditional expression
then
command(s)
fi
When the shell encounters a structure such as this, it first checks to see whether the conditional
expression is true. If so, the shell runs any commands that it finds between the then and the fi (which is
just if spelled backwards). If the conditional expression is not true, the shell skips the commands between
then and fi. Here is an example of a shell script that uses a simple if statement:
#!/bin/sh
set ‘date‘
if test $1 = Sun
then
fi
Here we have used the test command in our conditional expression. The expression
test $1 = Sun
checks to see if the parameter $1 contains Sun; if it does, the test command reports that the condition
is true, and the message is printed.
The test command can carryout a variety of tests; some of the arguments it takes are listed in Table
12-1.
BSIT 43 UNIX and Shell Programming 119
#!/bin/sh
set ‘date‘
if test $1 = Fri
then
echo “Thank goodness it’s Friday!”
elif test $1 = Sat | | test $1 = Sun
then
echo “You should not work on weekends.”
echo “Log off and go home.”
else
echo “It is not yet the weekend.”
echo “Get to work!”
fi
Here, the first conditional expression is tested to see if the day is a Friday. If it is, the message “Thank
goodness it’s Friday!” is printed, and the shell script is finished. If not, the second conditional expression
120 Chapter 12 - Shell Programming
is tested. Note that we have used the OR operator ( | | ) in this expression to test whether the day is a
Saturday or Sunday, in which case the second set of message will printed, and the script is finished.
Otherwise, the third set of messages is printed.
We could make even more elaborate selection structures by including more elif clauses. The important
thing to note about such structures is that only one of the alternatives may be chosen; as soon as one is,
the remaining choices are skipped.
The case statement compares word with pattern; if they match, the shell runs the command(s) on the
first line. Otherwise, the shell checks the remaining patterns, one by one, until it finds one that matches
the word; it then runs the command(s) on that line.
#!/bin/sh
set ‘date‘
case $1 in
Fri) echo “Thank goodness it’s Friday!”;;
Sat | Sun) echo “You should not work on week-ends”;
echo “Log off and go home!”;;
*) echo “It is not yet the weekend.”;
echo “Get to work!”;;
esac
BSIT 43 UNIX and Shell Programming 121
There are a few points to note about this structure. First, commands are separated by semicolons (;)
and the end of a group of commands is indicated by two semicolons (;;) The OR symbol used in case
statements is a single vertical line (|), not the double vertical lines ( | | ) used in the if statement. The last
pattern (*) marks the default case, which is selected if no other pattern is matched.
command(s)
done
Here is a simple application of the for loop:
#!/bin/sh
for name in $*
do
ps –u $name
done
Each time through the for loop, the user-defined variable name takes on the value of the next argument
in the list $*. This is then used as the argument to the ps –u command (used to see the PID of the
specified user). Assuming this script is contained in the executable file psall, it would be run by typing the
name of the file, followed by the login names you wish to see the PID status:
while condition
do
command(s)
done
As long as the condition is true, the commands between the do and the done are executed. Here is an
example of a shell script that uses the expr utility with the while loop to echo the keyboard entry five times:
#!/bin/sh
# Print a message five times
count=5
while test $count -gt 0
do
echo $*
count=‘expr $count – l‘
done
do
echo $*
count=‘expr $count – 1‘
done
12.15 CONCLUSION
This chapter presented the essential programming constructs offered by the shell. If you have written
C programs, you can realize that the domain of shell programming is quite different from that of any high-
level language. Shell scripts are ideal for integrating your existing applications. You should now be able to
write scripts up to moderate complexity. If you are the system administrator, you’ll need the services of
shell scripting to automate the routine functions, using cron scheduling when needed.
12.16 EXERCISES
1. Define each of the following terms:
loop
2. Develop a script logic that allows only kumar and sachin (login names) to execute a program.
3. Develop a script logic that executes script only from terminal tty01 and tty02.
4. Write a shell script that accepts one or more filenames as arguments, and converts them all to uppercase provided
they exist in the current directory.
5. Write a shell script that accepts two directory names, foo1 and foo2, and deletes those files in foo2 which are
identical to their namesakes in foo1.
6. Write a shell script that lists files by modification time when called with lm and by access time called with la. By
default, the script should show the listing of all files in the current directory.
7. Write a shell script to display the processes in the system every 30 seconds five times
Chapter 13
T
his chapter presents a number of shell scripts that make use of the programming features discussed
in the previous chapter. Before listing the code for each script, we will show a summary (in the
form of a man page) describing the script.
NAME
chex - change a file to be executable
SYNOPSIS
chex filename
DESCRIPTION
This is the outline for chex:
Select sh
Apply chmod u+x to file named as argument ($l)
Inform user that file is executable
Use ls -d to show the file modes
The shell script itself is shown in Listing 13-1B. Take a moment to study the outline, then create the
script:
1. Use the text editor to create the script file. Open a file named chex and enter the script
shown in the listing. Write and quit the file when finished.
2. Make the chex file executable. This requires you to change the access privileges on the file.
One way to do this is to tell the shell to run chex on itself. Try this command line:
$ sh chex chex
#!/bin/sh
# Make a file executable
chmod u+x $1
echo $1 is now executable
ls -l $1
This tells the shell to run chex, taking chex itself as the argument. The result is that chex makes itself
executable. The output from this command will look some-thing like this:
Now you can use chex to change the protections on other files.
5 17 84 chex
$
The output tells us that there are 5 lines, 17 words, and 84 characters in the file chex. This can be
very useful information, but it would be a bit more convenient to use if the output were labeled. Listing 13-
2A summarizes the script; Listing 13-2B shows the actual shell script.
126 Chapter 13 - Developing Shell Scripts
NAME
mywc-labeled word count
SYNOPSIS
mywc filename
DESCRIPTION
This is the outline for mywc:
Select sh
Run wc on $1 and capture output with set
Print the filename ($4)
Print the number of lines ($l)
Print the number of words ($2)
Print the number of characters ($3)
1. Use the text editor to create the script file. Open a file named mywc and enter the script
shown in Listing 13-2B. Write and quit the file when finished.
#!/bi n/sh
# Label the output from wc
set `wc $1`
echo "File: $4"
echo "Lines: $1"
echo "Words: $2"
echo "Characters: $3"
$ chex mywc
3. Test the new script. You might run mywc on the file chex:
$ mywc chex
File: chex
Lines: 5
Words: 17
Characters: 84
Listing 13-3A summarizes the script; Listing 13-3B shows the script itself. After studying the listings,
create the script:
1. Use the text editor to create the script file. Open a file named del and enter the script
shown in Listing 13-3B. Write and quit the file when finished.
$ chex del
3. Test the new script. Use del on a file you no longer need.
128 Chapter 13 - Developing Shell Scripts
NAME
del - delete a file interactively
SYNOPSIS
del filename
DESCRIPTION
This is the outline for del:
Select sh
Get the filename from the command line ($l)
#!/bin/sh
# Delete a file interactively
filename=$l
if test ! -f $filename
then
echo "There is no file \"$filename\"."
else
echo "Do you want to delete \"$filename\"?"
read choice
if test $choice = y
then
rm $filename
echo "\"$filename\" deleted."
else
echo "\"$filename\" not deleted."
fi
fi
NAME
tickle - a daily reminder service
SYNOPSIS
tickle
DESCRIPTION
Here is the outline for tickle:
Select sh
Use set to capture the output from date
Print a message
Check day ($l) and print an appropriate message
#!/bin/sh
# A daily reminder service
set `date`
echo "Remember for today:"
case $1 in
Mon) echo "Plan the week.";;
Tue) echo "Take clothes to the cleaners.";;
Wed) echo "Attend group meeting.";;
Thu) echo "Make plans for the weekend.";
echo "Pick up clothes at the cleaners.";;
Fri) echo "Answer e-mail.";;
Sat) echo"You should not be here working.";
echo"Finish your work and log off.";;
Sun) echo "Call Grandma and Grandpa.";;
esac
1. Use the text editor to create the script file. Open a file named tickle and enter the
script shown in Listing 31-4B. Write and quit the file when finished.
$ chex tickle
$ tickle
This should print out the appropriate message for the day.
1. Use the text editor to create the script file. Open a file named echo.sh and enter the script
shown in Listing 13-5B. Write and quit the file when finished.
$ chex echo.sh
NAME
echo.sh - echo the arguments ‘n’ times
SYNOPSIS
echo.sh n arguments(s)
DESCRIPTION
Here is the outline for echo.sh:
Select sh
Get $count from the command line ($l)
Use shift to get rid of the first argument
Get the message from the command line ($*)
#!/bin/sh
# Echo a line ‘n’ times
count=$l
shift
message=$*
The first argument (5) will be read in (stored in $1), then the rest of the command line will be repeated
five times:
132 Chapter 13 - Developing Shell Scripts
Play it again,Sachin.
13.6 EXERCISES
1. Write a shell script modch.sh that uses chmod to change the access permissions on a file so that only the owner
may read, write, or execute it. Be sure to label the output to show what was done to the file.
2. Modify the del script so that it detects whether the user has specified a directory to be deleted, in which case
the script should call the rmdir command. (Hint: use test with the -d option to test for a directory.)
3. Write shell scipt lshead.sh that uses ls –l command, to display the heading
for ls –l output.
m m m