Sie sind auf Seite 1von 132

Chapter 1

Introduction to UNIX and its


Background

1.1 THE OPERATING SYSTEM

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 multiprocessing : Supports running a program on more than one CPU.


l multitasking : Allows more than one program to run at the same time.

l multithreading : Allows different parts of a single program to run simultaneously

l real time: Responds to input instantly. General-purpose operating systems, such as DOS and
Unix, are not real-time.

BSIT 43 UNIX and Shell Programming 1


2 Chapter 1 - Introduction to UNIX and its Background

1.2 THE UNIX OPERATING SYSTEM


Unix is an increasingly popular operating system. Traditionally used on minicomputers and workstations
in the academic community, Unix is now available on personal computers, and the business community is
choosing Unix for its openness. Previous PC and mainframe users are now looking to Unix as their
operating system solution.

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.

1.3 THE ORIGINS OF UNIX


In the mid-1960s, among others, AT&T Bell Laboratories was participating in an effort to develop a
new operating system called Multics. Multics was intended to supply large-scale computing services as a
utility. In 1969, Bell Labs withdrew from the Multics project. Ken Thompson, Dennis Ritchie, and others
developed and simulated an initial design for a file system that later evolved into the UNIX file system. A
version of the system was developed to take advantage of a PDP-7 computer that was available to the
group. An early project that helped lead to the success of Unix was its deployment to do text processing
for the patent department at AT&T. This project moved Unix to the PDP-11 and resulted in a system
known for its small size. In 1973, C programming language was developed on and for Unix, and the Unix
operating system itself was rewritten into C. This enabled Unix to become the open portable system it is
today.
BSIT 43 UNIX and Shell Programming 3

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.

1.4 HOW TO WORK WITH UNIX? A BRIEF SESSION


Every user has to login with a username and password. The system administrator is responsible for
creating user accounts. While assigning user accounts, the system administrator takes care to see that:
l the account name is atleast three characters long and not more than eight characters long.

l no two accounts have the same name.

l the account name chosen is preferably related to the user’s name.

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:

1.5 ARCHITECTURE OF UNIX


UNIX, like other operating systems, is a layer between the hardware and the applications that run on
the computer. It has functions that manage the hardware and functions that manage executing applications.
So what’s the difference between UNIX and any other operating system? Basically two things: internal
implementation and the interface that is seen and used by users.

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.

1.6 UNIX FEATURES


UNIX goes beyond the traditional operating system by providing a standard set of libraries and
6 Chapter 1 - Introduction to UNIX and its Background

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?

2. How is Unix different from other operating systems ?

3. What is the function of the Unix kernel ?

4. What is the meaning of multitasking?

5. What is the importance of a user name and a password ?


BSIT 43 UNIX and Shell Programming 7

Chapter 2

Understanding the UNIX Commands

2.1 LOOKING FOR A UNIX COMMANDS

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:.

2.2 INTERNAL AND EXTERNAL COMMANDS


Some commands in Unix are internal, built into the shell. For example, the cd command is built-in.

BSIT 43 UNIX and Shell Programming 7


8 Chapter 2 - Understanding the UNIX Commands

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.

2.3 STRUCTURE OF UNIX COMMANDS


The basic structure of a Unix command is:

command [options,...] [arguments,...]

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.

2.4 FLEXIBILITY OF COMMAND USAGE


A number of commands can be entered in a single command line. The commands are separated by
semicolons (;). When the shell finds a semicolon in the command line, it executes the commands on either
side of the semicolon separately. The semicolon in this context is called a metacharacter because it has a
special meaning to the shell.

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

2.5 USING MANUAL PAGES ON-LINE


Online manual pages are available for most commands and applications on the system. Using the man
command followed by the name of the command accesses the man page for a command. For example,
to find out more about how to use man the following command is typed -

$ 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.

The following keys are used for navigating and searching:

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

uucp, uulog, uuname (C) - UNIX-to-UNIX system copy

uuname (C) - list names of systems known to uucp


uname (C) - print the name of the operating system

uucp, uulog, uuname (C) - UNIX-to-UNIX system copy

uuname (C) - list names of systems known to uucp


10 Chapter 2 - Understanding the UNIX Commands

2.6 WHEN THINGS GO WRONG


All terminals and keyboards are not similar and the keyboard operation depends on the terminal settings.
The terminal settings are found in the TERM variable. So when commands do not work as expected, the
following keystrokes can be tried.

keys to press what happens


[ctrl-h] erases text
[ctrl-c] or [Delete] Interrupts a command
[ctrl-d] Terminates a login session
[ctrl-s] stops screen scrolling
[ctrl-q] Resumes scrolling
[ctrl-u] kills a command line without executing it
[ctrl-\] kills a command being executed creates a
core file
[ctrl-z] Suspends a command execution & returns
to shell prompt
[ctrl-j] or [ctrl-m] same as enter

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 ?

2. How does the type command work ?

3. How do you find out whether a command is internal or external ?

4. What is a secondary prompt ?

5. How do you stop and resume the scrolling of the screen display ?

6. How do you get one-line descriptions of any command in Unix?


BSIT 43 UNIX and Shell Programming 11

Chapter 3

General-Purpose Utilities

3.1 DISPLAYING CALENDAR


cal - print a calendar

Syntax:
cal [ [ month ] year ]

where year is a number between 1 and 9999 and month is a number between 1 and 12 :

Example 1: No arguments are specified


The current, previous, and following months are printed, along with the current date and time.

$ cal

Example 2: Only month is specified

$ cal jan

BSIT 43 UNIX and Shell Programming 11


12 Chapter 3 - General-Purpose Utilities

$ cal 1

Example 3: Calendar for the year 2005 is displayed

$ cal 2005
BSIT 43 UNIX and Shell Programming 13

3.2 TO DISPLAY SYSTEM DATE AND TIME: DATE


date - to see the date and time S
yntax: date [ +format ]
The format specifier is preceded by a + followed by a %. Some of the format specifiers are as
follows –

Format What it specifies

+%m specifies the month number (1 to 12)

+%h specifies the abbreviated month (Jan –


Dec)

+%B specifies the full month name

+%d specifies the day of the month (1 to 31)

+%y specifies the year (2 digits)

+%H, +%M specify the hour, minute and seconds


and +%S

+%D specifies the date as mm/dd/yy

+%T specifies the time as hh:mm:ss

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

3.3 DISPLAYING MESSAGE: echo


echo - writes its arguments separated by blanks and terminated by a new line on the standard output.
14 Chapter 3 - General-Purpose Utilities

Syntax:
echo [-n] text
The -n option prints a line without the newline.

Example:

$ echo Hello
Hello

3.4 AN ALTERNATIVE TO ECHO: printf


Write formatted output
Syntax

printf format [ argument]

printf formats the given arguments according to the format string and writes them to the standard
output.

Example 1: Returns hello to the screen.

$ printf “hello”
hello

Example 2: Using a format string

$ printf “%s %0.2f \n” Rupees 550


Rupees 550.00

3.5 THE CALCULATOR: bc


bc - invoke a calculator

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

Simple arithmetic expressions can be formed with operators that include:


+-*/%^

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 type bc at the command prompt and press ENTER


10+20 type an arithmetic expression and press ENTER

30 the answer is displayed

^d press ^d to exit bc and return to the Unix prompt


While computing, bc truncates the decimal portion in the result. To enable floating-point computation,
the number of digits of precision is set.

Example 2: Enable floating point computation

$ bc
scale=5 specify the number of digits after the decimal point using scale

10/3

3.33333
^d

3.6 RECORDING YOUR SESSION: script


script - makes a typescript of a terminal session
syntax:
16 Chapter 3 - General-Purpose Utilities

script [-a] [file]

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

$ exit exits a script session.

The contents of the script file can be viewed using cat after ending a script session.

3.7 CHANGING YOUR PASSWORD: passwd


passwd - change login password
The passwd command is used to change a login password. The login password is one of the most
important defenses against security breaches. A password should be such that it is easily remembered by
the user and difficult to guess by others.

The general guidelines to form psswords are:


l A password should be atleast six characters long .

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:

3.8 WHO ARE THE USERS LOGGED-IN?: who


who - list the users logged on the systen

Syntax

Who
Who am i

Examples:

Lists the user’s name, terminal line and login date and time
$ who

sibns ttyp0 Dec 29 13:08

fysr ttyp1 Dec 29 13:51


jcepay ttyp2 Dec 29 13:54

$ who am i

sibns ttyp0 Dec 29 13:08


Lists only the names and number of users logged in

$ who -q

sibns fysr jcepay


# users=3
18 Chapter 3 - General-Purpose Utilities

3.9 KNOW YOUR MACHINE’S NAME: uname


uname - displays the name of the operating system
Syntax

uname [ options ]

Examples:
Displays all the information

$ uname -a
SCO_SV sco5 3.2 5.0.5 i386

Displays the machine’s node name in the communications network.


$uname -n
sco5

Displays the operating system release.


$ uname -r
3.2

Displays the name of the operating system

$ uname -s
SCO_SV

Displays the operating system version

$ 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

3.10 KNOWING YOUR TERMINAL: tty


tty – display the terminal’s name

Syntax

tty
Example

The terminal file name which is present in the /dev directory is displayed

$ tty
/dev/ttyp0

3.11 DISPLAYING AND SETTING TERMINAL CHARACTER:


stty
stty - display and set terminal modes

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

ixon ixany -ixoff


isig icanon -xcase echo -echoe echok -echonl -noflsh
-iexten -tostop -xclude
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel
-isscancode

To turn off keyboard input

$ stty –echo
To restore keyboard input

$ stty echo

To erase character while doing a backspace


$ stty echoe

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 intr \^c


To set the terminal characteristics to work with most of the terminals

$ stty sane

3.12 LOCK YOUR TERMINAL: lock


lock — lock a user’s terminal

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

3.13 CHECK YOUR SPELLINGS: spell and ispell


Spell - find spelling errors

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:

List the misspelt words in the file infile


$ spell infile

ispell - find spelling errors

Syntax
ispell filename
22 Chapter 3 - General-Purpose Utilities

where filename is the name of the file to be checked.

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.

The advantages of using ispell are as follows:

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

R replace the misspelled word completely

Spacebar accept the word this time only

A accept the word for the rest of this ispell


session

I accept the word, capitalized as it is in the


file, and update private dictionary

u Accept the word, and add a lowercase


version to the private dictionary

0, 1, ... n Replace with the suggested word


corresponding to that number.

l (the letter) Look up words in system dictionary

x Write the rest of this file, ignoring


misspellings, and start the next file.
BSIT 43 UNIX and Shell Programming 23

q Exit immediately and leave the file


unchanged

! Escape to the Unix shell.

Ctrl-l (the letter) Redraw the screen.

Ctrl-z Suspend Ispell

? Show the help screen.

ispell commands are not case sensitive (commands can be typed either in lowercase or uppercase);.

3.14 TAMING THE CURSOR: tput


tput - query the terminfo database

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

3. Display the current date in the mm/dd/yy format.

4. Use bc to divide 35 by 9. Set the scale to 2.

5. Record your login session into a file.

6. List out the users logged into the system.

7. Display your terminal filename.

8. Display the release version of your operating system.

9. How do you check a file for spelling errors.

10. How do you lock your terminal for 20 minutes ?

11. How do you check your terminal settings ?


BSIT 43 UNIX and Shell Programming 25

Chapter 4

The File System

4.1 CONCEPT OF A UNIX FILE

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.

BSIT 43 UNIX and Shell Programming 25


26 Chapter 4 - The File System

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.

4.2 WHAT’S IN A FILE NAME?


A UNIX filename can be upto 255 characters long and may or may not have extensions. Filenames
can be a combination of any of the following characters-

1. Upper case letters (A to Z)

2. Lower case letters (a to z)

3. Numbers (0 to 9)

4. period (.), comma (,) and underscore (_)

The following characters should be avoided while naming files:

& * \ | ( ) [ ] { } $ < > ? ‘ “ / ; ^ ! % ~ ` #

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.

4.3 THE PARENT-CHILD RELATIONSHIP


In UNIX all files are related to one another and are organized in a hierarchical (inverted tree) structure.
This is called the UNIX file system and at the very top of the tree is the root directory, named “/” which
serves as the reference point for all the files. The root directory has a number of subdirectories, which in
turn can have subdirectories and files. Therefore, every file except the root will have a parent. A sample
Unix file system tree:
BSIT 43 UNIX and Shell Programming 27

bin dev users tmp

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

4.4 CHECKING YOUR PRESENT WORKING DIRECTORY: pwd


In UNIX, the user’s location in the filesystem hierarchy is known as the “current working directory.”
When the user logs in, the system places the user automatically in the user’s “home directory.” The
location or the absolute pathname of the home directory is given by the command pwd which stands for
“print working directory.”

Example:
$ pwd
/users/user1

4.5 CHANGING THE CURRENT DIRECTORY: cd


To change the location in the file system hierarchy, the cd (change directory) command is used
followed by an argument defining the destination location. The argument can be either an absolute path to
the destination, or a relative path
28 Chapter 4 - The File System

Example: change to cprog directory from user1

$ cd cprog
$ pwd

/users/user1/cprog

Example: cd without arguments returns to the user’s home directory


$ cd
$ pwd
/users/user1/cprog

4.6 CREATING DIRECTORIES: mkdir


The Unix mkdir command is used to make directories. The basic syntax is mkdir directoryname.
Example: create a subdirectory textfiles for user1
$ mkdir textfiles
Example: create a directory set1 in the subdirectory cprog of user1
$ mkdir cprog/set1

4.7 REMOVING DIRECTORIES: rmdir


The UNIX rmdir command removes a directory from the file system tree. The rmdir command does
not work unless the directory to be removed is completely empty. A directory can be removed only if it
is hierarchically below the working directory.
Example: remove the directory textfiles which should be empty
$ rmdir textfiles
Example: remove the directory set1 from the directory cprog
$ rmdir cprog/set1

4.8 ABSOLUTE PATHNAMES


A pathname gives the file’s position in the file system. An absolute or full pathname specifies the
location of a file in relation to the top of the file system which is the / (root).
BSIT 43 UNIX and Shell Programming 29

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.

4.9 RELATIVE PATHNAMES


A relative pathname specifies a file in relation to the current directory and hence the relative pathname
depends on the current directory. A relative pathname starts from the current directory. In a relative
pathname, a single dot ( . ) represents the current working directory and two dots ( .. ) represent the
parent of the current working directory.
For files or directories in the current working directory, the relative path of a file or directory is the file
name or directory name itself. The parent of user1 is users and the relative pathname of the parent is ..
(double dot) and the relative pathname of the root directory is ../.. (double dot/double dot).

Example
Change to the directory set1 which is in cprog located in the working directory.

$ cd cprog/set1

Move up the file system tree by using the .. (double dot)


$ pwd
/user/user1/cprog/set1
$ cd ..
$ pwd
/user/user1/cprog
Any number of sets of .. (double dot) with a / (slash) can be used

$ pwd
/user/user1/cprog
$ cd ../..
$ pwd
/user
30 Chapter 4 - The File System

4.10 LISTING DIRECTORY CONTENTS: ls


The ls command shows the contents of a directory, and a view of basic information (like size, ownership,
and access permissions) about files and directories. The ls command has numerous options and a complete
listing can be obtained from the manual page on ls (by typing man ls). The ls command also accepts one
or more arguments. The arguments can be directories, or files.

Some of the commonly used options are listed below.

option action

-1 one filename in each line

-a all files including those beginning with a dot (.),


current directory (.) and directory above (..)

-A all files including those beginning with a dot


(.).does not list current directory (.) and directory
above (..)

-d if an argument is a directory, lists only its name


(not its contents)

-F marks directories with a /, executables with a * and


symbolic links with a @

-I shows the inode number

-l lists in long format, giving mode, number of links,


owner, group, size in bytes, the time that each file
was last modified.

-p puts a slash (/) after each directory.

-r sorts the filenames in the reverse order

-R recursive list

-t sorts by time modified (latest first)

-u sorts by last access time

-x lists files in columns with entries sorted across


BSIT 43 UNIX and Shell Programming 31

Examples:

One filename in each line


$ ls -1
arith.lst
exp.lst
oper.c
sqlscripts
All files including those beginning with a dot (.), current directory (.) and directory above (..)

$ 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

-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

Sorts the filenames in the reverse order

$ 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/

Combining the l and a options


$ ls -la
drwxr-x— 3 sibns group 512 Feb 12 13:20 .
drwxr-xr-x 11 sibns group 5120 Feb 12 12:06 ..
-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
BSIT 43 UNIX and Shell Programming 33

4.11 THE UNIX FILE SYSTEM


While there is no standard Unix file structure for all flavors of Unix, there are some common directories
created at the time of Unix installation. The directory structure of a typical Unix system is as shown:

root (/)

bin dev etc home tmp usr var

user1 user2 user3 user4

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.

/usr/include contains all the header files used by C programs.

/tmp is directory where users can create temporary files.

/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.

4. What are the different types of files in Unix ?

5. Why is the size of a directory file small ?

6. Create a new directory in your home directory and change to it. Display your absolute path.

7. Can a directory be removed if it is not empty ?

8. What are hidden files and how can they be displayed ?


BSIT 43 UNIX and Shell Programming 35

Chapter 5

Managing Ordinary Files

5.1 DISPLAYING AND CREATING FILES: cat


cat – concatenates and displays the contents of files

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

$ cat > 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.

5.2 COPYING THE FILES: cp


cp – Copies files from a source to a destination

BSIT 43 UNIX and Shell Programming 35


36 Chapter 5 - Managing Ordinary Files

Syntax:

cp [i] [-R] source target


Examples:

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 file1 file2 myfolder


The –i option prompts the user for confirmation before copying the file.

$ cp – i infile1 infile2

The –R option copies all files and subdirectories in the directory cprogs to the directory cprogback
$ cp –R cprogs cprogsback

5.3 DELETING THE FILES: rm


rm - Removes files or directories

Syntax

rm [-ifrR] file ……..


The rm command has to be used with caution because a file once deleted cannot be recovered.

Examples:

Remove the file named infile


$ rm infile

Remove more than one file at a time

$ rm file1 infile file2


The interactive option causes rm to ask for confirmation before each deletion.

$ rm –i file1

With the –f option, rm forces the deletion of write protected files.


BSIT 43 UNIX and Shell Programming 37

$ rm –f infile1

With –r, rm recursively deletes the contents of the directory specified and the directory itself

$ rm –r cprogs

5.4 RENAMING THE FILES: mv


mv - move or rename files or directories

Syntax

mv [-i] source target rename a file or directory

mv source file …. target directory move a group of files to a directory

Examples

Rename file1 to file2

$ mv file1 file2

move files file1.txt and file2.txt to the directory myfolder

$ mv file1.txt file2.txt myfolder

5.5 PAGING OUTPUT: more


more – view a file one screenful at a time

Syntax

more [ -options ] filename

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

5.6 PRINTING A FILE: lp


lp – print a file
Syntax
lp filename
Unix does not allow direct access to a printer. Files to be printed have to be spooled into a print queue.
Each print request is given a job number. The spooling facility ensures printing of files in an orderly
manner. When the lp command is given, it displays the request-id, which is a combination of the printer
name and the job number.
Example:
$ lp prog1.c
request id is mt-15 (1 file)
The print jobs in the queue can be viewed by typing lpstat. This command shows the request-id which
can be used to cancel a print job in the queue. A print job can be cancelled either by the user who owns
the print job or the system administrator. The system administrator can cancel any job.
Example:
$ lpstat
mt-20 user1 24330 Mar 12 13:46
mt-21 user1 15508 Mar 12 13:46
BSIT 43 UNIX and Shell Programming 39

mt-22 user1 12151 Mar 12 13:46


$ cancel mt-22
request “mt-22” canceled
$ lpstat
mt-20 sibns 24330 Mar 12 13:46
mt-21 sibns 15508 Mar 12 13:46

5.7 KNOWING FILE TYPES: file


file – determines file type
Syntax
file filename(s)
This command identifies the file as regular, directory or device file. Further attempts at classification
are done by testing each filename. The filetype is identified by examining the magic number found in the
first few bytes of the file. Each file type has a unique magic number.
Example: * denotes all files. Various types of files are identified by file.
$ file *
abs.dat: ascii text
absdel.cob: English text
add.lst: empty
cob: directory
hist: commands text

5.8 COUNTING LINES, WORDS, AND CHARACTERS: wc


wc – Counts lines, words and characters
Syntax
wc [ -lw ] [ -c ] filename(s)
The command counts lines, words and characters in the file(s) named depending on the option used.
Examples:
Count lines in the file sib0.cob
$ wc -l sib0.cob
582 sib0.cob
40 Chapter 5 - Managing Ordinary Files

Count words in the file sib0.cob

$ wc -lw sib0.cob
2378 sib0.cob
Count characters in the file sib0.cob
$ wc -c sib0.cob
22706 sib0.cob

5.9 DISPLAYING A DATA IN OCTAL: od


od – display files in octal format
Syntax
od [ -bcdhosx] file
The od command nakes non-printing characters in a file visible by displaying the ASCII octal value of
the characters. The command line options are listed as:

option action

-b displays bytes in octal

-c displays bytes in ASCII; non-


graphic characters are shown
as escape sequences

-d displays words in unsigned


decimal

-h shows bytes in hexadecimal

-o displays bytes in octal

-s displays in signed decimal

-x displays in hexadecimal
Example
$ cat f1
carriage return ^M
next page ^L
BSIT 43 UNIX and Shell Programming 41

The file f1 when displayed with od and the –bc options.


$ od –bc f1

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.

5.10 SPLITTING A FILE INTO MULTIPLE FILES: split


split - split large files into smaller files:
Syntax
split [options] filename prefix
where filename is the name of the large file to be split, prefix is the name to be given the small output
files and options can either be excluded or can be one of the following:
-l linenumber
-b bytes
If -l (the letter l) option is used, linenumber will be the number of lines to be put in each of the smaller
files (the default is 1,000). If the -b option is used, bytes will be the number of bytes to be put in each
of the smaller files.
The split command will give each output file created the name prefix with an extension attached to the
end to indicate its order. By default, the split command adds aa to the first output file, proceeding through
the alphabet to zz for subsequent files. If no prefix is specified, most systems use x .
Examples
Assuming that file.txt is 3,000 lines long, it will output three files, xaa, xab, and xac, and each one will
be 1,000 lines long.
$ split file.txt
This will output six 500-line files: fileaa, fileab, fileac, filead, fileae, and fileaf.
$ split -l 500 file.txt file
42 Chapter 5 - Managing Ordinary Files

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

5.11 COMPARING TWO FILES: cmp


cmp – compare two files
Syntax
cmp [-l] file1 file2
The cmp command compares two files of any type and sends the results to the standard output. By
default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first
difference occurred is reported. The numbering of Bytes and lines begin with one. The option –l displays
the byte number in decimal and the differing byte values in octal whenever a difference in the two files
occurs.
Examples
$ cat fruits1
apple
mango
pineapple
orange
$ cat fruits2
apple
grapes
pineapple
orange
$ cmp infile infile1

infile infile1 differ : char 7, line 2

5.12 FINDING WHAT IS COMMON: comm


comm – lines common to two sorted files
BSIT 43 UNIX and Shell Programming 43

Syntax

comm [123] file1 file2


The comm command reads two sorted files, file1 and file2, to produce a three-column output. The
first column gives lines that exist only in file1; the second column displays lines that exist only in file2;
and the third column shows the lines common in both files.

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:

$ comm fruits1 fruits2


apple
grapes
mango
pineapple
orange
$ comm -23 fruits1

fruits2

mango
$ comm -12 fruits1 fruits2

apple
pineapple
orange

5.13 CONVERTING ONE FILE TO OTHER: diff


diff – compares two files and displays the lines that are different
Syntax
diff file1 file2
44 Chapter 5 - Managing Ordinary Files

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:

$ diff fruits1 fruits2


2c2

< mango


> grapes

2c2 changes line 2, which is line 2 in the second file.

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

3. Remove the file lesson1 from the home 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.

6. What happens when lesson1 and lesson2 are compared.

7. List the contents of the directory lessons datewise.

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

Basic File Attributes

6.1 LISTING FILE ATTRIBUTES: ls -l


$ ls -l
total 170
-rw-r----- 1 sibns sibgrp 20583 Sep 22 11:31 a.out
-rw-r----- 1 sibns sibgrp 36387 Sep 22 11:31 lex.yy.c
-rw-r----- 1 sibns sibgrp 366 Sep 22 11:31 nayan.c
-rw-r----- 1 sibns sibgrp 203 Sep 22 11:31 nayan1.c
-rw-r--r-- 1 sibns sibgrp 0 Mar 17 17:51 s1
-rw-r--r-- 1 sibns sibgrp 17960 Feb 12 13:01 sib21.crn
drw-r--r-- 1 sibns sibgrp 512 Feb 12 13:23 y1

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:

BSIT 43 UNIX and Shell Programming 45


46 Chapter 6 - Basic File Attributes

Character File type


- ordinary file
d directory file
l symbolic link
b block special device
file

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.

6.2 FILE PERMISSIONS


In Unix, the system of assigning permissions to files is simple and well defined, There are three levels
of file protection that determine a file access rights.

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.

6.3 CHANGING FILE PERMISSIONS: chmod


chmod - change the access permissions of a file or directory
Syntax in Symbolic mode
chmod [ -R ] [ category ] [operator] [ permission ] file
Syntax in Absolute mode
chmod [ -R ] mode file
The chmod command changes the access permissions (or mode) of the specified file or directory.
Users who own the file or directory use it to control file and directory access. Mode is an expression
made up of letters and operators (called symbolic mode), or a number (called absolute mode).
The –R option is used to change file mode characters recursively. For a specified directory, chmod will
change the file mode characters of the directory and all files in the file hierarchy below it.
In the symbolic mode, category can be any one, or a combination, of the following letters:
letter user category

a for all users

g for group, all users who have the same


group ID as the owner of the file or
directory

o for others, all the other users on the


system who neither belong to the group
nor own the file .

u for user, the owner of the file or


directory.
48 Chapter 6 - Basic File Attributes

The operators are:

Operator action

+ to add permission

- to remove permission

= to assign absolute permission

The mode can be any combination of the following letters:

letter mode

r sets Read permission

w sets Write permission

x sets Execute permission for files and search


permission for directories

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-

octal number permission

4 set read permission

2 set write permission

1 set execute permission


BSIT 43 UNIX and Shell Programming 49

The permission combinations are listed in the table below:

Octal Number Permissions Description


0 None No permissions granted
1 --x Execute
2 -w- Write
3 -wx Write/execute
4 r-- Read
5 r-x Read/execute
6 rw- Read/write
7 rwx Read/write/execute

The permission fields in octal numbers are listed below:

octal number owner group others complete code


777 rwx rwx rwx rwxrwxrwx
755 rwx r-x r-x rwxr-xr-x
700 rwx --- --- rwx------
666 rw- rw- rw- rw-rw-rw-

Examples
$ chmod 764 siblst

For the file siblst, chmod assigns

7 (4+2+1) read , write and execute permission to the user(owner)


6 (4+2) read and write permission to the group and

4 read permission to others.

$ chmod 777 siblst


All permissions are assigned to all categories of users - user, group and others (ugo)

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

6.4 FILE SYSTEM AND INODES


A Unix system may have more than one file system where each file system has its own tree-like
directory structure with a root directory. When there are more than one file systems, one of them which
contains all the essential files of the Unix system will be the main file system. This is the root file system
and all the other file systems, if they exist, attach themselves to the root file system.
The file attributes (except name and content) of each file in the file system are stored in a table. This
table is called the inode and can be accessed by the inode number. The inode number along with the
filename is stored in the directory. When a Unix command uses a filename in its command line, the kernel
locates the inode number of the file from the directory and then reads the inode to get all the details of the
specified file.
In every file system, a part of the file system is reserved for storing inodes which is accessible only to
the kernel. Using simple arithmetic, the kernel locates the inode number of any file in the file system. The
inode number of a file is unique in the file system.
The ls command uses the inode to get a file’s attributes. The inode number can be displayed using the
ls command as follows-
$ ls –il siblst
2737 -rw-r—r— 1 sib sibgrp 38200 Mar 29 16:16 siblst
The inode number for the file siblst is 2737. This number cannot be assigned to any other filename
unless the file is deleted. If the file is deleted, the inode number is given to another file by the kernel.

6.5 HARD LINKS


In Unix, a single copy of a file can have several names, which are links to the same file. Since all the
filenames refer to the same file, they have the same inode number. The second column in output of the ls
–l command gives the number of links that a file has. The number is 1 for unlinked files.
Hard links can be created using the ln command. The syntax is the same as the syntax of the cp
command.
Example
Displays the link count of siblst as one
$ ls –il siblst
2737 -rw-r—r— 1 sib sibgrp 38200 Mar 29 16:16 siblst
create a hard link to siblst called sibrep

$ ln siblst sibrep
BSIT 43 UNIX and Shell Programming 51

display the inode numbers and link counts of the two files

$ ls –il siblst sibrep


2737 -rw-r—r— 2 sib sibgrp 38200 Mar 29 16:16 siblst

2737 -rw-r—r— 2 sib sibgrp 38200 Mar 29 16:16 sibrep

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.

$ ln file1 file2 file3 filelnk

display inode numbers and link counts of file1, file2, file3


$ ls -il file1 file2 file3

4014 -rw-r—r— 2 sibns group 10240 Apr 5 12:34 file1

4015 -rw-r—r— 2 sibns group 10240 Apr 5 12:34 file2


4017 -rw-r—r— 2 sibns group 7214 Apr 5 12:34 file3

display files in the filelnk directory

$ ls -il filelnktotal 56
4014 -rw-r—r— 2 sibns group 10240 Apr 5 12:34 file1

4015 -rw-r—r— 2 sibns group 10240 Apr 5 12:34 file2

4017 -rw-r—r— 2 sibns group 7214 Apr 5 12:34 file3


Hard links have their own uses. When files in a directory are shifted to another directory, these files
can still be made available by creating hard links. The use of hard links also prevents accidental deletions
of files.

Hard links have some limitations.


l Unix permits hard links to files, but, not to directories. Each directory inode is allowed to appear
once in exactly one parent directory and no more. This restriction means that every sub-directory
52 Chapter 6 - Basic File Attributes

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.

These limitations can be overcome by the use of symbolic links.

6.6 SYMBOLIC LINKS


A symbolic link is a special file that contains the pathname of the file to which it is linked. This is unlike
the hard link wherein the linked file also contains the contents of the file to which it is linked. Symbolic
links are also called as soft links and are more flexible. They can be used to link to directories, or to files
on other file systems. When a target file is deleted, a symbolic link to that file become unusable. This is
unlike a hard link where the contents of the file are preserved.

The ln command along with the –s option is used to create a symbolic link

$ ln -s o2 o2lnk
$ ls -il o2 o2lnk

3367 -rw-r—r— 1 sibns sibgrp 12451 Apr 1 12:44 o2

2823 lrwxrwxrwx 1 sibns sibgrp 2 Apr 1 12:42 o2lnk -> o2


The first character in the permission string of the symbolic link file will be ‘l’. The notation o2lnk -> o2
indicates that the file o2lnk contains the pathname for the file o2. In symbolic links, the linked files are two
different files. A symbolic link has a separate directory entry with its own inode number.

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.

6.7 DEFAULT FILE AND DIRECTORY PERMISSIONS: umask


Every time a file or a directory is created, default permissions are established for it. These default
permissions are initially assigned either by the operating system or the program being run. Setting default
permissions saves us the trouble of specifying permission codes explicitly every time a file or directory is
created. The operating system assigns the default permission values of 777 for executable files and 666
for all other files.
BSIT 43 UNIX and Shell Programming 53

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.

The eight possible umask permission combinations are listed below.

octal permissions description


number
0 rwx read write execute
1 rw- read write
2 r-x read execute
3 r-- read
4 -wx write execute
5 -w- write
6 --x execute
7 none no permissions granted

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.

6.8 LOCATING FILES: find


find - find files matching a set of selection criteria

Syntax

find pathname-list expression


The command find recursively descends the directory tree for each pathname in the pathname-list
looking for files that match a boolean expression. The pathname-list can have one or more directories
separated by whitespace. The expression is a selection criteria followed by the action to be taken.

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 . –name file1.lst –print


Files of different types can also be looked up using find. File type can be f (for ordinary files), d (for
directories), l (for symbolic links), c (for character special files) and b (for block special files).

Find and display all the directories in the file system /user1

$ find /user1 –type d –print


Find and display all the files with permission 777 in the root file system
BSIT 43 UNIX and Shell Programming 55

$ find / -perm 777 –print

find all the directories in the home directory with permissions 777. An implied and condition between
perm and type is applied by find here.

$ find $HOME -perm 777 –type d –print

find all files modified within the last 7 days


$ find /user1 –mtime –7 –print

find all files not accessed for more than a year

$ find /user1 –atime +365 –print


The logical operators –a (and), -o (or) and ! (not) can also be used the selection criteria of find.

Find the files sample.txt or file1.txt in /user1

$ find /user1 –name sample.lst –o –name file1.txt -print


find the file file1.txt in /user1 and remove it; the rm command is followed by a pair of flower brackets,
a backslash and a semicolon.

$ find /user1 –name file1.txt –exec rm {} \;

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.

3. What happens if a directory has permissions 777 ?

4. How are default file and directory permissions changed ?

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 ?

7. What are the uses of hard links ?

8. How are hard links created ?

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 ?

11. Find all the directories in the /user directory ?

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

7.1 THE BASICS OF vi

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.

7.2 THE MODES OF vi


The three modes of vi are the command mode, the input mode and the ex or last line mode.
The command mode is the default vi mode. Every key pressed in this mode is interpreted as a command
to be executed on the text. The key pressed is not displayed on the screen. If the [Esc] key is pressed in
this mode, a beep is sounded. Commands can be given to perform functions like moving the cursor
around the screen, delete lines, words or characters, etc.

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.

BSIT 43 UNIX and Shell Programming 57


58 Chapter 7 - The vi Editor

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.

An illustration of the three vi modes is shown below:

input mode ex mode

[Esc] : (colon) [Enter]

a,i,I,A,o,O,
R,r,s,S and C
Command
mode

vi file1 :x, :wq, ZZ

shell
BSIT 43 UNIX and Shell Programming 59

7.3 NAVIGATION IN COMMAND MODE


In the command mode, navigation keys for the movement of the cursor by characters, words and lines
are as follows:

Keys movement of cursor

h cursor moves left

j cursor moves down

k cursor moves up

l cursor moves right

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:

keys screen display

[ctrl-f] scroll display forward

[ctrl-h] scroll display backward

[ctrl-d] scroll half-screen forward

[ctrl-u] scroll half-screen backward

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

keys cursor position

G last line of file

nG line number n

7.4 ADDING AND REPLACING TEXT


The following key commands are used to add or replace text in the input mode:

command Function

i Insert text to before cursor

a append text after cursor

I insert text before first non-blank


character

A append text at end of line

o open a new line below

O open a new line above

r replace single char under cursor

R replace characters from cursor onwards

s replace single character with characters.

S replace entire line

7.5 SAVING TEXT


When a file is edited in vi, the changes are only made in the buffer. The buffer has to be saved to the
file. The following are the save and exit commands:
BSIT 43 UNIX and Shell Programming 61

command what it does


:w write buffer to the current filename and remain in vi
:w filename write buffer to file filename. and remain in vi
wq write buffer to current filename and quit vi
:x same as :wq, saves buffer to file and exits vi
:q! quit vi without saving buffer
:.w filename write current line to file
:$w filename write last line to file
:1,10w filename selected lines saved to file
:e filename close current buffer and filename
ZZ save to current file and exit vi
If for some reason, the Unix system crashes while in vi, the user can still recover the buffer by typing
$ vi –r filename at the

7.6 DELETING TEXT


The following commands are used for deletion of text:

command what it does

x delete a character under the cursor

dw delete a word

dd delete a line of text

3dd delete 3 lines of text

u undo previous change

7.7 PATTERN SEARCH


Searching for text in vi can be done in both forward and backward directions. The search command
can also be repeated. The commands are listed below:
62 Chapter 7 - The vi Editor

Command What happens

/pattern[Enter] searches forward and positions cursor on the first occurrence of


the pattern in the text being searched

?pattern[Enter] searches backward and positions cursor on the first occurrence of


previous line matching pattern

n repeat forward search for last / or ?

N repeat backward search for last / or ?

7.8 PATTERN SEARCH AND REPLACE


vi has a powerful search and replace feature which is very useful. It is used in the colon mode and the
syntax is as follows:

:line address/pattern to search/replace pattern/flags


Example:

: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.

7.9 MISCELLANEOUS OPERATIONS IN COMMAND MODE


C change rest of line starting at cursor (same as c$)

D delete rest of line starting at cursor (same as d$)

J join lines
Y yank lines ie. Copy lines (same as yy)

p put back text after cursor

P put back text before cursor


u undo last change
BSIT 43 UNIX and Shell Programming 63

U restore current line

. repeat last change

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

c) move the cursor four lines down

d) add a new line above a line of text

e) remove two lines from the end of a text and add it to the beginning of the file.

f) open a file and move to the fifteenth line in the file.

2 What are the different ways of adding text using vi ?

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 ?

5 How do you yank and paste lines ?


64

Chapter 8

The Shell

8.1 THE SHELL’S INTERPRETIVE CYCLES

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

8.2 COMMON SHELLS


There are several different shells that can be used in Unix. Some of the most common Unix shells are
Bourne shell (sh), C shell (csh) , and tcsh. Here is a table describing each.
The bourne shell (/bin/sh) is the original Unix shell written at AT&T. Some of its features are wildcard
substitution, I/O redirection, environment variables, and a simple script programming language. The korn
shell (/bin/ksh) and Bash (/bin/bash) are its derivatives.

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.

64 Chapter 8 - The Shell


BSIT 43 UNIX and Shell Programming 65

8.3 PATTERN MATCHING – THE WILD-CARDS


Wildcards are a special set of characters used by the shell to match filenames. A wildcard “stands in”
for other characters of the filename. The filenames pattern is formed by ordinary characters and
metacharacters using well defined rules. When the pattern is used as an argument with a command, the
shell first suitably expands the argument and then executes the command. The use of wildcards makes it
easier to deal with files in Unix. File names can be abbreviated using wildcards. The shell’s wildcards are
listed below:

wildcard what it matches


* any number of characters—including zero
characters.

? a single character

[abc] a single character- either a, b or c

[a-d] any single character in the range a to d

[!abc] a single character that is not a or b or c

[!a-d] any character not in the range a to d

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

Matches all filenames beginning with an alphabet- uppercase or lowercase

$ 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

remove all files ending with a, character, a


$ rm *.a?a
move all files beginning with lesson and ending with .txt to the lessonfiles directory
mv lesson*.txt lessonfiles
Wildcards can also be combined to match file names.
Example
Files that begin with file and end with lst?. The ? means any single character
$ ls file*.lst?
BSIT 43 UNIX and Shell Programming 67

8.4 ESCAPING AND QUOTING


In Unix, file names should not contain any of the following special characters:
& * ? [ ] < > | \ { } $ ( ) # ‘ “ /: ^ ! - % `

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 how are you \?

Or how are you? Can be enclosed in single quotes


$ echo ‘how are you?’

Display three asterisks, enclosed in double quotes

$ 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.

8.5 REDIRECTION: THE THREE STANDARD FILES


When a process starts executing, the operating system opens three files for the process:
68 Chapter 8 - The Shell

1. standard input (stdin), the file representing input from the keyboard

2. standard output (stdout), the file representing output to the screen


3. standard error (stderr), the file which represents error meassages coming from the command
and displayed on the screen

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

2. input can be read from a file

3. input can be from another command using pipes


The program writes its output to standard output. The file representing standard output can have three
sources:

1. By default, a process writes stdout to the screen.


2. Output can be redirected to a file

3. Can be used as an input to another command

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

The command wc reads the input from the file file1.txt.


$ wc < file1.txt
BSIT 43 UNIX and Shell Programming 69

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

This is a sample file


To be given as an input to

The wc command

[ctrl-d]
However, a few commands like mail make use of < for special functions.

Example

$ mail user1 < letter.txt


The mail command mails the file letter.txt to user1.

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

$ cat *.lst > 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:

file descriptor standard file represented


0 standard input, default keyboard
1 standard output, default screen
2 Standard error, default screen

The general format for standard error redirection is as follows::

command 2> errfile


where errfile is the name of the file to which the process writes the standard error. The 2> is a file
descriptor digit combined with >, the output redirection symbol. The file descriptor digit indicates to the
shell which standard file is to be accessed so that its contents may be redirected. The file descriptor digit
2 is an indication that the standard error file is being redirected.
Example:

When the ls command tries to list a file which does not exist, an error is redirected to the file errorfile

$ ls file1.lst 2> error


display the contents of errorfile.

$ cat errorfile

cat : cannot open file1.lst : no such file or directory (error 2)


Both standard output and standard error can be redirected to different files. The syntax is:

command > outfile 2> 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

$ trial.sh > result..txt 2> err.lst


To redirect both standard output and standard error to the same file:

command 1> outfile 2>&1


BSIT 43 UNIX and Shell Programming 71

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

8.6 /dev/null AND /dev/tty: TWO SPECIAL FILES


The /dev/null is a special file that always remains empty. Anything redirected to /dev/null disppears.
Redirecting error messages to /dev/null will prevent them from being displayed on the terminal screen.
This is the only device file which is not associated with any physical device.

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.

If there were no pipe construct available, the process would be as follows:


$ ls -la > filelist
72 Chapter 8 - The Shell

$ more < filelist

$ 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 files and redirect the output to a file.


$ ls | wc –l > outfile

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.

8.8 CREATING A TEE: tee


A tee allows a user to save the output from a command to a file and at the same time, pipe the output
to another command or save the output to a file. tee is an external command and can be placed anywhere
is a pipeline.
With tee a user can read input from an input stream, and split the output stream so it is both displayed
on screen (stdout) and also redirect it to a file.

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.

.$ ls *.txt | wc -l | tee /dev/tty hcount.txt

8.9 COMMAND SUBSTITUTION


In command substitution, the shell alows the the user to use the output from one command as arguments
to another command A pair of backquotes (“), also called grave accents does command substitution in
Unix shells. Only those commands that use standard output can be used in command substitution.

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

$ echo The number of files in the directory are ‘ls | wc –l‘

8.10 SHELL VARIABLES


A shell variable is a memory storage area that can be used to hold a value. A shell variable can be an
environment variable or a user-defined variable.

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

variable value held


HOME pathname of the home directory
SHELL pathname of the shell (/bin/sh)
PATH list of directories where shell looks
for commands
TERM terminal type
USER user name
MAIL pathname of the system mailbox

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.

The syntax to assign a value to a variable is as follows:

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

Assign the value 100 to the variable abc

$ abc=100

display the value assigned to the variable abc

$ echo $abc

The value stored in one variable can be assigned to another variable.


BSIT 43 UNIX and Shell Programming 75

$ xyz=$abc

$ echo $xyz

assign a null string to a variable abc

$ abc=

A variable can be removed by using the shell internal command, unset.

$ unset abc

A multiword string can be assigned to a variable by the use of single quotes.

Example

$ abc=’This is a multiword string’

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

A variable can also be concatenated with a string.

$ 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?

2. Explain this command: cat > out.dat

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.

5. What does the character \ mean to the shell ?

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?

8. How do find out the number of users logged in to the system ?


BSIT 43 UNIX and Shell Programming 77

Chapter 9

The Process

9.1 BASICS OF A 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.

Other attributes are inherited by a child process from its parent.

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.

BSIT 43 UNIX and Shell Programming 77


78 Chapter 9 - The Process

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

$ cat filename | more


two processes cat and more are generated by the shell.

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

9.2 PROCESS STATUS: ps


The ps command is used to display some of the process attributes just as the ls command displays the
file attributes. The ps command reads the kernel’s data structures and process tables to get the
characteristics of the processes. Without options ps displays the processes associated with a user at the
terminal.

$ ps

PID TTY TIME CMD


3472 ttyp9 00:00:00 sh

3473 ttyp9 00:00:00 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..

The several options of ps are listed below

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

UID PID PPID C STIME TTY TIME CMD


sibns 3472 3471 2 12:57:04 ttyp9 00:00:00 sh -i

sibns 3475 3472 2 12:57:14 ttyp9 00:00:00 ps -f

the processes being run by the user sibns


80 Chapter 9 - The Process

$ ps -u sibns

PID TTY TIME CMD


3478 ttyp9 00:00:00 ps
3470 ttyp18 00:00:00 script
3471 ttyp18 00:00:00 script
3472 ttyp9 00:00:00 sh
2814 ttyp18 00:00:00 sh
Shows processes of all users. System processes are not displayed.

$ ps -a

PID TTY TIME CMD


447 tty02 00:00:00 vbiosd
3480 ttyp9 00:00:00 ps
3403 ttyp5 00:00:00 vi
3425 ttyp14 00:00:00 vi
3443 ttyp2 00:00:00 vi
3444 tty01 00:00:00 sh
3243 ttyp10 00:00:00 vi
2389 ttyp2 00:00:00 sh
3392 ttyp6 00:00:00 sh

9.3 SYSTEM PROCESS


In addition to the processes created by the user, there are a number of system processes also
running.They are created during system startup and the system is in the multiuser state.The -e or -a option
with ps will give a list of all the processes running on the system.
All user and system processes are displayed

$ ps -e

PID TTY TIME CMD


0 ? 00:00:00 sched
1 ? 00:00:00 init
2 ? 00:00:00 vhand
3 ? 00:00:00 bdflush
4 ? 00:00:00 kmdaemon
5 ? 00:00:01 htepi_daemon
BSIT 43 UNIX and Shell Programming 81

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.

9.4 MECHANISM OF PROCESS CREATION


The creation of a process is in three phases and uses three system calls known as fork, exec and wait.
fork is a system call that creates a new process from an existing process. The new process is called the
child process, and the existing process is called the parent. The attributes of the parent and child are an
identical image except for parameters like PID. The child gets a new PID and the process is forked. This
is the mechanism used to multiply processes in the system.The child process overwrites the image with
a copy of the programme that is to be executed by using the exec system call. No additional processes are
created and the existing program is replaced with a new program. The PID of this process is the same as
that of the child process which was forked.

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:

1. The real UID and real GID of the process.


2. The effective UID and GID of the process.
3. The process priority.
4. The current directory of the running process.
5. A description of all the files opened by the parent process.
6. Environment variables like HOME and PATH.
82 Chapter 9 - The Process

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.

9.5 RUNNING JOBS IN BACKGROUND


Unix is a multitasking operating system and a number of programs can run at the same time. A
command can be started and put in the background which can continue to run while another command is
started in the foreground. This is particularly useful when a command has a longer execution time.
To run a background process, the command is ended with an ampersand (&). Once a process is
running in the background, the parent does not wait for the child process to die.

When a background process is created, the following takes place:


1 The PID of the command entered is displayed. The PID is used to keep track of all processes
currently running on the system.

1 The prompt returns so that you can enter another command.

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

$ find / -type f -print >ofile &


24
When the background process starts, the system assigns it a PID (24 in this example), displays it, and
then prompts the user for another command..In the Korn or C shell, job numbers are also assigned along
with the PID. The PID of the last background process is stored in the system variable $!.

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.

9.6 JOBS EXECUTION WITH LOW PRIORITY: nice


nice – runs a command with an altered scheduling priority

Syntax

nice [-increment | -n increment ] command [argument ... ]


The nice command is used to change the scheduling priority of a command being executed. Each
process has a nice value which is used to calculate its priority. Nice values range from 0 to 39. Higher nice
values result in lower priorities. By default, when nice is not used, commands have a nice value of 20.
Using nice executes a command with a nice value equal to 20 plus increment. If no increment is specified,
a value of 10 is assumed giving a nice value of 30. The -n option is preferrably used to specify the
increment. Only the superuser (root) can increase the priority of a user.
Example

Execute a command with a low priority value

$ nice sort file1.lst > file1.srt &


84 Chapter 9 - The Process

The nice value is specified

$ nice –n 10 sort file1.lst > file1.srt &

9.7 KILLING A PROCESS WITH SIGNALS


The occurrence of an event is communicated to a process by sending a signal to the process. These
signals are identified by numbers and are designated to perform specific functions. Signals are also identified
by symbolic names with the prefix SIG. Signals are generated by the keyboard or by the kill command.

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

Kill many processes at the same time


$ kill 2984 5841 654

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

9.8 EXECUTE LATER: at and batch


The commands at and batch are used to schedule jobs to run at specified times.
The argument for the command at is the time at which the program is to be executed

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

9.9 RUNNING JOBS PERIODICALLY: CRON


A task can be automatically run in the background at regular intervals by a Unix utility called cron.
The cron daemon takes care of running these background jobs, which are called cron jobs.
crontab or the crontable is a file which contains the schedule of cron entries to be run and at specified
times. cron checks the crontable at regular tables to see if there are any jobs scheduled.

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.

The following options can be used with crontab:


crontab -e Edit your crontab file, or create one if it doesn’t already exist.

crontab -l Display your crontab file.

crontab -r Remove your crontab file.


An editor has to be specified to open the crontab file.

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

day of week (1 - 7) (monday = 1)


month (1 - 12)
day of month (1 - 31)
hour (0 - 23)
min (0 - 59)

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

9.10 TIMING PROCESSES: time


The time command can be used to find out how efficiently a program uses the system resources. The
time command accepts the command line of the program to be timed as its argument. The program is
executed and the time usage is displayed on the terminal..

Example

The time taken to perform a find operation can be displayed by preceding the find command with
time.

$ time find / -name core –print

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

2. What is the name and PID of your shell process?

3. How can you tell if a process is linked to a terminal?

4. How do you run a process in the background ?

5. How can you terminate a running process?

6. Open another terminal session. Find the pid of this process and kill it. What command did you use?

7. What is the significance of -9 in the command: kill -9 pid?

8. What is a daemon process ?

9. How do you kill a process running in the background ?

10. What does the following crontab entry mean ?

30 15 * * * sort file1.lst > file1.srt


BSIT 43 UNIX and Shell Programming 89

Chapter 10

Communication and Electronic Mail

10.1 WRITE AND TALK

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

BSIT 43 UNIX and Shell Programming 89


90 Chapter 10 - Communication and Electronic Mail

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.

10.2 REFUSING AND ACCEPTING MESSAGES


Permission to receive messages from other users of the system may be granted or denied by use of
the mesg command. By default, for reasons of security, users are not allowed to receive messages. This
can be altered by the mesg command.
mesg - allow or deny messages sent to the terminal
Syntax
mesg [n] [-n] [y] [-y]
The mesg command with argument n denies messages sent by write or talk. This is done by revoking
the non-user write permission on the user’s terminal. The mesg command with argument y restores
permission. When mesg is used without arguments, it reports the current state without changing it.

10.3 E-MAIL BASICS


e-mail messages are stored in the users’s mailbox as and when they are received. There are two kinds
of mailboxes in the system - the system mailbox and the personal mailbox. The system mailbox is a file
that has all the mail received but not read, deleted or saved to another file. The system mailbox is generally
located in the system’s /var/spool/mail directory and has the same name as that of the user’s login name.
For example, for a user with a login name, user1, the system mailbox file will be /var/spool/mail/user1.

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-

subject field - optionally filled up


date and from fields - automatically filled up by the system
to field - mail address of the user to whom the mail is being sent
Cc field - send a carbon copy to another user

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.

10.4 THE UNIVERSAL MAILER: mailx


mailx is a command line tool which can run non-interactively from shell scripts. mailx can be invoked
in sending and receiving mode. In the sending mode, mailx becomes interactive. For example typing

$ 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.

$ mailx -s “hello” user1 < mesg.txt


The e-mail is appended to user1’s mailbox /var/spool/mail/user1.

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

A copy of an editor buffer of your file “out”


was saved when the editor was killed.
This buffer can be retrieved using the “recover” command of the editor.
An easy way to do this is to give the command “vi -r out”.
This works for “edit” and “ex” also.
&q
Saved 1 message in /college1/sibns/mbox
Held 1 message in /usr/spool/mail/sibns
Typing 2 at the mailx prompt (&) displays the second message. The message headers and the message
body are displayed. Typing q at the prompt will quit mailx and save message number 2 in the user’s
personal mailbox, mbox.
The following command is used to list mail messages stored in the user’s personal mailbox, mbox:
$ mailx –f

10.5 A FULL-SCREEN MAIL PROGRAM: pine


pine is a character-based menu driven mailer with the additional feature of an address box. Incoming
and outgoing mails are sent into different files called folders. pine is invoked by typing pine at the Unix
prompt. The pine main menu window is shown below:
94 Chapter 10 - Communication and Electronic Mail

$ pine

Pine 4.44 Main Menu Folder: INBOX No Messages

? HELP - Get help using pine

C COMPOSE MESSAGE - Compose and send a message

I MESSAGE INDEX - View message in current folder

L FOLDER LIST - Select a foler to view

A ADDRESS BOOK - Update address book

S SETUP - Configure pine options

Q QUIT - Leave the pine programme

Copyright 1989-2002. PINE is a trademark of the University of Washington.


[Folder "INBOX" opened with 0 messages]
? Help P PrevCmd R RelNotes
O OTHER CMDS > [ListFldrs] N NextCmd K KBLock

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 --------

^G Get Help ^X Send ^R Rich Hdr ^Y PrvPg/Top ^X Cut Line ^O


Postpone
^C Cancel ^D Del Chart ^J Attach ^V NxtPg/End ^U UnDel Line ^T To
AddrBk
BSIT 43 UNIX and Shell Programming 95

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.

Typing the Q key will quit pine after a confirmation.

A summary of commands used above is listed:


96 Chapter 10 - Communication and Electronic Mail

In the compose message screen:

key what it does


[ctrl-g] displays help
[ctrl-c] cancel message
[ctrl-x] send message
[ctrl-d] delete character
[ctrl-y] go to previous page
[ctrl-v] go to next page
[ctrl-k] cut line
[ctrl-u] undelete a line

In the message text viewer:

Key what it does


? Get help
V View Attachment or Attachment index
R Reply to Message
E Export message to Unix file
S Save message in pine folder
In the attachment text viewer:

Key what it does


V View attachment
S Save attachment
E Exit viewer

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

4. What happened if you type q at the mailx prompt ?

5. Where is a user’s personal mailbox located ?

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.

8. How is pine different from mailx ?

9. How do you create an address book using pine ?

10. Send a mail to user1 using pine.


98 Chapter 11 - Simple Filters

Chapter 11

Simple Filters

11.1 THE SAMPLE DATABASE

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

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1006|H.R.Rajesh |Asst.Prof. |Mechanical |11/07/76|21000
1010|Jayanthi |Sys.Manager |Computer Science |13/03/66|20000
1007|Sanjay |Lecturer |Civil |19/04/73|15000
1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000

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.

98 Chapter 11 - Simple Filters


BSIT 43 UNIX and Shell Programming 99

11.2 PAGINATING FILES: pr


This command i.e. pr, prepares a file for printing by adding suitable headers, footers and formatted
text. This command has to be used with a file name as an argument.
$ pr emp.lst
2005-04-03 17:01 emp.lst Page 1

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1006|H.R.Rajesh |Asst.Prof. |Mechanical |11/07/76|21000
1010|Jayanthi |Sys.Manager |Computer Science |13/03/66|20000
1007|Sanjay |Lecturer |Civil |19/04/73|15000
1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000
pr adds five lines of margin at the top and five at the bottom. The header shows the date and time of
last modification of the file, along with the file name and page number.
There’s one option that uses a number prefixed by a + to print from a specific page number. Another
option (-l) set the page length.
pr –l 54 emp.lst Page set to 54 lines
pr +5 emp.lst Starts printing from page 5

11.3 DISPLAYING THE BEGINNING OF FILE: head


The head command, as the name implies, displays the top of the file. When used without an option, it
displays the first ten lines of the file.

$ head emp.lst Shows first ten lines of the emp.lst file


1001|T.N.Raju |Professor |Information Science|14/06/61|30000
1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1006|H.R.Rajesh |Asst.Prof. |Mechanical |11/07/76|21000
1010|Jayanthi |Sys.Manager |Computer Science |13/03/66|20000
1007|Sanjay |Lecturer |Civil |19/04/73|15000
1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000

You can use the –n option to display the first ‘n’ number of lines.
100 Chapter 11 - Simple Filters

Example: $ head -n 3 emp.lst Displays first 3 lines.

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000

11.4 DISPLAYING THE END OF A FILE: tail


The tail command displays the end of the file (as opposite to head command). Like head, it displays the
last ten lines when used without arguments.

You can use the –n option to display last ‘n’ number of lines.
Example: $ tail -n 3 emp.lst Displays last 3 lines

1007|Sanjay |Lecturer |Civil |19/04/73|15000


1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000

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.

$ tail +5 emp.lst Display 5th line onwards.

1002|Mallu |Lecturer |Information Science|20/07/74|15000


1006|H.R.Rajesh |Asst.Prof. |Mechanical |11/07/76|21000
1010|Jayanthi |Sys.Manager |Computer Science |13/03/66|20000
1007|Sanjay |Lecturer |Civil |19/04/73|15000
1008|Shivaprakash |Lecturer |Electronics |11/11/78|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000

11.5 CUTTING A FILE VERTICALLY: cut


The features of the cut and paste commands will be illustrated with specific reference to the file
shortlist, which stores the first five lines of emp.lst file.

$ head -n 5 emp.lst | tee shortlist

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
BSIT 43 UNIX and Shell Programming 101

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.

11.5.1 Cutting Columns(-c)


To extract specific columns, you need to follow the –c option with a list of column numbers, delimited
by a comma. Ranges can also be used using the hyphen. Here’s how we extract the name and designation
field from shortlist:
$ cut -c 6-23,25-37 shortlist

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.

1001T.N.Raju Professor 14/06/61|30000


1004D.S.Raghu Lecturer 05/12/75|17000
1005S.K.Anantha Asst.Prof. 20/07/63|24000
1009M.P.Rajendra Sr.Lecturer 13/03/66|20000
1002Mallu Lecturer 20/07/74|15000

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.

11.5.2 Cutting Fields (-f)


The –c option is useful for fixed length lines. Most UNIX files (like /etc/passwd and /etc/group) don’t
contain fixed-length lines. To extract useful data from these files you’ll need to cut fields rather than
columns. (A field is a set of columns)
cut uses the tab as the default field delimiter, but can also work with a different delimiter.
Two options need to be used here are –d for the field delimiter and –f for the field list. To cut second
and third field from a file shortlist, the following is the command.
102 Chapter 11 - Simple Filters

$ cut -d \| -f 2,3 shortlist | tee cutlist1

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.

11.6 PASTING FILES: paste


What you cut with cut command can be pasted back with the paste command – but vertically rather
than horizontally. You can view two files side by side by pasting them. In the previous topic, cut was used
to create the two files cutlist1 and cutlist2 containing two cut-out portions of the same file. Using paste,
you can fix them. The following command does pasting job.
$ paste cutlist1 cutlist2

T.N.Raju |Professor 1001|Information Science|14/06/61|30000


D.S.Raghu |Lecturer 1004|Information Science|05/12/75|17000
S.K.Anantha |Asst.Prof. 1005|Information Science|20/07/63|24000
M.P.Rajendra |Sr.Lecturer 1009|Computer Science |13/03/66|20000
Mallu |Lecturer 1002|Information Science|20/07/74|15000
BSIT 43 UNIX and Shell Programming 103

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

T.N.Raju |Professor |1001|Information Science|14/06/61|30000


D.S.Raghu |Lecturer |1004|Information Science|05/12/75|17000
S.K.Anantha |Asst.Prof. |1005|Information Science|20/07/63|24000
M.P.Rajendra |Sr.Lecturer |1009|Computer Science |13/03/66|20000
Mallu |Lecturer |1002|Information Science|20/07/74|15000

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.

$ paste -s -d “||\n” addressbook

Mallu|Mallu@yahoo.com|2546131
Maharaj|Maharaj@rediffmail.com|2512657
Nagendra|san@yahoo.com|2121134

11.7 ORDERING A FILE: sort


Sort command in UNIX is used to orders a file. Like cut, it identifies fields and it can sort on specified
fields. We’ll consider the important sort options by sorting the file shortlist in different ways. By default,
the entire line is sorted.
104 Chapter 11 - Simple Filters

$ sort shortlist

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1002|Mallu |Lecturer |Information Science|20/07/74|15000
1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra|Sr.Lecturer |Computer Science |13/03/66|20000

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).

11.7.1 Sort Options


Unlike cut and paste, sort uses the space as the default field separator. Whereas in cut and paste we
have used tab as the default field separator. Here we’ll use the –t option to specify the delimiter.

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

1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000


1002|Mallu |Lecturer |Information Science|20/07/74|15000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1001|T.N.Raju |Professor |Information Science|14/06/61|30000

The sorting order can be reversed with –r (reverse) option. The following sequence reverses a previous
sorting order:
$ sort -t “|” -r -k 2 shortlist

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000

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

1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
BSIT 43 UNIX and Shell Programming 105

1002|Mallu |Lecturer |Information Science|20/07/74|15000


1001|T.N.Raju |Professor |Information Science|14/06/61|30000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000

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

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000

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

$ sort -o sortedlist -k 3 shortlist ouput stored in sortedlist


$ sort -o shortlist shortlist output stored in the same file

To check whether the file has actually been sorted in the default order, use the –c (check) option.

$ sort -c shortlist File is sorted


$ _

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

11.8 LOCATE REPEATED AND NON REPEATED LINES: uniq


When you concatenate or merge the files, you’ll face the problem of duplicate entries creeping in.
UNIX offers a special tool to handle these lines – the uniq command. Consider a sorted file dept.lst that
includes repeated lines:

$ 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

11.9 TRANSLATING CHARACTERS: tr


The tr (translate) filter manipulates individual characters in a line. More specifically, it translates
characters using one or two compact expressions:
tr expression1 expression2 standard input

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.

Let’s use tr to replace the | with a $ (dollar) and the / with a -.


$ tr ‘|/’ ‘~-’ < emp.lst | head -n 5

1001~T.N.Raju ~Professor ~Information Science~14-06-61~30000


1004~D.S.Raghu ~Lecturer ~Information Science~05-12-75~17000
1005~S.K.Anantha ~Asst.Prof. ~Information Science~20-07-63~24000
1009~M.P.Rajendra ~Sr.Lecturer ~Computer Science ~13-03-66~20000
1002~Mallu ~Lecturer ~Information Science~20-07-74~15000

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

$ tr ‘[A-Z]’ ‘[a-z]’ < dept.lst | head -n 5

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.

11.10 SEARCHING FOR A PATTERN: grep


The grep command in UNIX is used to search for a pattern in a given file. It displays the selected
pattern, the line numbers or the filenames where the pattern occurs. The syntax is given below:

grep options pattern filename(s)


grep searches for pattern in one or more filenames. The first argument is the pattern and ones
remaining are filenames. Let’s use grep to display lines containing the string Information from the file
emp.lst; the content of this file are shown below:
$ grep “Information” emp.lst

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000
1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1002|Mallu |Lecturer |Information Science|20/07/74|15000
1003|B.S.Ashok |Lecturer |Information Science|01/01/78|15000

We can also store the output of a grep to a file:

$ who | grep kumar > user.lst

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.

11.10.1 grep options


grep is one of the most important UNIX commands, and you must know its different options.
Ignoring Case (-i) When you look for a name, but are not sure of the case, grep offers the –i option
which ignores case for pattern matching:
BSIT 43 UNIX and Shell Programming 109

$ grep -i “anantha” emp.lst

1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000

This locates the name S.K.Anantha.

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.

$ grep -v “Lecturer” emp.lst > otherlist

The above UNIX command stores the result in a file called otherlist. The content of the file is as
follows:

1001|T.N.Raju |Professor |Information Science|14/06/61|30000


1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000
1006|H.R.Rajesh |Asst.Prof. |Mechanical |11/07/76|21000
1010|Jayanthi |Sys.Manager |Computer Science |13/03/66|20000

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

1002|Mallu |Lecturer |Information Science|20/07/74|15000

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.

Table 11.2: Options used by grep

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.

2. Generate a code list by selecting departments from emp.lst

3. Remove repeated lines from a file.

4. Covert the content of file emp.lst to uppercase.

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 .

12.1 A SIMPLE SHELL SCRIPT


Suppose you were to make up a file named commands containing following lines:
# A simple 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

BSIT 43 UNIX and Shell Programming 111


112 Chapter 12 - Shell Programming

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 not found


If this happens, you can still get the shell to run your command this way:

$ ./commands

Remember, “dot” (.) stands for the current working directory.

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

Thus, our commands file would look something like this.

#!/bin/sh
# A simple shell script
cal
date
who

12.3 THE SHELL AS A PROGRAMMING LANGUAGE


The sample script commands is almost trivial - it does nothing more than execute three simple commands
that you could just as easily type into the standard input. The shell can actually do much more. It is, in fact,
a sophisticated programming language, with many of the same features found in other programming
languages, including

l Variables

l Input/output function
l Arithmetic operations

l Conditional expressions

l Selection structures
l Repetition structures

We will discuss each of these in order.

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 User-defined Variables: These are variables that you create yourself.

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

$ display.args I LOVE INDIA.

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:

display.args I LOVE INDIA.


What if the user types in more than nine arguments? The positional parameter $* contains all of the
arguments $1, $2, $3, ....$9, and any arguments beyond these nine. Thus, we can rewrite display.args to
handle any number of arguments:

#!/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: $*

Suppose we were then to type the command line

$ display.args Welcome to the world of UNIX


The computer would respond with
BSIT 43 UNIX and Shell Programming 115

You typed 6 arguments: The arguments are : Welcome to the world of UNIX

12.5 INPUT USING THE READ STATEMENT


The positional parameters are useful for capturing command-line arguments but they have a limitation:
once the script begins running, the positional parameters connot be used for obtaining more input from the
standard input. For this you have to use the read statement. Let’s modify the previous program to make
use of read:

#!/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:

$ display.args Welcome to the world of UNIX


The shell script would respond by prompting you for your name:
What is your name?
Suppose you were to type
Sachin Tendulkar
The computer would respond with
Well, Sachin Tendulkar, you typed 6 arguments:
Welcome to the world of UNIX

12.6 THE set COMMAND


The positional parameters are sometimes called read-only variables, because the shell sets their
values for you when you type arguments to the script. However, you can also set their values using the
set command. To illustrate this, consider the following shell script, which we will assume is in the file
setdate:
116 Chapter 12 - Shell Programming

#!/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

The output will look something like this:


Time:10:56:08 EST
Day: Fri
Date: 20 Aug 2004
What happened? Consider the command line

set ‘date‘
The backquotes run the date command, which produces output something like this:

Fri Aug 20 10:56:08 EST 2004

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

12.7 ARITHMETIC OPERATIONS USING THE expr UTILITY


The shell is not intended for numerical work-if you have to do a lot of calculations, you should consider
C, C++, or Fortran. Nevertheless, the expr utility may be used to perform simple arithmetic operations on
integers. (expr is not a shell command, but rather a separate UNIX utility; however, it is most often used
BSIT 43 UNIX and Shell Programming 117

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 (%).

12.8 CONTROL STRUCTURES


Normally, the shell processes the commands in a script sequentially, one after another in the order they
are written in the file. Often, however, you will want to change the way that commands are processed.
You may want to choose to run one command or another, depending on the circumstances; or you may
want to run a command more than once.
To alter the normal sequential execution of commands, the shell offers a variety of control structures.
There are two types of selection structures, which allow a choice between alternative commands:
l if/then/elif/else/fi
l case
There are three types of repetition or iteration structures for carrying out commands more than
once:
l for

l while

l until
118 Chapter 12 - Shell Programming

12.9 THE if STATEMENT AND test COMMAND


The if statement lets you choose whether to run a particular command (or group of commands),
depending on some condition. The simplest version of this structure has the general form

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

echo “Thank goodness it’s Sunday!”

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

Argument Test is true if ...


-d file fi1e is a directory
-f file fi1e is an ordinary file
-r file fi1e is readable
-s file File size is greater than zero
-w file file is writable
-x file fi1e is executable
nl -eq n2 integer n1 equals integer n2
nl -ge n2 integer n1 greater than or equal to integer n2
nl -gt n2 integer n1 greater than integer n2
n1 -le n2 integer n1 less than or equal to integer n2
n1 -ne n2 integer n1 not equal to integer n2
n1 -lt n2 integer n1 less than integer n2
sl = s2 string s1 equals string s2
sl !=s2 string s1 not equal to string s2
Table 12-1: Different operations performed by test operators

12.10 THE elif AND else STATEMENTS


We can make the selection structures much more elaborate by combining the if with the elif (“else if’)
and else statements. Here is a simple example:

#!/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.

12.11 THE case STATEMENT


The shell provides another selection structure that may run faster than the if statement on some UNIX
systems. This is the case statement, and it has the following general form:
case word in
pattern-l) command(s) ;;
pattern-2) command(s) ;;
……
……
pattern-N) command(s) ;;
esac

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.

Here is a simple shell script that uses the case statement:

#!/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.

12.12 for Loops


Sometimes we want to run a command (or group of commands) again and again. This is called
iteration, repetition, or looping. The most commonly used shell repetition structure is the for loop,
which has the general form

for variable in list


do

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:

$ ps –u kumar rao sachin

12.13 while Loops


The general form of the while loop is
122 Chapter 12 - Shell Programming

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

12.14 until Loops


Another kind of iteration structure is the until loop. It has the general form
until condition
do
command(s)
done
This loop continues to execute the command(s) between the do and done until the condition is true.
We can rewrite the previous script using an until loop instead of the while loop:
#!/bin/sh
# Print a message five times
count=5
until test $count -eq 0
BSIT 43 UNIX and Shell Programming 123

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:

shell script, comment, subshell

child process, positional parameter, selection structure

default case, repetition structure, iteration

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

using a (i) while loop (ii) for loop.


124 Chapter 13 - Developing Shell Scripts

Chapter 13

Developing SHELL Scripts

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.

13.1 CREATING EXECUTABLE FILE: chex


If you are planning to write a lot of shell scripts; you will find it convenient to have a script that makes
files executable. If we were to write our own man page for such a script, it might look like Listing 13-1A.

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

Listing 13-1A: Summary of the chex script.

124 Chapter 13 - Developing Shell Scripts


BSIT 43 UNIX and Shell Programming 125

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

Listing 13-1B: The chex script.

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:

chex is now executable:


-rwxr-xr-x 1 yourlogin 59 Date time chex

Now you can use chex to change the protections on other files.

13.2 ASSIGNING LABELS TO wc (Word Count) Command: mywc


The wc (“word count”) filter counts the words, lines, and characters in a file. For example, try running
wc on the chex file you have just created:
$ wc chex

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)

Listing 13-2A Summary of the mywc script.

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"

Listing 13-2B:The mywc script.

2. Make the script executable. Use chex for this:

$ chex mywc

mywc is now executable:


-rwxr-xr-x 1 yourlogin 59 Date time mywc
BSIT 43 UNIX and Shell Programming 127

3. Test the new script. You might run mywc on the file chex:

$ mywc chex

File: chex

Lines: 5

Words: 17

Characters: 84

13.3 DELETING FILES SAFELY: del


The rm command can be very dangerous because it allows you to remove a file, but does not give you
a way of getting back a file you may have removed accidentally. Most shells allow you to create an alias
for the rm with the -i (“interactive”) option; it will ask you if you are sure you want to remove the file in
question. But sh does not allow aliases. Let’s create a script that will duplicate the effect of the rm -i
command. The script will also tell what action has been taken.

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.

2. Make the script executable. Use chex for this:

$ chex del

del is now executable:

-rwxr-xr-x 1 yourlogin 347 Date time 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)

If there is no file with that name


print an error message
Otherwise
Ask if the user wants to delete the file
Read the user's choice (y/n)

If the choice is yes (y)


remove the file & print a message
otherwise
print a message

Listing 13-3A: Summary of the del script.

#!/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

Listing 13-3B: The del script


BSIT 43 UNIX and Shell Programming 129

13.4 A DAILY REMINDER SYSTEM: tickle


We have already seen how you can use the calendar utility to remind yourself of important events.
One limitation of calendar is that you have to enter each event individually, along with its date. This can
be a problem for routine events that happen every day or every week. For reminding yourself of such
events, the following tickle script can be very useful. It uses the date command to check the day of the
week, then prints out an appropriate message.

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

Listing 13-4A: Summary of the tickle script.

#!/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

Listing 13-4B: The tickle script.


130 Chapter 13 - Developing Shell Scripts

After studying the listings, create the script:

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.

2. Make the script executable. Use chex for this:

$ chex tickle

tickle is now executable:

-rwxr-xr-x 1 yourlogin 457 Date time tickle

3. Test the news script. Enter the tickle command:

$ tickle

This should print out the appropriate message for the day.

13.5 DISPLAYING ARGUMENTS MULTIPLE TIMES: echo.sh


The standard echo command echoes its arguments just once. The script presented in Listing 13-5A
and Listing 13-5B echoes its arguments as many times as the user chooses.

Be sure you understand the listings, then create the script:

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.

2. Make the script executable. Use chex for this.

$ chex echo.sh

echo.sh is now executable

-rwxr-xr-x 1 yourlogin 111 Date time echo.sh


BSIT 43 UNIX and Shell Programming 131

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 ($*)

While $count is greater than 0


do
print the message
subtract 1 from $count
done

Listing 13-5A: Summary of the echo.sh script.

#!/bin/sh
# Echo a line ‘n’ times
count=$l
shift
message=$*

while test $count -gt 0


do
echo $message
count=`expr $count – 1`
done

Listing 13-5B : The echo.sh script.

3. Test the new script. Try this:


$ echo.sh 5 Play it again, Sachin.

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.

Play it again, Sachin.


Play it again, Sachin.

Play it again, Sachin.

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.)

1. Rewrite tickle to use an if/then/elif.../fi structure.

2. Rewrite echo.sh to use an until loop.

3. Write shell scipt lshead.sh that uses ls –l command, to display the heading

for ls –l output.

m m m

Das könnte Ihnen auch gefallen