Shell Scripting

177
1 1.1 Introduction to Operating System An operating system is a program that manages the computer hardware. It also provides a basis for application programs and acts as an intermediary between a user of a computer and the computer hardware. Hence operating system is an important part of almost every computer system. A computer system can be divided roughly into four components: such as the hardware, the operating system, the application programs and the users as shown in figure. Figure: Abstract view of the components of a computer system The hard ware: The CPU, the memory, and the input/output devices – provides the basic computing resources. The Application programs: Such as word processors, spreadsheets, compilers and web browsers- define the ways in which these resources are used to solve the computing problems of the users. The operating system: controls and co-ordinates the use of the hardware among the various application programs for the various users. The components of a computer system are its hardware, software and the data. The operating system provides the means for the proper use of these resources in the operation of the computer system. An operating system is similar to government. Like a government, it performs no useful function by itself. It simply provides an environment within which other programs can do useful work. 1.2 Introduction to UNIX First, the word "UNIX"(UNICS – Uniplexed information and computing system) is a legally registered trademark belonging to The Open Group. In most general terms, UNIX (pronounced "yoo-niks") is a multi user, multi tasking and portable operating system. It is designed to facilitate programming, text processing and communication. The UNIX operating system, is a set of programs (or software) That controls the User 1 User 2 User 3 User N Compiler Assembler Text Editor …… Data base Operating System Computer Hardware System and Application Programs system

Transcript of Shell Scripting

Page 1: Shell Scripting

1

1.1 Introduction to Operating System

An operating system is a program that manages the computer hardware. It also

provides a basis for application programs and acts as an intermediary between a user of a computer and the computer hardware.

Hence operating system is an important part of almost every computer system. A

computer system can be divided roughly into four components: such as the hardware, the operating system, the application programs and the users as shown in figure.

Figure: Abstract view of the components of a computer system The hard ware: The CPU, the memory, and the input/output devices – provides the basic computing resources. The Application programs: Such as word processors, spreadsheets, compilers and web browsers- define the ways in which these resources are used to solve the computing problems of the users. The operating system: controls and co-ordinates the use of the hardware among the various application programs for the various users.

The components of a computer system are its hardware, software and the data. The operating system provides the means for the proper use of these resources in the operation of the computer system. An operating system is similar to government. Like a government, it performs no useful function by itself. It simply provides an environment within which other programs can do useful work.

1.2 Introduction to UNIX

First, the word "UNIX"(UNICS – Uniplexed information and computing system) is a legally registered trademark belonging to The Open Group. In most general terms, UNIX (pronounced "yoo-niks") is a multi user, multi tasking and portable operating system. It is designed to facilitate programming, text processing and communication.

The UNIX operating system, is a set of programs (or software) That controls the

User 1 User 2 User 3 User N

Compiler Assembler Text Editor …… Data base�������������

Operating System

Computer Hardware

System and Application Programs system

Page 2: Shell Scripting

2

computer, acts as the link between you and the computer and provides tools to help you do your work. Designed to provide an uncomplicated, efficient and flexible computing environment. This operating system was developed by Ken Thompson and Dennis Ritchie, at AT & T in bell labs. Initially this operating system was designed by using Assembly language hence it was non-portable. After developing the C language, the developers re-written the same operating system by using C. 1.3 Why UNIX?

UNIX is the most widely used computer Operating System (OS) in the world.

UNIX has been ported to run on a wide range of computers, from handheld personal digital assistants (PDAs) to inexpensive home computing systems to some of the worlds' largest super-computers. UNIX is a multi-user, multitasking operating system which enables many people to run many programs on a single computer at the same time. After more than three decades of use, UNIX is still regarded as one of the most powerful, versatile, flexible and (perhaps most importantly) reliable operating systems in the world of computing.

With this power, versatility and flexibility, some people who are new to UNIX and have little to no experience with command line interfaces find themselves intimidated. The intent of this hypertext is to remove the intimidation by presenting the UNIX OS in a clear, concise and organized manner.

As a new user to the world of UNIX, don't be afraid to try things. You, as a single user can not damage the system. So experiment and have fun, you'll have nothing to lose, and you will probably even learn something.

Before getting started, always, alwaysalways, alwaysalways, alwaysalways, always keep the following in mind UNIX is case UNIX is case UNIX is case UNIX is case

sensitive! sensitive! sensitive! sensitive! Always use the correct case (usually lower) !!!!!!!!!!!!. 1.4 UNIX Flavors

People new to UNIX are sometimes a little confused about the different names they hear used. People frequently ask "What's the difference between UNIX and Solaris and Linux?" The short answer is "for most users, not a great deal". All of today's varieties of UNIX are offspring from the original UNIX operating system created at Bell Labs. Over the years, specific universities and computer vendors made their own modifications to the UNIX source code to (hopefully) make the operating system run more efficiently (and faster) on their proprietary hardware. When the vendors sold their hardware, they also distributed (sometimes referred to as "bundled") their modified version (or flavor) of UNIX. All flavors of UNIX share basic features, which is an essential concept for the end user to understand. Several modern flavors of UNIX are listed below.

Originator Proprietary Name 1

U. Cal. Berkley BSD

Sun Solaris

IBM AIX

HP HP-UX

Page 3: Shell Scripting

3

SGI IRIX

Digital Equipment 2 DEC UNIX

Compaq Tru64 UNIX 3

Apple MacOS X 4

L. Torvalds/GNU 5 Linux 6

1 All proprietary UNIX flavors and their names are respective trademarks of the originating entity/vendor. 2 Digital Equipment Corp. (DEC) is no longer a corporate entity since being acquired by Compaq in 1998. Compaq then "merged" with Hewlett-Packard (HP) in 2001. 3 Compaq acquired DEC UNIX in its acquisition of DEC, and renamed this Tru64 UNIX, which now belongs to HP. However, both the DEC UNIX and the Tru64 UNIX flavors of UNIX are still in use. 4 Mac OS X is an offspring from a BSD parent. 5 This is not the place to put on the gloves and settle which group provided what. 6 Pronounced lih-nuhks with a short "i".

As Operating System portability became a focal point, several flavors of UNIX were "ported" to multiple hardware platforms. For example, Sun's Solaris will run on PC's as well as Sun's proprietary hardware. It is interesting to note at this point that Linux is a non-proprietary flavor of UNIX that has been ported to a plethora of different types of hardware devices, not just computers (e.g. PDA's, cell phones, Digital Video devices such as TiVo, etc.)

What is important for the UNIX end-user to keep in mind is that most basic (core) commands are similar (and most likely identical) on most UNIX systems.

1.5 Major Differences between UNIX and DOS

UNIX DOS Multi User Single User Strong Security Poor security Shell and AWK programming Batch Programming Strong File Manipulation Utilities Limited File Manipulation Utilities Strong Text manipulation facility(Filters) Text manipulation is difficult Message facility No Message facility Commands are case sensitive Commands are not case sensitive Root is / Root is \

1.6 UNIX system Architecture

The UNIX system has four major components: Figure: Model of a UNIX system

Page 4: Shell Scripting

4

1.6.1 The kernel

The nucleus of the UNIX system is called the kernel. It is a collection of

programs mostly written in C, which communicates with the hard ware directly. It is loaded into the memory when the system is booted.

It controls the computer resources: 1. Manages computer memory 2. Maintains the file system 3. Allocates the computer resources among users 4. Control access to computer

Hence it is a heart of a computer.

1.6.2 The shell

The shell is a program that allows users to communicate with the operating

system. The shell reads the commands entered by the user and interprets them as request to execute other programs, access files or provide output; hence they called as a command interpreter.

The shell is also a powerful programming language like C programming language.

1.6.3 Commands A program is a set of instructions given to the computer programs that can be executed by the computer without need for translation are called executable programs or commands.

Packages of programs are called tools. The UNIX system provides tools for jabs such as creating and changing text, writing programs developing software tools and exchanging information with others via computer. 1.6.4 The file system

The file system is a collection of all the files available on your computer. It allows you to store retrieve and manage information electronically. The file system in UNIX is hierarchical.

1.7 Features of UNIX operating system

1.7.1 Portability

It is written in C making it easy to port to different configurations.

Page 5: Shell Scripting

5

The UNIX OS is written in a modern, high-level programming language, specifically the C programming language. This makes it easy for programmers to read and modify the UNIX source code, and more importantly, port (Porting software means to move the original source code to a different hardware device (e.g. a different computer), modify the code as necessary, re-build the executable image (i.e. re-compile/link the source), and have the program provide the same services with the same reliability as on the original machine) this source code to other types of hardware. This accounts for its presence on a very wide range of computer hardware and other devices.

1.7.2 Multi user system

UNIX is a multi-user, Operating System, which allows multiple users to access and share resources simultaneously (i.e. concurrently), such as printers and file servers can be used by many users.

1.7.3 Multi tasking system

UNIX is a multitasking operating system, it allows user to execute more than one program at the same time. For Example: User can execute multiple applications at same time, such as user to edit a file, print another file on the printer, send email to a friend and browse the world wide web-all without leaving any of the applications.

1.7.4 The Building Block approach

The developer of UNIX operating system felt that “small is beautiful”, hence they developed a few hundreds of commands each of which performs one job only. UNIX operating system provides features that allow complex programs to be built s from combining more than one above said commands. This is achieved by using filters and pipes.

Example: $ who | wc -l

The above said command line is used to count numbers of user currently logged into the system. It combines two commands who and wc.

1.7.5 Hierarchical file system

It uses a hierarchical file system that allows easy maintenance and efficient implementation. It uses a consistent format for files, the byte stream, and making application programs easier to write.

1.7.6 Pattern Matching

It provides pattern matching facility like in Dos, through meta-characters, that are used to construct the generalized pattern for matching file names.

Page 6: Shell Scripting

6

Example: list all the file name begins with a name a a ax a1 a2 a3 ……aa ab.abc…Matches all the file names in the current directory begins with a followed by any number of characters including none.

1.7.7 Documentation

The reference manuals are available online. (Online documents are stored in our computer’s file system). So we can view executing the command man (short for manual page).

1.7.8 Programming facility

The UNIX shell is an interface, an interpreter but it also a programming language too, it supports variables, control structures, loops, functions and arrays etc. These features are used to design shell scripts.

1.7.9 UNIX tool kit

UNIX provides set of general purpose tools, text manipulation utilities called as filters, compilers and interpreters, networked applications and system administration tools and so on.

1.7.10 Interactive UNIX provides a simple, but powerful command line User Interface (UI).

Hence an interactive environment that allows user to communicate directly with the computer & receive immediate responses to you requests & messages.

1.8 Getting Started 1.8.1 Login Name: Account Name or User Name

A login name is the name by which the UNIX system verifies that you are an authorized user of the system when you request access to it.

The login names are provided by the system administrator. Rules for naming Login name.

• It is 3 to 8 characters long.

• It can consist of uppercase (A-Z), lowercase (a-z) letters, Numbers (0-9) and the underscore character (_).

• But it cannot start with a number.

1.8.2 Logging On

Since UNIX is a multi-user system, each individual user must identify themselves

Page 7: Shell Scripting

7

to the system for exclusive access to their accounts (and all corresponding files). This is done with a unique username and an associated password. Typically, your user name is assigned to you by a system administrator; your password being initially set by the administrator as well. Your password can then be changed (by you) at a later time (using the passwd command).

`To begin the login process, a user must establish a connection to the machine

they wish to login into. If the machine is physically near by, this may be as simple as powering the machine on and entering your username and password.

However, sometimes UNIX systems are physically remote from the user, so the

user must connect to the system using another method. One common way to do this (from a variety of different hardware systems) is via a telnet session (the session itself will consist of a client-side application and a server-side application). The telnet application is available on most machines, including WINDOWS operating systems (on WINDOWS, one can start telnet using the Start => Run=> telnet sequence). Once the telnet session is started, the user can then use the open command to specify the name (or IP address) of the desired machine to connect to.

Once the connections are made and the session is established, the user will see something similar to the following: Red Hat Linux release 7.3 (Valhalla) Kernel 2.4.18-3 on an i686 login:

At this point the user types their user name at the login: prompt followed by pressing the Enter key (denoted by [Enter]). Once this is complete, the user will see the Password: prompt, whereas the user enters their account password followed by the Enter key (see example below). Note that the password characters will not be displayed when typed. For security reasons, most systems will not display any characters when a password is entered, as opposed to ***** or the like being displayed. Why is this important? Red Hat Linux release 7.3 (Valhalla) Kernel 2.4.18-3 on an i686 login: karun [Enter] Password:

As previously mentioned, remember to always, always keep the following in mind. Once you have successfully logged into the system, you will see a string of characters on your screen. This character string is referred to as the command prompt or shell prompt. Following the command prompt is where the user types in (submits) commands to the system. The prompt may be a simple single character such as a $ or %, or it may be something more complex such as the following:

Page 8: Shell Scripting

8

$ Which would serve to indicate a user’s current location in the file system hierarchy.

1.8.3 Logging Off Logging off of a UNIX system is fairly straight forward, and can be accomplished in several ways.

The first way which should always work is by holding down the Ctrl key and pressing the character d key at the same time. This is commonly referred to and written as a [Ctrl-d] sequence.

Another technique that will typically work is by typing the command exit at the

command prompt followed by the [Enter] key.

$exit 1.9 On-line Manual

Before beginning the discussion of UNIX commands and command format in general, let's discuss where to find information about commands on a UNIX system. Information about interacting with a UNIX system is found in the on-line manual, or as they are commonly referred to, the man pages. Information about most (if not all) commands and topics are found in the man pages.

UNIX man pages are the key to understanding a UNIX system, so do not underestimate their value!

The command to invoke the manual pages is man, and the syntax for using the

man command is:

$ man command you want to see the manual entry for [Enter] For example, the command: $ man man [Enter]

Will display the manual pages for the man command. Similarly, the following will display the manual pages for the ls command: $ man ls [Enter]

Commands for basic navigation through the man pages for a specific command are as follows:

• The Enter key will move you forward, line-by-line through the pages for that command

Page 9: Shell Scripting

9

• The Space Bar or the f key will move you forward, page-by-page for the pages for that command

• The b key will move you backward, page-by-page for the pages for that command

• The q key will allow you to quit from a man page and return you to the shell

1.10 Basic Commands 1.11 clear : Clears the terminal screen. $clear

1.12 cal : The calendar This command displays calendar of any specific month or a complete year calendar. It can be invoked in different manner. 1. cal without arguments displays the calendar of the current month.

$cal

July 2007 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2. cal with two arguments First is month and second argument is year

$cal 8 1947 # displays calendar for the Month of August 1947 August 1947 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3. cal with one argument is year - to display complete year calendar $cal 2006 # displays complete calendar of the year 2006 Note: In Linux to display calendar of a particular month in a year use month numbers 1-12, along with the year. In UNIX use three characters Month name with year. Example: $cal Aug 1947.

Page 10: Shell Scripting

10

1.13 date: display the system date & time $date

Sun Jul 22 08:32:40 IST 2007

date command can be used with suitable format specifiers as argument. Each

format is preceded by a + symbol, followed by the % operator, and a single character describing the format.

+%a abbreviated weekday name(Mon…Sun) +%h month Name (Jan …Dec) +%m month Number (1…12) +%d day of month (1…31) +%y last two digits of year (00...99) +%Y year (four digits) +%H hours +%M minutes +%S seconds +%D date (mm/dd/yy)

$date +%a

Sun

$date +%h

Jul

$date +%y

07

Or you can combine more than one in one command $date +"%a %h %Y"

Sun Jul 2007

$date +"Date is: %a / %h / %Y"

Date is: Sun / Jul / 2007

$date +"Date is: %a / %h / %Y and Time is %H:%M:%S"

Date is: Sun / Jul / 2007 and Time is 08:42:04

$date +"Date is %D"

Date is: 07/22/07

1.14 passwd : Change your password This command is used to change the password. Each password should satisfy following rules.

Page 11: Shell Scripting

11

• Each password must have at least 8 characters

• Each password must contain at least two alphabetic characters & at least one numeric or special character. Alphabetic character can be upper case or lower case letters.

• A new password must differ from the old by at least three characters.

$ passwd

Old password:

New password:

Re-enter new password:

$

1.15 tput : tput-initialize a terminal or query terminfo-data base tput uses the term info database to make the values of terminal dependent capabilities & information available to the shell, to initialize or reset the terminal. $tput init #initializes the terminal

$tput clear #it clears the current terminal screen Synatx: tput cup <row_coordinate> <column_coordinate> $tput cup 10 20 # you can use the cup argument to position the cursor at row

10 and column 20. 1.16 lock : Lock your terminal

You sometime need to be away from you terminal for a while, &don’t want to log out because a job is running in the background.

For this purpose UNIX provide a command called lock, which requires you to enter a password when you decide to lock:

$lock

passwd:******

Re-enter password:******

The $ prompt disappears, and the system will remain locked in this condition for

30 minutes. If you are not back by that time, then it will log you out. Any time before that you can unlock the terminal simply by re-entering the same password. ******

The passwords that lock prompts for need not be the same as that set with the passwd command. You may decide to set the duration of the lock (not exceeding 60 minutes) by using an option.

Page 12: Shell Scripting

12

$lock -45 #Locks it for 45 minutes 1.17 bc : The calculator bc is actually a preprocessor for the desk calculator program. UNIX provides two calculators:

1. a graphical object (The xcalc command) xcalc: is available in the Xwindow system and quite easy to use.

2. bc command: is less friendly, but extremely powerful. When you invoke bc without arguments, the input has to be keyed in, each line terminated by pressing ENTER. After you have finished your work, use ^d to quitting from bc. $bc

12 + 5 # Value displayed after computation 17

^d

$

You can also ask bc to perform the calculations together. $bc

12*2 ; 2^4 # ^indicates to the power of 24

16

9/5

1 #decimal portion is truncated

By default bc performs truncated division, You have to set scale to the number of digit of precision before you perform any divisions. scale =2 # Truncate to 2 decimal places 17/7

2.42 # Not rounded off, result is actually 2.42857…

Using bc you can also convert numbers in one base to another base. You may need to convert binary number to decimal set ibase (input base) to 2 before you provide the number. ibase=2

11001010

202 #Output in decimal ie base 10 The reverse is also possible, this time with obase:

Page 13: Shell Scripting

13

obase = 2

14

1110 #binary of 14 In this way you can convert from one base to the other (not exceeding 16). obase = 16

14

E #Hexadecimal value of 14.

bc can also be used with variables that retain their values until you exit the program. bc supports only single lower case letters a-z. x = 3; y=4; z = 5

p = x + y + z

12

bc is nearly a programming language featuring arrays, functions, conditionals (if) & loops such as for while ,it also comes with a library for performing scientific calculations.

1.11 Command structure / command line syntax General syntax of a UNIX system command line is. $command option(s) argument(s)

On every UNIX system, on command line you must type at least two components: A command name and the return key[ENTER KEY]. Command line may also contain either options or arguments, or both.

A command is the name of the program or UNIX command you want to run.

• An option modifies how the command runs. Or change the default output of a command these are prefixed by –(hyphen).

• An argument specifies data that the command processes, usually the name of a directory or file.

In command line that includes options and/or arguments, the component words are separated by at least one blank space. If an argument name contains a blank, enclose that name in double quotation marks. Example: If the argument to your command is sample 1, you must type it as follows: “Sample 1”. If you forget the double quotation marks, the shell will interpret sample 1 as two separates arguments. Some commands allows you to specify multiple options (begins with - ;(hyphen) and or arguments on a command line.

Page 14: Shell Scripting

14

Consider the following command line: $ls -l -i file1 file2 file3 | options | command arguments

In the above said example, the ls command is used with two options, -l and -i, to list information about file1, file2 and file3. The -l option provides information in a long, format, including such things as mode, owner, links, group, size types and the -i option prints the inode number. (The UNIX system usually allows you to group options such as these to read -li, and to enter them in any order).

Note: Most options can be grouped together, but argument cannot.

Example: following example shows the valid command lines

$wc # Only a command Hello how are you # Reading data from keyboard

^d #Terminating input from keyboard press ^d

1 4 18 # Number of lines, words and characters

$wc -l # command with an option Hello how are you

^d

1 # Only number of lines

$wc -l -w # Command with an options $ wc -lw Hello how are you

^d

1 4 # number of lines and words

$wc file # command with an argument (file name) 4 14 124 file

$wc -l file # command with an argument and an option 4

$wc -l filea fileb # command with an option and arguments 2 filea

3 fileb

5 Total

$wc -lw filea # command with an options and an argument 2 5 filea

Page 15: Shell Scripting

15

$wc -lw filea fileb # command with an options and an arguments 2 5 filea

3 8 fileb

5 13 Total

Note : In the first three examples, command reads input data from keyboard 1.111 cat - concatenate files and print on the standard output This command used for:

1. Used for creating a small file 2. Display the contents of file 3. Used for appending data 4. Used for concatenating files

Used for creating a small file: Syntax is $cat filename Enter the command cat on the command prompt followed by > character and the filename then press enter. Now cat is waits for input from the user. Enter the input finally press ^d(CTRL-d) to terminate the end of the input to the file.

$cat > filea

hello good morning

this is the first file in UNIX.

^d

$

Used for Display the contents of file: Syntax is $cat filename It is mainly used to display the contents of a small file on the terminal. $cat filea

hello good morning

this is the first file in UNIX.

$

Used for Appending data to the existing file: Syntax is $cat >>filename Enter the command cat on the command prompt followed by >> character and the filename then press enter. Now cat is waits for input from the user. Enter the input,this entered input is appending from the end of a file. Finally press ^d(CTRL-d) to terminate the end of the input to the file. $cat >>filea

created by cat command.

^d

Page 16: Shell Scripting

16

$

$cat filea

hello good morning

this is the first file in UNIX.

created by cat command.

$

Used for concatenating file: Syntax is $cat file1 file2 file3.... In this case cat displays the contents of file1, immediately file2 contents with out any header information then file3 and so on as shown below. $cat fileb

this is second file

$cat filec

this is filec

$cat filea fileb filec

hello good morning #contents of three files filea, fileb and filec this is the first file in UNIX.

created by cat command.

this is second file

this is filec

1.112 more : paginating output This is similar to pg command in DOS. Display the file contents page at a time from the beginning of a file. If the file is too large, in the cat command we can see on screen only the last page. $more filename

You will see the contents of filename on the screen, one page at a time. At the bottom of the screen, you will also see the filename and percentage of the file that has been viewed. --More--(17%)

More has having so many internal commands to navigate in a file, such as: f or spacebar – Move forward one page b - Move backward one page 12f - Scrolling 12 pages forward 10b - Scrolling 10 pages backward /pat - Searching for a pattern pat q - Used to exit more

Page 17: Shell Scripting

17

1.113 alias : Used to create an alias name for the existing commands For example: $alias dir=”ls -l”

Now dir command behaves exactly as ls -l, means displays directory contents in

long format. $alias rm=”rm -i”

When ever your using the command rm to delete a file(s), it will gives you warning just like rm -i. We can display the definition of an alias names: $alias rm

rm=”rm -i”

$alias dir

dir=”ls -l”

$alias –a # Used to list all alias names on the system Future if you don’t want to use aliases you can remove it by using the command. $unalias dir Now if you use command dir systems prompts you following error message. $dir

bash: dir: command not found

1.114 script - make typescript of terminal session

This is the command used to records your login session in a file. If the argument file is given, script saves all dialogue in named file. If no file name is given, the typescript is saved in the file typescript. Starts the recording by invoking the command script. $script

Script started, file is typescript

Page 18: Shell Scripting

18

$date

Tue Jun 5 18:08:11 IST 2007

$who

root tty1 Jun 5 18:01

chandu tty2 Jun 5 16:16

root pts/0 Jun 5 18:03

root :1 Jun 5 16:30 (localhost)

root pts/1 Jun 5 16:30

root pts/2 Jun 5 18:03 (:0.0)

$wc mn.doc

3 11 56 mn.doc

$exit # Terminate the recording session by exit or Ctrl-d exit

Script done, file is typescript

$

Now you can view this file with the cat command along with the file typescript. $cat typescript

Script started on Tue 05 Jun 2007 06:08:04 PM IST

$date

Tue Jun 5 18:08:11 IST 2007

$who

root tty1 Jun 5 18:01

chandu tty2 Jun 5 16:16

root pts/0 Jun 5 18:03

root :1 Jun 5 16:30 (localhost)

root pts/1 Jun 5 16:30

root pts/2 Jun 5 18:03 (:0.0)

$ wc mn.doc

3 11 56 mn.doc

$exit

exit

Script done on Tue 05 Jun 2007 06:08:54 PM IST

The script overwrites any previous typescript that may exist. If you want to

append to it use the following command line. $script -a # Appends to existing file typescript or if you want to use different file, use the following command line. $script filename # now your session is recording in a filename.

Page 19: Shell Scripting

19

1.115 uname - print system information -a, all print all information, in the following order: -s, kernel-name print the kernel name -n, node name print the network node hostname -r, kernel-release print the kernel release -v, kernel-version print the kernel version -m machine print the machine hardware name -p, processor print the processor type -i, hardware-platform print the hardware platform -o, operating-system print the operating system $ uname

Linux

$ uname -a

Linux linux 2.6.8-24-smp 1 SMP Wed Oct 6 09:16:23 UTC 2004

i686 i686 i386 GNU/Linux

$ uname -s

Linux

$ uname -n

linux

$ uname -r

2.6.8-24-smp

$ uname -v

1 SMP Wed Oct 6 09:16:23 UTC 2004

$ uname -m

i686

$ uname -p

i686

$ uname -i

i386

$ uname -o

GNU/Linux

1.116 tty - print the file name of the terminal connected to standard input Here tty stands for teletype (Television and Type writer) $ tty

Page 20: Shell Scripting

20

/dev/pts/2

In the above example the terminal filename is 2 stored in the pts directory; this is

the directory in turn stored in /dev directory. 1.117 stty - change and print terminal line settings -a, all print all current settings in human-readable form -g, save print all current settings in a stty-readable for -F, file=DEVICE open and use the specified DEVICE instead of stdin 1. Print the existing terminal settings $stty -a

speed 38400 baud; rows 54; columns 166; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke 2. Changing the terminal settings $stty –echo

After this, the keyboard entry is not echoed on the terminal. So in order to display

on terminal you should turn on echo command by using the command stty echo. $stty eof \^y

Now end of file is set to ^y, by default eof is set to ^d. 1.118 echo - display a line of text Echo the STRING(s) to standard output. It uses following options. -n do not output the trailing new line -e enable interpretation of the backslash-escaped characters listed below

Escape Sequence Meaning

\\ Backslash \a Alert (BEL) \b Backspace

\c Suppress trailing new line \f Form feed \n New line

Page 21: Shell Scripting

21

\r Carriage return \t Horizontal tab \v Vertical tab \033[0m Normal characters \033[1m Bold characters \033[4m Underlined characters \033[5m Blinking characters \033[7m Reverse video characters

Hence it is just like a printf function in C but it wont supports format specifiers. $echo hello

hello

Prints the string hello on the terminal $echo how are you

how are you

Prints the sentence hello how are you on the terminal without quotes also. $ echo hello how are you

hello how are you

Prints the sentence hello how are you on the terminal but removes number of blanks by single blank. $echo "hello how are you"

hello how are you

$ echo 'hello how are you'

hello how are you

$echo -e 'hello\thow\tare you'

hello how are you

$ echo -e "hello\thow\tare you"

hello how are you

$ echo -e "\n\nhello\thow\tare you\n"

hello how are you

Note: Double or single quotes are used to preserve number of blanks as it is and to use escape sequence characters.

Considering the above said examples along with quotes there is no differences in

Page 22: Shell Scripting

22

the two types of quotes. $echo -e "\n Todays date is `date`\n"

Todays date is Tue Jun 5 19:11:47 IST 2007

$echo -e '\n Todays date is `date`\n'

Todays date is `date`

Consider above said two examples echo command with command substitution quotes (back quotes) , there will be a meaning for back quotes along with double quotes but there is no meaning with single quote, hence in the first example date command output is substituted to echo but not in second example because along with single quotes back quotes are loose their meanings.

Chapter 2. The Editors 2.1 Introduction

Text editors are application programs which allow users to create new text files and/or modify the contents of existing text files. Many of us have used simple text editors, perhaps a popular one such as Microsoft's Notepad text editor.

UNIX has many text editing applications available for use. The two most popular of these are vi (whose name comes from VIsual editor) and emacs (whose name is derived from Editor MACroS). The vi editor is typically a non-GUI based tool, whereas emacs is available in both GUI and non-GUI modes (however, most users prefer the GUI mode of emacs). Both of these text editing applications are included in most, if not all UNIX installations. Since this text is focused upon command line (i.e. text) based applications, the discussion of text based editor programs will focus upon the vi editor.

Functions of an Editor:

• Create a file

• Open an existing file

• Copy & Pas te text

• Search for text

• Handle a large amount of data 2.2 The vi editor

The vi editor is a visual editor used to create and edit a text file, documents & programs. In UNIX vi is a symbolic link to the vim editor, which is an improved version of vi editor.

vi is the screen editor : It works in three modes – command, text, exit (escape).

Page 23: Shell Scripting

23

1. Command mode – a default mode of the editor where every key is interpreted as a

command to run on a text. In the command mode you can: delete, move, and copy text move around in the file

2. Text mode/Input mode – Every key is pressed after switching to this mode

actually shows up as text. In this mode insert, append, replace, open of text operation are handled.

3. Ex mode – This mode is used to handle files such as: text search, changing visual

interface, saving & quitting is handled. Pressing a : in the command mode invokes this mode. You then enter an exit mode command followed by Enter.

These modes can be represented diagrammatically as shown bellow:

2.2.1 Creating a File

First, enter the editor; type vi and the name of the file you want to create or edit $vi filename

For example, suppose you want to create a file called stuff. When you type the vi command with the filename stuff, vi clears the screen and displays a window in which you can enter and edit text as shown below. $vi stuff

Input/Insert Mode

Ex-Mode

Command Mode

$vi filename Shell

ESC a,A,i,I,o,O r,R,s,S

ESC , w

q, q!, wq, x

Page 24: Shell Scripting

24

For text editing, vi uses 24 of 25 lines, 25th line is dedicated to display messages Such as filename, cursor position value, Modes size of a file and also used to execute exit mode commands. A line starts with ~ indicates corresponding line is empty.

If you have successfully entered vi, you are in the command mode and vi is waiting for your commands. How do you create text?

Type a to enter the input mode of vi. (Do not press the RETURN key.)You can now add text to the file. (a is not printed on the screen.) Type some text.

To begin a new line, press the RETURN key.

2.2.2 How to Leave the Append Mode

When you finish creating text, press the ESCAPE key to leave the input mode and return to the command mode. Then you can edit any text you have created or write the text that is in the buffer to a file.

If you press the ESCAPE key and a bell sounds, you are already in the command mode. Pressing the ESCAPE key while you are in the command mode does not affect the text in the file, even if you do it several times. 2.2.3 Quitting vi

When you have finished your text, you will want to write the buffer contents to a file and return to the shell. To do this, hold down the SHIFT key and press Z twice (ZZ). The editor remembers the file name you specified with the vi command at the beginning of the editing session, and moves the buffer text to the file of that name. A notice at the

|~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [NEWFILE]

Page 25: Shell Scripting

25

bottom of the screen gives the file name and the number of lines and characters in the file. Then the, shell gives you a prompt.

You can also use the :w and :q commands of the line editor for writing and quitting a file ( Line editor commands begin with a colon and appear on the bottom line of the screen). The :w command writes the buffer to a file. The :q command leaves the editor and returns you to the shell. You can type these commands separately or combine them into the single command: wq. 2.3 Input Mode Commands

Command Function i Inserts text to left of cursor (Existing text shifted right) I Inserts text at beginning of line (Existing text shifted right)

a Inserts text to right of cursor (Existing text shifted right) A Inserts text at end of line

o Opens line below

O Opens line Above rch Replaces single character under cursor with ch(No Esc is required)

R Replaces text from cursor to right (Existing text overwritten)

s Replaces single character under cursor by any number of characters S Replaces entire line

2.4 Exit Mode Commands Command Action :w Saves file and remain in editing mode :x Saves file and quits editing mode :wq Write and quit editing mode(same as x) :w file Write to file (like save as in windows) :w! file Same as above but overwrites existing file :q Quits editing mode when no changes are made to file :q! Quits editing mode but only after abandoning the changes :n1 ,n2w file Writes lines n1 to n2 to file :.w file Writes current line to file :$w file Writes last line to file :sh Escapes to UNIX shell :13 Place a cursor at line 13 :r ab.c Reads file ab.c below current line :r !head -3 ab.c

Reads first three lines of ab.c below current line

:w >> xy.c Appends buffer contents to a file xy.c :e xy.c Stops editing current file and edits file xy.c :e! xy.c As above but after abandoning the changes made to current file

Page 26: Shell Scripting

26

:n Edits next file(if vi invoked with more than one filename) :rew Rewinds file list to start editing from first file specified in command line

2.5 Command Mode Commands

Command Action 1. Line Navigation Commands: Moving the cursor is done using various alpha-numeric keystrokes. Specifically k nk moves cursor up , moves cursor n lines up j nj move cursor down , moves cursor n lines down h nh move cursor left , moves cursor left n columns J nJ Joining current line and next line, Joins current + n-1 lines l nl move cursor right, moves cursor right n columns 2. Word Navigation Commands : b moves back to the beginning of word w moves forward to the beginning of word e moves forward to the end of word 0 or | moves to beginning of line $ moves to end of line G moves to end of file 20G moves to line 20 ^f Scrolls forward ^b Scrolls back word ^d Scrolls half page forward ^u Scrolls half page back word 3. Editing Text x Deletes a single character dd Deletes a line 3dd Deletes 3 lines d$ or D Delete from the cursor position to the end of the line dw Delete word 3dw Deletes 3 words dG Delete from the cursor position to the end of the File P and p Paste text on right and left of cursor when you delete parts

Of lines such as using the command dw or x. Paste text above or below the cursor when you deletes entire line or lines using commands dd.

yy yanks current line (Copying current line), then you can paste using p or P.

nyy Yanks n lines c$ Changes from cursor to end of line 3cw or c3w Changes three words cc Changes current line u Undoing last editing instructions . Repeating the last command /pat Search pattern pat forward ?pat Search pattern pat back word

Page 27: Shell Scripting

27

n or N Repeating the last pattern search. n – repeats search in same direction of original search N - repeats search in opposite direction of original search

!!tr ‘[a-z]’ ‘[A-Z]’ Converts current line to uppercase 2.6 Substitution – search and replace ( :s)

This is exit mode command using this you can replace a pattern in the file with something else. :address/source_pattern/target_pattern/flags

The source_pattern is replaced by target_pattern in all lines specified by address. The address can be one or pair of numbers, separated by a comma. For example 1,$ addresses all lines in a file. The most commonly used flag is g, which carries out the substitution for all occurrences of the pattern in a line. Example: :1,$s /UNIX/Linux/g

In the above all occurrences of UNIX are replaced by Linux globally throughout

the file. If the pattern not found vi responds with the message Substitute pattern match failed.

The target pattern is optional. If you leave it out, then you’ll delete all instances of the source pattern in all lines matched by the address:

:1,50s/UNIX//g Deletes UNIX everywhere in lines 1 to 50 :.s/UNIX/Linux Substitute only the current line :$s/UNIX/Linux/g Substitute only the last line Interactive substitution: by adding c (confirmatory) parameter as the flag at the

end. :1,$s /UNIX/Linux/gc Each line is selected in turn, followed by a sequence of carets in the next line, just

below the pattern that requires substitution. The cursor is positioned at the end of this caret sequence, waiting for your response.

UNIX is a multi-user operating system ^^^^^y UNIX is a multi-tasking operating system ^^^^^n A y performs the substitution, any other doesn’t. This sequence is repeated for

each of the matched lines.

Page 28: Shell Scripting

28

Chapter 3. The UNIX File System 3.1 Introduction

In UNIX everything can be treated as files. Hence files are building blocks of UNIX operating system. When you execute a command in UNIX, the UNIX kernel fetches the corresponding executable file from a file system, loads its instruction text to memory and creates a process to execute the command.

The UNIX file system is a methodology for logically organizing and storing large quantities of data such that the system is easy to manage. A file can be informally defined as a collection of (typically related) data, which can be logically viewed as a stream of bytes (i.e. characters). A file is the smallest unit of storage in the UNIX file system.

By contrast, a file system consists of files, relationships to other files, as well as the attributes of each file. File attributes are information relating to the file, but do not include the data contained within a file. The file system in UNIX is a collection of all types of files discussed below. The UNIX file system is a hierarchical arrangement of directories and files. Everything starts in the directory called root, denoted by ‘/’. Each intermediate node in a file system tree is a directory file. The leaf nodes of a file system tree are either empty directory files or other types of files. The Root directory(/) has a number of subdirectories under it. These subdirectories again containing more subdirectories under it. On the file system apart from root, must have parent. For example home directory is a parent of chandu, while /, is the parent of home, and the grandparent of chandu. For the file ab.c chandu is a parent.

/ ( Root )

/bin /dev /etc /home /temp /usr date cat who dsk rdsk passwd shadow chandu bindu include lib

Reva ab.c cse ise

xyz.c

Figure: The UNIX File System

Page 29: Shell Scripting

29

Figure: Example of a UNIX files system Notice that the root directory ( / )containing several important system directories. / : Root directory /bin : Short for binaries, this is the directory where many commonly used like cat, date, who, cp, etc. /dev : Stores all the character and device files. /etc : Stores system administrative commands and files. /etc/group : Stores all group information. /etc/passwd : Stores all users’ information. /etc/shadow : Stores user passwords. /home : contains user directories and files /mnt : contains device files related to mounted devices root : the root users' home directory (note this is different than /) /sbin : system binary files reside here. If there is no sbin directory on your system, these files most likely reside in etc. /tmp : storage for temporary files which are periodically removed from the file system /usr/include : Stores standard header files. /usr/lib : Contains standard libraries. /stand : Contains bootable programs and data files used in the booting process. 3.2 File Names In order to open a connection to a file, or to perform other operations such as deleting a file, you need some way to refer to the file. Nearly all files have names that are strings, even files which are actually devices such as tape drives or terminals. These strings are called file names. You specify the file name to say which file you want to open or operate on. The only two characters can’t be appear in a file names are the slash character ( / ) and the NULL character. The slash separates the file names that form a pathname and the NULL character terminates a pathname. Hence the legal file names characters are: A to Z, a to z, 0 to 9 and _. Two file names are automatically created after creating new directory are called: . (dot) and . . (dot-dot) refers to the current and parent directory respectively. Some UNIX systems restrict a file name to 14 characters, but some versions extended this limit to 255 characters. Hence file name may not exceed NAME_MAX characters and the total number of characters in a path name may not be exceeding PATH_MAX in UNIX. 3.3 Path Name

Page 30: Shell Scripting

30

A sequence of one or more filenames separated by slashes and optionally starting with a slash (/) character forms a path name. A path name that begins with a slash called an absolute pathname (Example: /home/chandu/reva/cse/xyz.c), otherwise it called as relative path name (Example: $cd .. , $ ls . , $ cat cse/xyz.c).

First slash in an absolute path name indicates root directory, and subsequent slashes represents directory separators. Example for path names: /a The file named a, in the root directory. /a/b The file named b, in the directory named a in the root directory. a The file named a, in the current working directory. /a/./b This is the same as /a/b. ./a The file named a, in the current working directory. ../a The file named a, in the parent directory of the current working directory. 3.4 type : Locating commands

This command is used to search the command(executable files) in the file system and display the location(pathname). For example: $type ls

ls is /bin/ls

The above output indicates, ls is a command stored in the directory bin under root directory. 3.5 Home Directory

When we log in, the working directory is set to home directory, called as Login directory. It is created by the system when new user account is opened.

For example when user chandu logs into a system, he lands up in a directory

/home/chandu. The shell variable HOME holds your home directory. When chandu executes the command echo $HOME it returns his home directory. $echo $HOME

/home/chandu

3.6 Working Directory or Current Directory Each process has associated with it a directory, called its current working directory or simply working directory that is used in the resolution of relative file names.

Page 31: Shell Scripting

31

When you log in and begin a new session, your working directory is initially set to the home directory associated with your login account in the system user database. Users can change the working directory using shell commands like cd.

At any time you can know your current directory, by invoking the command pwd (print working directory). Like HOME, pwd also displays the absolute path name. $pwd

/home/chandu

Commands for handling Directories 3.7 ls : listing directory contents

$ls # In UNIX list files in a single column

ab.c reva # this is same as $ls -x in Linux

$

Files are arranged in the ascending order numerals, uppercase finally lowercase. ls options: option Meaning -a Shows all file names beginning with a dot ,including . -d dirname Lists only dirname if dirname is a directory -F Marks executables with *, directories with / and symbolic links with

@ -i Displays inode number -l Long listing – Displays seven attributes of a file -R Recursive listing -t Sorts file name by last modification time -u Sorts file name by last access time -x Displays directory contents in multiple columns List all files; filenames begins with . are hidden files $ ls -a

. .. ab.c .bash_logout .bash_profile .bashrc reva

Lists directories of directory reva $ls reva

cse ise

Page 32: Shell Scripting

32

Lists directories by appending / at the end of a filename, for executables appends * and for symbolic links appends @ sign at the end of a file name. $ ls -F

ab.c reva/

Lists file names along with their respective inode numbers. $ ls -i

361623 ab.c 361725 reva

Displays all the attributes of a file $ ls -l

total 8

-rw-r--r-- 1 kishan kishan 22 Jul 27 12:13 ab.c

drwxr-xr-x 4 kishan kishan 4096 Jul 27 11:48 reva

Recursively listing the directory contents $ls -R

.:

ab.c reva

./reva:

cse ise

./reva/cse:

xy.c

./reva/ise:

Listing the directory contents in reverse order $ ls -r

reva ab.c

3.8 mkdir : Creating Directories Enables you to make new directories and subdirectories within your current directory. $mkdir option(s) directory_name(s) $mkdir reva # Creates a directory reva under current directory $

The second prompt shows that the command has succeeded, means subdirectory

Page 33: Shell Scripting

33

reva has been created. $mkdir cse5 cse3 cse7 # Creates three directories under current directory $

$mkdir ise ise/ise1 ise/ise3 # Creates a directory tree. $

This creates three directories such as ise and two subdirectories - ise1 and ise3

under ise. Some times the mkdir command is fail to creates a directory for example:

$mkdir xyz

mkdir: cannot create directory ‘xyz’: File exists

$mkdir /home/ISE

mkdir: cannot create directory ‘/home/ISE’: Permission denied

The above said example clearly indicate in the first case it is fail to create a directory because already directory is existing with the same name. In the second case ordinary user tries to create a directory under system directories hence due to lack of privileges once again it is fail to create a new directory. 3.9 cd: Changing the directory

Using this command we can move around the file system. This command can be used with or without using arguments. 1. Used with an argument

$cd directory-name or path-name

It changes the current directory to the directory-name or pathname-name.

For example, user chandu is in home directory, when ever chandu invoke the following command, changes his directory to reva directory. $cd reva

$pwd

/home/chandu/reva

2. To change to parent directory, chandu has to invoke the following command. $cd ..

Suppose chandu wants to switch to the directory bin, he has to invoke the

following command,

Page 34: Shell Scripting

34

$cd /bin

$pwd

/bin

3. Used without argument: Places user to his home directory. $pwd

/home/chandu/reva/cse

$cd

$pwd

/home/chandu

3.10 rmdir : removes empty directories

Users can remove directories from the file system using the rmdir command as follows: $rmdir directory-name(s)

user can also delete a directory tree. $rmdir ise/ise2 ise/ise3 ise

$

This removes three directories such as ise and two subdirectories under ise. Some times the rmdir command is fail to remove directories: 1. Unless it is empty $rmdir cse

rmdir: directory “cse” : Directory not empty

2. Unless you are placed in a directory which is hierarchically above the directory you want to be delete. $pwd

/home/chandu/cse

$rmdir cse

rmdir: directory “/home/chandu/cse” : Directory does not exist

Commands for handling ordinary files 3.11 cp : copy files and directories

Page 35: Shell Scripting

35

Once files are created and saved, users typically find the need to make copies of

various files. Files are copied in UNIX using the cp command as follows: Copies contents of source_file to destination_file $cp source_file destination_file

If the destination file doesn't exist, it will first creates before copying takes place. if not, it will simply be overwritten without any warning from the system.

Copies contents of source_files to destination_directory $cp source_files destination_directory For example: $cp file1 file2 file3 CSE

Copies file1,file2 and file3 from current directory to CSE directory, and retains the same file names. $cp file1 file2 file3 CSE/f1 f2 f3

Copies file1, file2 and file3 from current directory to CSE directory, with file1 to f1, file2 to f2 and file3 to f3. Copies contents of source_file to destination_file with interactive -i This option warns the user before overwriting destination file. If destination file exists. $cp -i source_file destination_file cp: overwrite `destination_file`? y A y at this prompt overwrites the file, else leaves it un-copied. Copying directory structures using -R $ cp -R source_directory destination_directory 3.12 rm : remove files or directories

rm removes each specified file or files. By default, it does not remove directories.

$rm file(s)

But deletes the directory user has to invoke the command

$rm -r directory_name # -r or -R recursively

Page 36: Shell Scripting

36

$rm -i * # removes all files with interactive 3.13 mv: renaming and moving files Rename oldname to newname, or move file(s) to DIRECTORY. Before applying the command mv contents of directories are: $ls -R

.:

ab.c reva

./reva:

cse ise

./reva/cse:

xy.c

./reva/ise:

$

It performs two functions:

1.Renames the file or directory $mv oldname newname

$mv ab.c filea.c #renames regular file name ab.c to

#filea.c

$mv reva CIT #renames directory name reva to CIT

After executing mv command contents of directories are as shown below.

$ ls -R

.:

CIT filea.c

./CIT:

cse ise

./CIT/cse:

xy.c

./CIT/ise:

2. Moves file or files to directory

Page 37: Shell Scripting

37

$mv file(s) directory_name

$mv filea.c CIT #moves filea.c to CIT

After mv now the contents of directory are:

$ ls -R

.:

CIT

./CIT:

cse filea.c ise

./CIT/cse:

xy.c

./CIT/ise:

$ cat >mn.c #creating file under current directory

hello

how are you?

^d

$ ls -R # Now Contents of directory are:

.:

CIT mn.c

./CIT:

cse filea.c ise

./CIT/cse:

xy.c

./CIT/ise:

Moving and Renaming:

$mv mn.c CIT/fileb.c

$ls -R

.:

CIT

./CIT:

cse filea.c fileb.c ise

./CIT/cse:

Page 38: Shell Scripting

38

xy.c

./CIT/ise:

3.15 wc – Word Count , print the number of lines, words, and bytes(characters) in files Syntax: $wc option(s) file(s)

Print newline, word, and byte counts for each file, and a total line if more than one file is specified. With no file, or when file is -, read standard input. -c : Bytes : Print the number of characters in a file includes space, tab, and newline. -l : Lines : Print the number of lines in a file -w : Words : Print the number of words in a file For example: $ cat ab.doc first file in UNIX

am appending data to the file using cat command

$ cat xy.doc a second file in UNIX

$ wc ab.doc 2 13 67 ab.doc # First column indicates number of Lines, second column # indicates number of words, and third column indicates number # of bytes in a file. $ wc -l ab.doc 2 ab.doc # With the option -l displays only number of lines in a file. $ wc -c ab.doc 67 ab.doc # With the option -c displays only number of bytes in a file. $ wc -lc ab.doc 2 67 ab.doc # With the options -l -c displays both lines and number of bytes in a file. $ wc ab.doc xy.doc 2 13 67 ab.doc # Number of lines, words and bytes in a file ab.doc

1 5 22 xy.doc # Number of lines, words and bytes in a file xy.doc

3 18 89 total # Total number of lines, words and bytes in a files ab.doc and xy.doc. $

Page 39: Shell Scripting

39

3.17 cmp : compare two files byte by byte Compare two files byte by byte, and the location of the first mismatch is displayed on the screen. $cat cmp1

hello how are you

$cat cmp2

helLo how are You?

$cmp cmp1 cmp2

cmp1 cmp2 differ: char 4, line 1

If two files are identical, cmp displays no messages, but simply returns the prompt back. $cat cmp1

hello

$cat cmp2

hello

$cmp cmp1 cmp2

$ 3.21 UNIX file Types The different type’s files available in UNIX are: Regular files. Example: All .exe files, C, C++, PDF Document files. Directory files. Example: Folders in WINDOWS. Device files. Example: Floppy, CDROM, Printer. FIFO files. Example: PIPEs. Link Files. Example: Shortcuts in WINDOWS. 3.21.1 Regular files It is a text or binary file. For Example: All exe files, text, Document files and program files.

We can create such type of files by using the commands vi, vim, emacs or by using cat. For example using cat: $ cat > newf

Hello, this is the file created by using the command cat.

Page 40: Shell Scripting

40

^d

$

Above command create a new file with file name newf with the contents are

Hello, this is the file created by using the command cat. We can delete the file(s) by using the command rm.: Syntax: $ rm file(s) For example: $rm newf

$

Above command deletes the file newf permanently from the disk. 3.21.2 Directory file A directory is a file that contains information to associate other files with names; these associations are called links or directory entries. Sometimes, people speak of "files in a directory", but in reality, a directory only contains pointers to files, not the files themselves. Hence is like a folder in WINDOWS that contains other files, including other subdirectories. mkdir is the command used for creating the directory Syntax : $mkdir directory_name(s) For example: $mkdir newdir

Above command create a new directory called newdir under present working directory. To remove the directory that directory should be empty. 3.21.3 Device files Actually these are physical devices such as printers, tapes, floppy devices CDROMs, hard disks and terminals.

There are two types of device files: Block and Character device files.

1. Block Device files: A physical device that transmits block of data at a time. For example: floppy devices CDROMs, hard disks.

2. Character Device files: A physical device that transmits data in a character based manner. For example: Line printers, modems etc. mknod is the command used for creating the device files.

Page 41: Shell Scripting

41

Syntax: # mknod Device_Filename DeviceFile_Type Major_ Number Minor_Number Example1: # mknod /dev/cdev c 120 20

The above command creates character device file called cdev with major number 120 and minor number 20. Example2: #mknod /dev/bdev b 225 15 The above command creates block device file called bdev with major number 225 and minor number 15

A major device number is an index to a kernel table that contains the addresses of all device driver functions known to the system. A minor device number is an integer value to be passed as an argument to a device driver function when it is called. The minor device number tells the device driver function what actual physical device it is taking to and the input output buffering scheme to be used for data transfer. 3.21.5 Link files Links are nothing but file names. UNIX allows a file to have more than one name and yet maintain single copy on the disk. Changes made to one link are visible in all other links. Hence links just like reference variables in C++. Types of Links There are two types of links:

1 Hard links 2 Soft/symbolic links

3.21.5.1 Hard Links

It is a UNIX path or file name, by default files are having only one hard link and directories are having two. ln is the command used for creating hard links. Syntax: $ln existing_file_name link_file_name For example: $ln abc abcl

After this file user may refer to the same file by either abc or abcl. $ls –li abc abcl

Page 42: Shell Scripting

42

167 -rw-r--r-- 2 chandu chandu 50 DEC 10 12:25 abc

167 -rw-r--r-- 2 chandu chandu 50 DEC 10 12:25 abcl

For each ln on the same file the hard link count is incremented by 1, and inode

numbers are same for all the links. Links are ordinary files hence we can remove links by using rm command, each time hard link count is decremented by 1. For Example after remove the file abc the hard link count is decremented by 1 as shown below. $rm abc

$ls –li abcl

167 -rw-r--r-- 1 chandu chandu 50 DEC 10 12:25 abcl

3.21.5.2 Symbolic Links Symbolic links are called soft links. Soft link are created in the same manner as hard links, but it requires –s option to the ln command. Symbolic links are just like shortcuts in WINDOWS. Syntax: $ln -s existing_file_name link_file_name For example: $ln –s xyz xyzl

After this file user may refer to the same file by either xyz or xyzl. But the hard link count is remains same and inode numbers, sizes are different. The size of the link file is number of characters in the original file name or path name. $ ls –li abc abcl 267 -rw-r--r-- 1 chandu chandu 50 DEC 10 1:25 abc 260 lrwxrwxrwx 1 chandu chandu 3 DEC 10 1:25 abcl->abc Limitations of Hard links

• Users can’t create hard links for directories unless they have super user privileges.

• Users can’t create hard links on a file system that references files on a different system.

3.21.5.3 Differences between Hard links and Symbolic Links Hard Link Soft Links

Note: After remove the file abc in the above example the association is broken between abc and abcl. After wards suppose if you create the file with name abc, the abc will get new inode number hence there is no link at all even after creating the file with the original name. Hence both the files are entirely different in case of hard links. But in case of soft links if you delete the file xyz, the link file xyzl still references to the non existing file xyz. After creating the new file with the same i.e. xyz the link file references to this new version of the file.

Page 43: Shell Scripting

43

1.Do not create new inode. 1. Create a new inode. 2.Can’t link directories unless super user privileges.

2. Can link directories.

3.Cannot link file across file systems. 3. Can link files across file systems. 4.Increase the hard link count. 4. Does not change the hard link count. 5.Always refer to the old file only, means hard links can be broken by removal of one or more links.

5. Always reference to the latest version of the files to which they link.

3.23 find : Searching for files

UNIX provides the ability to locate files contained within the file system hierarchy by the use of the find command. The find command is a very flexible and powerful command which allows not only the ability to find files, but also to process the resulting files in many different ways. The subtle behavior and sophisticated syntax of find sometimes proves formidable as with most powerful UNIX commands. $find search_path selection_criteria action

Find, recursively examines all files in the directory specified in search_path Then it matches each file for one or more selection_criteria

-inum : Locating a file by inode number $find / -inum 12345 -print -type : Locating a file by file type $find / -type d -print # Locate All directories from the system $find / -type l -print # Locate All symbolic links from the system -perm : Locating a file by permissions $find / -perm 766 -print # Selects all files on the system are having the permissions 766 Finally, it takes some action on those selected files -print : Prints selected file on standard output -ls : Executes ls -lids command on selected files -exec cmd : Executes UNIX command cmd followed by {} \; $find . -name aaa.txt Find all the files named aaa.txt in the current directory or any subdirectory tree. $find / -name vimrc Find all the files named 'vimrc' anywhere on the system.

Page 44: Shell Scripting

44

$find /usr/local/games -name "*xpilot*" Find all files whose names contain the string 'xpilot' which exist within the '/usr/local/games' directory tree. Chapter 4. The File Attributes 4.1 File Attributes

An almost all the attributes of file are listed by using ls command with –l option. As shown below: When the user chandu invoke the command from his home directory the ls command displays the following output. From the above output: total 45

Indicates total number of bytes occupied by the current directory in terms of Block, each block size in UNIX is 512 bytes and 1024 bytes in Linux. Inodes in UNIX Every file system contains a block called inode block, this block contains a table called inode table. All above said attributes of a file and directory are stored in this table as a record (is an individual entry or row in an inode table) except the name of a file or directory. The inode is accessed by a number called an i-number (inode number), it is an unique number in a file system. The mapping of file name and inode numbers is done via directory files. A directory file contains a list of file names and their respective inode numbers for all files stored in that directory. For example: The contents of the directory cse are as follows.

Inode Number File name 120 . 140 .. 200 xyz.c

$ls –li * Total 45 Inode Permissions Owner Name File Size In bytes File Name

125 – rw-r-xr-- 1 chandu chandu 25 DEC 25 10:40 ab.c 150 d rw-r-xr-- 2 chandu chandu 40 DEC 20 11:20 cse

File Types Links Group Name Last Modification Date & Time

Page 45: Shell Scripting

45

File type and permission: First character in the second column indicates file type

- : Indicates Ordinary / hard link file b : Block device file c : Character device file d : Directory l : Symbolic Link file p : Fifo file ("named pipe") special file

Permission The permissions assigned to the file for the owner, the group and all others. Next

series of nine characters r, w, x and - indicates file permissions: for Owner, group and others. First three characters for owner of file, next three characters for group and last three characters for others. r = read, w = write, x = execute -=no permission. Hard link count

Number of alias names attached to the file name, by default files are having only one hard link and for directories 2. Owner : The owner of the file, one who created file. Group : Associated group for the file Size : The size of the file in bytes (i.e. characters) File name : The name associated with the file Modification date: The date the file was last modified, i.e. a "time-stamp". If the file has not been modified within the last year (or six months for Linux), the year of last modification is displayed. Attribute Meaning 1. Inode Number The System inode number of the file. 2. File Type Type of File. 3. Permissions The file access permission for Owner Group and Others. 4. Hard Links Number of hard links of a file. 5. Owner Name The file Owner or User Name. 6. Group Name The group to which User or Owner Belongs to. 7. File Size The file size in bytes. 8. Last Access Time The time, the file was last accessed. 9.Last Modification Time The time, the file was last modified. 10.Last Change Time The time, the file Access Permission, Owner, Group or

Hard link count was last changed. 11. File System Id The file system id where the file is stored. In addition to the above attributes, the UNIX system also stores the Major and Minor numbers for each Device file.

Page 46: Shell Scripting

46

All the above attributes are assigned to the file by the kernel when it is created. Some of these attributes will stay unchanged for the entire life of the file, whereas others may change as the file is being used. The attributes that are constant for any file are:

• File type

• File Inode number

• File System Id

• Major and Minor numbers of device files

Other attributes are changed are changed by the following UNIX commands or by using system calls.

UNIX Command

Attributes Changed

chmod Changes access permission, last change time. chown Changes UID, Last Change time. chgrp Changes GID, Last change time.

ln Increase hard link count. touch Changes last access time and modification time.

vi, emacs Changes file size, last access time and last modification time. rm Decrease the hard link count by one. If the hard link count is

zero the file will be permanently removed from the disk. 4.2 chmod : Changing File Permissions Changing files permission can only be done by the owner of the file (or the root user). This is accomplished using the chmod command to change file protection modes using relative permission and absolute permissions. 4.2.1 Relative Permissions

When changing permissions in this manner, chmod only changes the permissions specified in the command line and leaves the other permissions unchanged. S

Syntax of using this command line is:

$chmod category operation permission filename(s)

Category Operation Permissions u User(owner) + Assigns Permissions r Read Permission g Group - Removes Permissions w Write Permission o Others = Assigns Absolute Permissions x Execute Permission a – All (ugo)

Page 47: Shell Scripting

47

For example: before applying the command chmod on a file f1 permissions are: $ls -l f1 -rw-r--r-- 1 kishan kishan 49 Jul 7 21:07 f1

Owner can have read and write, group can read and others also having only read

permission.

$chmod a+x f1 # Sets executable permission for all other user, new permissions are $ls -l f1 -rwxr-xr-x 1 kishan kishan 49 Jul 7 21:07 f1

$chmod g+w f1 #sets only write permission for group $ls -l f1

-rwxrwxr-x 1 kishan kishan 49 Jul 7 21:07 f1

$chmod ugo-x f1 # removes executable permission for all the users, it is same as $chmod a-x f1 $ ls -l f1 -rw-rw-r-- 1 kishan kishan 49 Jul 7 21:07 f1

4.2.2 Absolute Permission

In this mode chmod command uses octal numbers (0-7) to set new permissions to the file.

Each octal Digit represents permissions of each category.

7 (111) Read, Write and Execute Permission 6 (110) Read and Write Permission 5 (101) Read and execute Permission 4 (100) Read permission 3 (011) Write and Execute permission 2 (010) Write Permission 1 (001) Execute Permission

Syntax of using the chmod command using octal number is: $chmod OctalString filename(s) $ ls -l f1 -rw-rw-r-- 1 kishan kishan 49 Jul 7 21:07 f1

$chmod 777 f1 # Sets all permissions to all categories of users, Existing # Permissions are over written by these new permissions.

Page 48: Shell Scripting

48

$ ls -l f1 -rwxrwxrwx 1 kishan kishan 49 Jul 7 21:07 f1

If the owner of a file wanted to set read and execute for the user permissions, read

only for group permissions and no privileges for all others, the command to set this would be $chmod 540 f1 $ ls -l f1 -r-xr----- 1 kishan kishan 49 Jul 7 21:07 f1

The permissions of read, write and execute take on a slightly different meaning

with respect to directory files. Thus if a file is a directory:

• Read permission determines if a user can view the files contained in a directory, i.e. list the files in it

• Write permission determines if a user can create new files or delete files in the directory. This allows a user with write permissions to a directory to have the ability to delete files in the directory even if they don't have write permissions for the file.

• Execute permission determines if the user can move (i.e. cd) into the directory

4.3 chown : Changing File Owner ship

The super user only able to change the ownership of a file. Syntax: #chown New-Owner-Name file(s)

For example user kishan wants to change his ownership of his file to user chandu,

he has to invoke the following command.

$su

passwd: # Enter Password of Super user #chown chandu f1

#exit

$ls -l f1 -r-xr----- 1 chandu kishan 49 Jul 7 21:07 f1

4.4 chgrp : Changing File group

The Owner and super user can change the group a file. Syntax: $chgrp New-Group-Name file(s)

Page 49: Shell Scripting

49

For example user kishan wants to change his group of his file to group chandu, he

has to invoke the following command. $ls -l f2

-rw-r--r-- 1 kishan kishan 49 Jul 7 21:07 f2

$chgrp chandu f2

$ls -l f2

-rw-r--r-- 1 kishan chandu 49 Jul 7 21:07 f2

chmod and chgrp uses -R option to change the owner ship and group owner ship

recursively. Chapter 5. The SHELL 5.1 Introduction

So what is this shell thing, you ask? Simply, the UNIX shell is a program that interprets and processes the commands the user types at the keyboard, i.e. the command line interface (CLI). When you type a command and press the Enter key, the shell "interprets" the command, translating and substituting as necessary, then locating, loading and starting the associated program (assuming it is found and displaying errors if it is not). The shell is not the only user interface since several Graphical UI programs are available. However, the shell is indispensable when interacting with the command line driven programs of UNIX.

As is common with UNIX, there are a variety of shells from which one can choose, each one with its own distinctive features. The shell you interact with will generally be selected by the system administrator when your account is created, but can be changed by the user at a later time. Each shell program will also have its own distinctive command line prompt, but this too can be changed by the user.

Some of the more popular (in a historic sense) include:

Shell Name Program

Name Description

Bourne Shell sh The original UNIX shell created by Steven Bourne, available on most UNIX systems

C-Shell csh A shell created by Bill Joy at Berkely, designed to provide some C language-like features

TC-Shell tcsh A public domain available extension of csh which allows for command line editing and filename completion

Korn Shell ksh A shell created by David Korn from Bell Labs which provides features of csh and tcsh with the programming capability similar to sh

Page 50: Shell Scripting

50

Bourne Again Shell

bash A shell available from GNU which combines all the best features from sh, csh and the ksh

It is important to keep in mind that the shell program, once started (now a

process), remains alive for the length of your login session (unless somehow manually terminated). All commands entered at the command line are input arguments to the shell process. 5.2 How commands are executed / shell’s interpretive cycle

The normal sequence of steps is as follows:

1. The shell issues the prompt and waits for you to enter a command. 2. After a command is entered, the shell scans the command line for any Meta-

characters and expands abbreviations (like the *, ?, Example : rm *) to create a simplified command line.

3. Then passes on the command line to the kernel for execution. 4. The shell waits for the command to complete. 5. After command execution is complete, the prompt reappears; hence you are free

to enter another command. To execute a command, enter a command line when a prompt (such as a $sign for ordinary user or # for super user) appears on your screen. The shell considers yours command as input, searches through one or more directories to retrieve the program you specified, and conveys your request, along with the program requested, to the Kernel. The kernel then follows the instructions in the program and executes the command you requested. After the program has finished running; the shell signals that at is ready for your next command by printing another prompt. 5.3 Meta Characters

Characters that have special meanings to the Shell are known as meta-characters. Users should avoid using these characters in filenames as results might be unpredictable. We have already seen several meta-characters, for example, vertical bar (|), or the greater than (>) or less than (<). The function of meta-characters can be different depending on whether the Shell or a UNIX tool interprets the character. The following is a list of UNIX special characters.

& Executes command in Background = Assignment operator ; Command separator \ Continuation of command line \m Literal translation of meta-character m

Page 51: Shell Scripting

51

' Turn off special meaning " Group characters into a single argument # Comment follows * Wildcard filename substitution ? Wildcard filename substitution single character $* Argument substitution follows: all the arguments on the command line.

$# Argument count $$ Process id $? Exit status . Current directory [ ] Selective filename substitution

Wildcards: A special type of meta-characters These are behaves like joker in card games, that can match any card. The following table shows how UNIX expands wildcard definitions: UNIX Meaning * Matches z ero or any number of characters ? Matches single character *.* All files that contain a period in the filename *.com All files in the current directory that end in .com ?.com All files in the current directory that end in .com and have one character preceding the period [xyz] A single character, either x,y or z [!xyz] A single character, that is not x,y or z name[xyz] All files in the current directory, namex, namey, or namez name[a-z] All files in the current directory namea through namez name[a-z4] All files in the current directory, namea through namez and name4 $ls ab* list all files begins with ab, including ab $ls ab? list all files having three character in a file name but begins with ab, but not ab. $ls *.c lists all files with extension .c $mv * /home/yogish moves all files in current directory to yogish directory. $rm *.[!l][!o][!g] Removes all files with three character extensions except the ones with the .log extension

There are no absolute rules concerning the use of wildcards. The output produced by the wildcard expansion process is command dependent. For instance, the ls * command will list files in the current directory and the files in the directory in the next lower level as well. The wc * (word count) command will produce output for the files in the current directory only. Thus as you can see commands vary in how wildcards are expanded.

Page 52: Shell Scripting

52

5.4 Escaping and Quoting These are used to remove or turn off the meaning of wild characters.

• Escaping: Providing a \ (backslash) before the wild-card to remove (escape) its special meaning.

• Quoting: Enclosing the wild card, or even the entire pattern, within quotes (like ‘ab*’).

Example: $rm ab\* #Removes a file ab* not ab followed by any characters even ab also. $cat > ab\[a-z\] #creates a file with the name ab[a-z] $cat ab\[a-z\] # Displays the contents of a file ab[a-z] $rm ab\ xy #removes a file “ab xy”, a single file but containing space # hence escape space $echo –e “hello \\n” #Escaping \ $echo ‘\’ #displays a \, use single or double quotes $rm ‘ab*’ #removes file ab* $rm “ab xy” #removes file ab xy 5.5 Input Output Redirection and Pipes

In typical UNIX installations, commands are entered at the keyboard and output resulting from these commands is displayed on the computer screen. Thus, input (by default) comes from the terminal and the resulting output (stream) is displayed on (or directed to) the monitor. Commands typically get their input from a source referred to as standard input (stdin) and typically display their output to a destination referred to as standard output (stdout) as pictured below:

As depicted in the diagram above, input flows (by default) as a stream of bytes

from standard input along a channel, is then manipulated (or generated) by the command, and command output is then directed to the standard output. The ls command can then be described as follows; there is really no input (other than the command itself) and the ls command produces output which flows to the destination of stdout (the terminal screen), as below:

The notations of standard input and standard output are actually implemented in

Page 53: Shell Scripting

53

UNIX as files (as are most things) and referenced by integer file descriptors (or channel descriptors). The file descriptor for standard input is 0 (zero) and the file descriptor for standard output is 1. These are not seen in ordinary use since these are the default values. Input/Output Redirection

UNIX provides the capability to change where standard input comes from, or where output goes using a concept called Input/Output (I/O) redirection. I/O redirection is accomplished using a redirection operator which allows the user to specify the input or output data be redirected to (or from) a file. Note that redirection always results in the data stream going to or coming from a file. 5.5.1 Input redirection The ability also exists to redirect the standard input using the input redirection operator, the < (less than) symbol. Note the point of the operator implies the direction. The general syntax of input redirection looks as follows: $ command < input_file_spec Standard Input – Keyboard(File handle 0) There will be a three input sources:

1. Keyboard, the default sources 2. A file using redirection with the < symbol or with out redirection 3. Another program using a pipe

$wc

hello this input from keyboard

^d

1 5 31

$

wc command is invoked with out file name, hence it reads data from keyboard(standard input stream). $cat file

hello this is testing

$wc < file #The file name is missing in the output, Which means that wc dint open 1 4 22 #file ,It reads the data from the standard input file as a stream. $wc 0<file # 0 is a file handle of input device 1 4 22

$wc file # takes input from file hence displays file name 1 4 22 file

$wc – file # reads data first from standard (- )input device then from file hello how are you

Page 54: Shell Scripting

54

^d

1 4 18 -

1 4 22 file

2 8 40 total

$wc file1 - file2# reads data first from file1 then from key board and from file2 2 2 10 file1

hello

^d

1 1 6 -

1 5 19 file2

4 8 35 total

5.5.2 Output redirection

The simplest case to demonstrate this is basic output redirection. The output

redirection operator is the > (greater than) symbol, and the general syntax looks as follows: $command > output_file_spec

Spaces around the redirection operator are not mandatory, but do add readability to the command. Thus in our ls example from above, we can observe the following use of output redirection: $ ls > my_files $

Notice there is no output appearing after the command, only the return of the prompt. Why is this, you ask? This is because all output from this command was redirected to the file my_files. Observe in the following diagram, no data goes to the terminal screen, but to the file instead.

Examining the file as follows results in the contents of the my_files being displayed: $ cat my_files foo

bar

fred

barney

dino

$

Page 55: Shell Scripting

55

In this example, if the file my_files does not exist, the redirection operator causes its creation, and if it does exist, the contents are overwritten. Consider the example below: $ echo "Hello World!" > my_files $ cat my_files Hello World!

Notice here that the previous contents of the my_files file are gone, and replaced

with the string "Hello World!" This might not be the most desirable behavior, so the shell provides us with the capability to append output to files. The append operator is the >>.

Thus we can do the following: $ ls > my_files

$ echo "Hello World!" >> my_files

$ cat my_files

foo

bar

fred

barney

dino

Hello World!

The first output redirection creates the file if it does not exist, or overwrites its contents if it does, and the second redirection appends the string "Hello World!" to the end of the file. When using the append redirection operator, if the file does not exist, >> will cause its creation and append the output (to the empty file).

Looking in more detail at this, we will use the wc (word count) command. The wc command counts the number of bytes, word and lines in a file. Thus if we do the following using the file created above, we see: $ wc my_files 6 7 39 my_files

Where the output indicates 6 lines, 7 words and 39 bytes, followed by the name of

the file wc opened.

Someone will certainly ask if input redirection and output redirection can be combined, and the answer is most definitely yes. They can be combined as follows: $ wc < my_files > wc_output $

There is no output sent to the terminal screen since all output was sent to the file wc_output. If we then looked at the contents of wc_output, it would contain the same data as above.

Page 56: Shell Scripting

56

To this point, we have discussed the standard input stream (descriptor 0) and the standard output stream (descriptor 1). There is another output stream called standard error (stderr) which has file descriptor 2. Typically when programs return errors, they return these using the standard error channel. Both stdout and stderr direct output to the terminal by default, so distinguishing between the two may be difficult. However each of these output channels can be redirected independently. Refer to the diagram below:

5.5.3 Standard Error Redirection

The standard error redirection operator is similar to the stdout redirection operator and is the 2> (two followed by the greater than, with no spaces) symbol, and the general syntax looks as follows: $command 2> output_file_spec Thus to show an example, we observe the following: $ ls foo bar 2> error_file foo

$ cat error_file ls: bar: No such file or directory

Note here that only the standard output appears once the standard error stream is

redirected into the file named error_file, and when we display the contents of error_file, it contains what was previously displayed on the termimal. To show another example: $ ls foo bar > foo_file 2> error_file $ $ cat foo_file foo

$ cat error_file ls: bar: No such file or directory

In this case both stdout and stderr were redirected to file, thus no output was sent

to the terminal. The contents of each output file was what was previously displayed on the screen.

Page 57: Shell Scripting

57

Note there are numerous ways to combine input, output and error redirection.

Another relevant topic that merits discussion here is the special file named

/dev/null (sometimes referred to as the "bit bucket"). This virtual device discards all data written to it, and returns an End of File (EOF) to any process that reads from it. I informally describe this file as a "garbage can/recycle bin" like thing, except there's no bottom to it. This implies that it can never fill up, and nothing sent to it can ever be retrieved. This file is used in place of an output redirection file specification, when the redirected stream is not desired. For example, if you never care about viewing the standard output, only the standard error channel, you can do the following: $ ls foo bar > /dev/null ls: bar: No such file or directory

In this case, successful command output will be discarded. The /dev/null file is

typically used as an empty destination in such cases where there is a large volume of extraneous output, or cases where errors are handled internally so error messages are not warranted.

One final miscellaneous item is the technique of combining the two output streams into a single file. This is typically done with the 2>&1 command, as follows: $ command > /dev/null 2>&1 [Enter] $

Here the leftmost redirection operator (>) sends stdout to /dev/null and the 2>&1 indicates that channel 2 should be redirected to the same location as channel 1, thus no output is returned to the terminal. Redirection Summary

Redirection Operator Resulting Operation

command > file stdout written to file, overwriting if file exists

command >> file stdout written to file, appending if file exists

command < file input read from file

command 2> file stderr written to file, overwriting if file exists

command 2>> file stderr written to file, appending if file exists

command > file 2>&1 stdout written to file, stderr written to same file descriptor 5.5.4 Pipe Operator

A concept closely related to I/O redirection is the concept of piping and the pipe operator. The pipe operator is the | character (typically located above the enter key). This operator serves to join the standard output stream from one process to the standard input stream of another process in the following manner:

Page 58: Shell Scripting

58

We can look at an example of pipes using the who and the wc commands. Recall that the who command will list each user logged into a machine, one per line as follows: $ who

chandu pts/2 Oct 1 13:07

kishan pts/12 Oct 1 12:07

karun pts/4 Oct 1 13:37

bindu pts/6 Oct 1 13:03

Also recall that the wc command counts characters, words and lines. Thus if we

connect the standard output from the who command to the standard input of the wc (using the -l (ell) option), we can count the number of users on the system: $ who | wc -l

4

In the first part of this example, each of the four lines from the who command will be "piped" into the wc command, where the -l (ell) option will enable the wc command to count the number of lines.

While this example only uses two commands connected through a single pipe operator, many commands can be connected via multiple pipe operators. 5.6 Here Document (<< Operator) It allows a shell script to get its input from the same file that holds the script. Data used this way is said to be in a here document. To create a ‘here document’, we need to use the operator <<. Any command using standard input can also have the input from a here document. The here document symbol (<<) followed by the data, and a delimiter (the terminating string). For example: $wc <<EOF >this is for testing here document, the terminator will be

EOF

We will now get output of the command as number of lines, words, characters till first occurrence of the EOF symbol in the script.

Page 59: Shell Scripting

59

Note: We can use any string as a terminator. Chapter 6. The Filters 6.1 Introduction A set of commands used to filter out wanted data from the mass of data. By default majority of filter commands reads the data from file and display on the screen. 6.2 Sample Database : emp.dat

A text file containing records of five fields each. An empid, ename, edept , esal and ejdate. And fields are separated by |.

EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004 1345|sahana|ise|7000|21/02/1994 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 2187|bhavana|cse|5600|7/04/1991 1567|sadhu|ece|5607|10/04/2006 2287|niveditha|cse|5600|7/10/1997 1567|abhi|mech|5777|8/02/1995 1667|chethan|mech|8677|21/7/2000 1767|vikky|ece|8777|7/8/1991 1887|raghu|mech|5777|20/4/1994 3167|yogish|cse|45000|14/6/1976 4187|soumya|cse|5600|7/04/1985 6.3 pr : paginating files

This command prepares a file for printing by adding suitable headers, footers and formatted text. $pr emp.dat July 19 9:45 2007 emp.dat page1

EID |NAME |EDEPT|ESAL|EJDATE

1223|karun|cse|8000|05/06/2007

2445|chandu|mech|9000|31/03/2004

1345|sahana|ise|7000|21/02/1994

Page 60: Shell Scripting

60

1645|sindu|ece|7900|20/05/2003

2167|deepu|ece|2300|11/8/1983

1167|bindu|ise|2350|17/03/2001

2187|bhavana|cse|5600|7/04/1991

…………………….

…………………….

pr adds five lines of margin at the top and five at the bottom. The header shows

the date and time last modification of the file along with the filename and page number. pr options: -d Double space input -n Numbers lines -o N Offset lines by N spaces, means increases left margin +page Starts Prints from a page -l N Page length is set to N lines $pr +5 filename # Starts printing from page 5 $pr -l 50 filename # Page length is set to 50 lines 6.4 lp : printing a file lp submits files for printing, the following example prints a single copy of a named file filea. $lp filea request id is prl-425 (1-file)

$ Prompt is returned immediately after the job is submitted. The file is not actually printed at the time the command is invoked, but later, depending on the number of jobs already lined up in the queue. Here prl is printer name and the 425 is a job number, Which can later be accessed with other commands. lp options: The command lp accept the above said request because a default printer has been defined by the administrator. -d Name: Prints by using named printer

$lp -d laser filea # Prints using laser printer -t title : Prints title on the first page. $lp -t “First Page” filea

Page 61: Shell Scripting

61

-n N : Prints N copies $lp -n3 filea #prints 3 copies of file filea cancel : used to cancel jobs submitted by lp command You can use cancel command with printer name or with request-id. $cancel prl-425 #Cancels job with request id prl-425 $cancel laser # Cancels current job on the laser printer 6.5 head : Displays the files contents from the beginning $head emp.dat #Displays first ten lines of a files

EID |NAME |EDEPT|ESAL|EJDATE

1223|karun|cse|8000|05/06/2007

2445|chandu|mech|9000|31/03/2004

1345|sahana|ise|7000|21/02/1994

1645|sindu|ece|7900|20/05/2003

2167|deepu|ece|2300|11/8/1983

1167|bindu|ise|2350|17/03/2001

2187|bhavana|cse|5600|7/04/1991

1567|sadhu|ece|5607|10/04/2006

2287|niveditha|cse|5600|7/10/1997

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1767|vikky|ece|8777|7/8/1991

1887|raghu|mech|5777|20/4/1994

3167|yogish|cse|45000|14/6/1976

4187|soumya|cse|5600|7/04/1985

$head -3 emp.dat # Displays 3 lines from the beginning

EID |NAME |EDEPT|ESAL|EJDATE

1223|karun|cse|8000|05/06/2007

2445|chandu|mech|9000|31/03/2004

$ head -12 emp.dat # Displays 12 lines from the beginning

EID |NAME |EDEPT|ESAL|EJDATE

1223|karun|cse|8000|05/06/2007

2445|chandu|mech|9000|31/03/2004

1345|sahana|ise|7000|21/02/1994

1645|sindu|ece|7900|20/05/2003

Page 62: Shell Scripting

62

2167|deepu|ece|2300|11/8/1983

1167|bindu|ise|2350|17/03/2001

2187|bhavana|cse|5600|7/04/1991

1567|sadhu|ece|5607|10/04/2006

2287|niveditha|cse|5600|7/10/1997

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

6.7 tail : Displays the files contents from the end of a file $tail emp.dat # Displays last 10 lines of a file by default 1167|bindu|ise|2350|17/03/2001

2187|bhavana|cse|5600|7/04/1991

1567|sadhu|ece|5607|10/04/2006

2287|niveditha|cse|5600|7/10/1997

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1767|vikky|ece|8777|7/8/1991

1887|raghu|mech|5777|20/4/1994

3167|yogish|cse|45000|14/6/1976

4187|soumya|cse|5600|7/04/1985

$tail -5 emp.dat #Displays last 5 lines of a file 1667|chethan|mech|8677|21/7/2000

1767|vikky|ece|8777|7/8/1991

1887|raghu|mech|5777|20/4/1994

3167|yogish|cse|45000|14/6/1976

4187|soumya|cse|5600|7/04/1985

$tail +5 emp.dat #Displays file contents from line 5 to end of file #some times –n +5 1645|sindu|ece|7900|20/05/2003

2167|deepu|ece|2300|11/8/1983

1167|bindu|ise|2350|17/03/2001

2187|bhavana|cse|5600|7/04/1991

1567|sadhu|ece|5607|10/04/2006

2287|niveditha|cse|5600|7/10/1997

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1767|vikky|ece|8777|7/8/1991

1887|raghu|mech|5777|20/4/1994

3167|yogish|cse|45000|14/6/1976

4187|soumya|cse|5600|7/04/1985

6.8 cut : Cutting a file vertically

Page 63: Shell Scripting

63

$cut -c 1-4 emp.dat # Cuts columns 1-4 EID

1223

2445

1345

1645

2167

1167

2187

1567

2287

1567

1667

1767

1887

3167

4187

$cut -c -4 emp.dat # Same as above, cuts column 1 to 4 EID 1223

2445

1345

1645

2167

1167

2187

1567

2287

1567

1667

1767

1887

3167

4187

$cut -c 20- emp.dat #Displays column 20 to end of line AL|EJDATE

|05/06/2007

00|31/03/2004

0|21/02/1994

|20/05/2003

|11/8/1983

|17/03/2001

Page 64: Shell Scripting

64

00|7/04/1991

|10/04/2006

5600|7/10/1997

|8/02/1995

677|21/7/2000

|7/8/1991

7|20/4/1994

00|14/6/1976

0|7/04/1985

-f : cutting fields along with -d (delimiter) $cut -d ”|” -f 2 , 4 emp.dat # Displays names and Date of joining NAME |EJDATE

karun|05/06/2007

chandu|31/03/2004

sahana|21/02/1994

sindu|20/05/2003

deepu|11/8/1983

bindu|17/03/2001

bhavana|7/04/1991

sadhu|10/04/2006

niveditha|7/10/1997

abhi|8/02/1995

chethan|21/7/2000

vikky|7/8/1991

raghu|20/4/1994

yogish|14/6/1976

soumya|7/04/1985

Write a command line to display only names of current Login users $who | cut -d" " -f 1 # Space is delimiting character root

kishan

yogish

root

root

root

6.9 paste : Pasting files Using this command we can paste together cut files. $tail +12 emp.dat | cut -c -4 >cutlist1

$tail +12 emp.dat | cut -d"|" -f2 >cutlist2

Page 65: Shell Scripting

65

$at cutlist1

1667

1767

1887

3167

4187

$cat cutlist2

chethan

vikky

raghu

yogish

soumya

$paste cutlist1 cutlist2 # Paste two cut files with out using delimiting character

1667 chethan # By default fields are separated by blank 1767 vikky

1887 raghu

3167 yogish

4187 soumya

$ paste -d "|" cutlist1 cutlist2 # Paste with delimiting character | 1667|chethan

1767|vikky

1887|raghu

3167|yogish

4187|soumya

6.10 sort : Ordering a file

This is the command used to ordering of data in ascending or descending order. This command also works with fields. By default, sort reorders lines in ASCII collating sequence- White space first, then numerals, uppercase letters and finally lower case letters. $sort emp5.dat

1223|karun|cse|8000|05/06/2007

1345|sahana|ise|7000|21/02/1994

1645|sindu|ece|7900|20/05/2003

2167|deepu|ece|2300|11/8/1983

2445|chandu|mech|9000|31/03/2004

-r : Reverse the sorting order $sort -r emp5.dat

2445|chandu|mech|9000|31/03/2004

Page 66: Shell Scripting

66

2167|deepu|ece|2300|11/8/1983

1645|sindu|ece|7900|20/05/2003

1345|sahana|ise|7000|21/02/1994

1223|karun|cse|8000|05/06/2007

-k : Sorts using Field wise $sort -t"|" -k2 emp5.dat # Sorting on the second field 2445|chandu|mech|9000|31/03/2004

2167|deepu|ece|2300|11/8/1983

1223|karun|cse|8000|05/06/2007

1345|sahana|ise|7000|21/02/1994

1645|sindu|ece|7900|20/05/2003

-u : Used to remove duplicate lines $cut -d"|" -f3 emp.dat|sort -u #Selects only dept names cse

ece

ise

mech

-n : Used to sort numerically $sort -n numeric file $cat numfile #Contents of numfile 12345

111

2258

453

7070

2500

1500

$sort numfile #Output of numfile without using -n option 111

12345

1500

2258

2500

453

7070

$sort -n numfile #Output of numfile using -n option 111

453

1500

2258

2500

Page 67: Shell Scripting

67

7070

12345

6.11 uniq: Locate repeated and non repeated lines

This is the command used to find repeated and non repeated lines from only sorted file.

By default it displays only uniq lines from the sorted file. Options:

-u : Selects only non-repeated lines -d : Selects only repeated lines

-c : Counting frequency of occurrence 6.12 tr : Translating characters

Always this command reads data directly from standard input. It doesn't take file name as argument. Syntax: $tr options expression1 expression2 < filename

Translate expression1 to expression2; the first character in the expression one is replaced with the first character in the expression2, and similarly for the other characters. $ tr "|" "@" <emp5.dat

1223@karun@cse@8000@05/06/2007

1345@sahana@ise@7000@21/02/1994

1645@sindu@ece@7900@20/05/2003

2167@deepu@ece@2300@11/8/1983

2445@chandu@mech@9000@31/03/2004

$tr "|/" "@Y" <emp5.dat # replaces | by @ and / by Y 1223@karun@cse@8000@05Y06Y2007

1345@sahana@ise@7000@21Y02Y1994

1645@sindu@ece@7900@20Y05Y2003

2167@deepu@ece@2300@11Y8Y1983

2445@chandu@mech@9000@31Y03Y2004

Write a command line to convert all lowercase letters into uppercase: $tr '[a-z]' '[A-Z]' <emp5.dat

1223|KARUN|CSE|8000|05/06/2007

1345|SAHANA|ISE|7000|21/02/1994

1645|SINDU|ECE|7900|20/05/2003

2167|DEEPU|ECE|2300|11/8/1983

2445|CHANDU|MECH|9000|31/03/2004

Page 68: Shell Scripting

68

option : -d – Deleting characters $tr -d '|' < emp5.dat # Deletes delimiting character from file 1223karuncse800005/06/2007

1345sahanaise700021/02/1994

1645sinduece790020/05/2003

2167deepuece230011/8/1983

2445chandumech900031/03/2004

option : -s Compressing multiple occurrences of characters by single character $tr -s '0 ' < emp5.dat #Squeezes multiple number of zeros by single zero 1223|karun|cse|80|05/06/207

1345|sahana|ise|70|21/02/1994

1645|sindu|ece|790|20/05/203

2167|deepu|ece|230|11/8/1983

2445|chandu|mech|90|31/03/204

option : -c complimenting $ tr -cd "|" < emp5.dat # Deletes all characters but not | | | | | | | | | | | | | | | | | | | |

Write a command line to display only file permissions, owner and name of a file. $ls -l | tail +2 | tr -s " " | cut -c 2- | cut -d" " -f1,3,8

rw-r--r-- yogish a

rw-r--r-- yogish a1.c

rw-r--r-- root ab.doc

rw-r--r-- yogish a.cpp

rw-r--r-- yogish alarm1.cpp

rw-r--r-- yogish alarm.cpp

rwxr-xr-x yogish a.out

6.13 Regular Expressions

An expression containing alphabets, digits and wide characters. * Zero or more occurrences of the previous characters . A single character [abc] A single character a, b or c [a-d1-3] Matches a single character between a-d or 1-3 [^abc] A single character which is not a, b or c ^pat Matches pattern pat at beginning of line pat$ Matches pattern pat at the end of line ^$ Matches empty lines 6.14 grep :Used to search a pattern

Page 69: Shell Scripting

69

grep, egrep, fgrep - print lines containing a pattern. $grep option(s) PATTERN FILE(s)

grep searches the named input FILEs (or standard input if no files are named, or

the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E. Fgrep is the same as grep -F.

For example: Search a pattern cse from the emp.dat $grep cse emp.dat # Quoting for string is not necessary 1223|karun|cse|8000|05/06/2007

2187|bhavana|cse|5600|7/04/1991

2287|niveditha|cse|5600|7/10/1997

3167|yogish|cse|45000|14/6/1976

4187|soumya|cse|5600|7/04/1985

$grep tce emp.dat #Returns prompt, because no pattern found $

Grep with multiple file names displays the file names along with the output. $grep cse emp.dat emp5.dat

emp.dat:1223|karun|cse|8000|05/06/2007

emp.dat:2187|bhavana|cse|5600|7/04/1991

emp.dat:2287|niveditha|cse|5600|7/10/1997

emp.dat:3167|yogish|cse|45000|14/6/1976

emp.dat:4187|soumya|cse|5600|7/04/1985

emp5.dat:1223|karun|cse|8000|05/06/2007

grep options: -i Ignoring case while matching -v Doesn’t display lines matching expression -n Displays lines containing the pattern along with line number -c Displays number of lines containing the pattern -l Displays list of files in the current directory containing the pattern -e Used to specifies multiple pattern in a single grep -f Takes pattern from file -w Matches a pattern string as a word $grep -v cse emp.dat # Doesn't display lines matching expression 2445|chandu|mech|9000|31/03/2004

Page 70: Shell Scripting

70

1345|sahana|ise|7000|21/02/1994

1645|sindu|ece|7900|20/05/2003

2167|deepu|ece|2300|11/8/1983

1167|bindu|ise|2350|17/03/2001

1567|sadhu|ece|5607|10/04/2006

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1767|vikky|ece|8777|7/8/1991

1887|raghu|mech|5777|20/4/1994

$grep -n cse emp.da # Displays lines containing the pattern along with line number

2:1223|karun|cse|8000|05/06/2007

8:2187|bhavana|cse|5600|7/04/1991

10:2287|niveditha|cse|5600|7/10/1997

15:3167|yogish|cse|45000|14/6/1976

16:4187|soumya|cse|5600|7/04/1985

$grep -c cse emp.dat #Displays number of lines containing the pattern 5

$ grep -l cse *.dat # List all the filenames containing the pattern emp.dat

emp1.dat

emp5.dat

$grep -e mech -e ise emp.dat#Used to specifies multiple pattern in a single grep

2445|chandu|mech|9000|31/03/2004

1345|sahana|ise|7000|21/02/1994

1167|bindu|ise|2350|17/03/2001

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1887|raghu|mech|5777|20/4/1994

$cat >pat # File containing patterns to be search mech

ise

$grep -f pat emp.dat # -f reads pattern from a file pat 2445|chandu|mech|9000|31/03/2004

1345|sahana|ise|7000|21/02/1994

1167|bindu|ise|2350|17/03/2001

1567|abhi|mech|5777|8/02/1995

1667|chethan|mech|8677|21/7/2000

1887|raghu|mech|5777|20/4/1994

6.15 egrep

Page 71: Shell Scripting

71

Searches a pattern like grep and also supports all the options supported by grep and it supports all basic regular expression character set and Extended regular expression character set such as. + One or more occurrences of the previous character ? Zero or One occurrence of character exp1|exp2 Matches exp1 or exp2 (exp1|exp2)exp3 Matches exp1exp3 or exp2exp3 exp1(exp2|exp3) Matches exp1exp2 or exp1exp3 Write regular expressions to match the following pattern 1. Agarwal, agrawal [aA]g[ar][ar]wal 2. Agarwal, agrawal,aggarwal [aA]g*[ar][ar]wal SIGSTOP or SIGTSTP SIG(STOP|TSTP) SIGTTIN or SIGTTOU SIGTT(IN|OU) HARRIS or HARRISON HARRIS(|ON) Displays lines begins with 2 followed by any three digits from the file emp.dat $grep ^2... emp.dat

2445|chandu|mech|9000|31/03/2004

2167|deepu|ece|2300|11/8/1983

2187|bhavana|cse|5600|7/04/1991

2287|niveditha|cse|5600|7/10/1997

7 .Displays lines ends with 91 $grep . .91$ emp.dat

2187|bhavana|cse|5600|7/04/1991

1767|vikky|ece|8777|7/8/1991

8. Displays only the directories in the current directory $ls -l | grep “^d”

6.16 sed : The stream editor It is a multipurpose tool which combines the work of several filters. The syntax of using the command sed as shown below:

Page 72: Shell Scripting

72

$sed options 'address/pattern action' fie(s) The address/pattern and action should be enclosed between single quotes. Address may be one are one or two line numbers and pattern should be enclosed by pair of slashes, ie /pat/. For example: Selects lines from beginning 1. $sed '3q' emp1.dat # Same as head -3, q indicates quits after line 3. EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004 2. $sed -n '1,3p' emp1.dat #Prints line one through three, same as above EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004 Selects lines from any where 3. $sed -n '5,7p' emp1.dat #Prints records 5,6 and 7 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 Selects lines from the end 4. $sed -n '$p' emp1.dat #Prints last line 4187|soumya|cse|5600|7/04/1985 5. $sed -n '14,$p' emp1.dat #Prints lines from 14 till end of file 1887|raghu|mech|5777|20/4/1994 3167|yogish|cse|45000|14/6/1976 4187|soumya|cse|5600|7/04/1985 Selecting multiple groups of lines 6. $sed -n '1,2p > 5,7p > $p' emp1.dat EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983

Page 73: Shell Scripting

73

1167|bindu|ise|2350|17/03/2001 4187|soumya|cse|5600|7/04/1985 7. $sed -n -e '1,2p' -e '5,7p' -e '$p' emp1.dat #Prints above output but with -e EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 4187|soumya|cse|5600|7/04/1985 Negating the action(!) 8. $sed -n '2,13!p' emp1.dat #Does not Prints line, other than line 2 thru 13 EID |NAME |EDEPT|ESAL|EJDATE 1887|raghu|mech|5777|20/4/1994 3167|yogish|cse|45000|14/6/1976 4187|soumya|cse|5600|7/04/1985 9. $sed -n '/cse/p' emp1.dat #Prints employees belongs to the dept cse 1223|karun|cse|8000|05/06/2007 2187|bhavana|cse|5600|7/04/1991 2287|niveditha|cse|5600|7/10/1997 3167|yogish|cse|45000|14/6/1976 4187|soumya|cse|5600|7/04/1985 Prints all the lines between ise and mech 10. $sed -n '/ise/,/mech/p' emp1.dat 1345|sahana|ise|7000|21/02/1994 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 2187|bhavana|cse|5600|7/04/1991 1567|sadhu|ece|5607|10/04/2006 2287|niveditha|cse|5600|7/10/1997 1567|abhi|mech|5777|8/02/1995 11. Writes selected lines to a file $sed -n '/ise/w f1' emp1.dat #Writes employees belongs to dept ise to file f1 $ cat f1 1345|sahana|ise|7000|21/02/1994 1167|bindu|ise|2350|17/03/2001 12. $sed -n '2,3w f2' emp1.dat #Writes records 2 and 3 to file f2 $ cat f2 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004

Page 74: Shell Scripting

74

sed with text editing: i-insert, a-append, c-change, d-delete and s-substitution Inserts text at line 1 13. sed '1i\EMPLOYEE DATA BASE FILE ' emp1.dat EMPLOYEE DATA BASE FILE EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004 1345|sahana|ise|7000|21/02/1994 .............. .............. Appends blank line after each line of the file 14. $sed 'a\ ' emp1.dat EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 2445|chandu|mech|9000|31/03/2004 1345|sahana|ise|7000|21/02/1994 ................... ................... 15. $sed '2,4a\ APPENDING ' emp1.dat #appends APPENDING from line 2 to 4 EID |NAME |EDEPT|ESAL|EJDATE 1223|karun|cse|8000|05/06/2007 APPENDING 2445|chandu|mech|9000|31/03/2004 APPENDING 1345|sahana|ise|7000|21/02/1994 APPENDING 1645|sindu|ece|7900|20/05/2003 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 Delete all the employees belongs to cse and ece 16. $sed '/cse/,/ece/d' emp1.dat EID |NAME |EDEPT|ESAL|EJDATE 2167|deepu|ece|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 1887|raghu|mech|5777|20/4/1994

Page 75: Shell Scripting

75

17. sed '1,13d' emp1.dat #Deletes records 1 through 13 1887|raghu|mech|5777|20/4/1994 3167|yogish|cse|45000|14/6/1976 4187|soumya|cse|5600|7/04/1985 Substitution - syntax : sed 'address s/source string/destination string/g' file(s) it substitutes source string by destination string. 18. $sed '1,6 s/ece/CSE/g' emp1.dat EID |NAME |EDEPT|ESAL|EJDATE #Substitutes ece by CSE in lines 1 1223|karun|cse|8000|05/06/2007 #through 6 2445|chandu|mech|9000|31/03/2004 1345|sahana|ise|7000|21/02/1994 1645|sindu|CSE|7900|20/05/2003 2167|deepu|CSE|2300|11/8/1983 1167|bindu|ise|2350|17/03/2001 2187|bhavana|cse|5600|7/04/1991 19. $ sed ' s/|/@/g' emp1.dat #Substitutes | by @ EID @NAME @EDEPT@ESAL@EJDATE 1223@karun@cse@8000@05/06/2007 2445@chandu@mech@9000@31/03/2004 1345@sahana@ise@7000@21/02/1994 ................ ................ Chapter 7. A PROCESS 7.1 Introduction

A process can be simply defined as an instance of a running program. It should be

understood that a program is an entity that resides on a non-volatile media (such as disk), and a process is an entity that is being executed (with at least some portion, i.e. segment/page) in RAM.

Since we know that UNIX is a multi-user, multi-tasking operating system, we know that multiple processes can be running on a UNIX system at the same time. Not only can multiple processes be run at the same time, but on a typical multi-user UNIX system, hundreds of processes are running at any given time.

Each time a program is run, a process is created (typically) with the same name as the program itself. So, if the telnet application program is run, a process with the name of telnet is created. Most systems have only one telnet program, but if five users run the telnet program, there will be five processes created named telnet. If the processes have the same name, why doesn't the system get the processes confused, you ask? This will be discussed in detail later in this text.

Page 76: Shell Scripting

76

Processes have the concept of a lifetime associated with them. Processes come to

life, or are said to be born as a program is loaded and begins execution. Processes remain alive while the program continues execution (processes, however can have different states of life). Processes ceast to exist and are said to die as the running program terminates (either normally or abnormally).

All processes (except the very first one) have a parent which created them. Similarly, when a process is created, it is created as a child process, with the process responsible for its' creation being its parent. When a process creates a child process, it is said to have spawned a child. Every process on a UNIX system must have a parent (again, except the very first one), since "orphaned" processes are not allowed. Also, all processes on a UNIX system can be linked to the one initial process. As you will see, processes have a similar hierarchical structure to that of the file system.

Each process will have many attributes associated with it. A processes attributes are stored in a structure memory which is called a Process Control Block, or PCB. Each individual process will have a PCB associated with it. Some of the attributes relevant to this discussion include: each process will have a unique numeric identifier associated with it, referred to as its process identification number, or PID. Each process also has a reference to the PID of its parent, i.e. the parent process id, or PPID. Each process will have priorities associated with it, i.e. the priority of order of execution processes have ownership attributes associated with them, both from the user level and from the group level processes will have a state attributed associated with them, typically assigned and updated by the kernel.

In general, processes come to life using two different, but related techniques. In the first technique when a parent creates a child process, the parent creates (spawns) a copy of itself, including its PCB, which will become the child process. The (child) copy is almost identical to the parent, except the child gets its own PID. This process duplication is accomplished using the fork() system call and is referred to as "forking" a new process. The "process space" of the child process is then overlayed with the process image of the newly running program. This is accomplished using the exec() system call and is referred to as "exec-ing" a new process. Two processes now exist with two different PIDs. This sequence is referred to as the fork-exec sequence, as represented in the diagram below.

Page 77: Shell Scripting

77

In the second technique, the parent process is directly overlayed by the "process image" of the newly running program (child). This results in only a single process, since the original parent was overlayed and essentially destroyed. The PID of the new child is the same as the PID of the deceased parent. As above, this process overlaying is accomplished using the exec() system call. Processes will be discussed in greater detail in subsequent sections. 7.2 Process Status

As defined earlier, a process is an instance of a running program. Each time a program is run, a process is created (typically) with the same name as the program itself. Processes have the concept of a lifetime associated with them. Processes come to life, or are said to be born as a program is loaded and begins execution. Processes remain alive while the program continues execution (processes, however can have different states of life). Processes ease to exist and are said to die as the running program terminates (either normally or abnormally).

As was also mentioned, processes have a parent which created them. Similarly, when a process is created, it is created as a child process, with the process responsible for its' creation being its parent. When a process creates a child process, it is said to have spawned a child. Every process on a UNIX system must have a parent (again, except the very first one), since "orphaned" processes are not allowed. Also, all processes on a UNIX system can be linked to the single initial process.

Each process will have many attributes associated with it. A processes attributes are stored in a structure memory which is called a Process Control Block, or PCB. Each individual process will have a PCB associated with it. Some of these attributes include PID, PPID, priority, ownership, state, etc. The diagram below shows the parent-child relationship, as well as the PCB of each process.

Page 78: Shell Scripting

78

To examine the actual status of processes on a UNIX system, the ps command will be used along with several of its options. By simply runnig the ps command without any options, one can observe the following: $ ps

PID TTY TIME CMD

30758 pts/0 00:00:00 bash

30891 pts/0 00:00:00 ps

In this output, two running programs are listed, the bash (parent) process and the

ps (child) process. Along with this is displayed the PID of each as well as the TTY (connected terminal id) and the cumulative execution time. Only two are listed since these two processes are the only two belonging to this user id (uid) and this TTY.

A bit more information can be viewed using the -x option with ps (note that not

all options will work on all UNIX implementations; s $ ps -x

PID TTY STAT TIME CMD

30758 pts/0 S 0:00 -ksh

31043 pts/0 R 0:00 ps -x

In this output, we see all information from above as well as an additional field

labeled STAT which indicates the process status. What do the S and R status values indicate? What happens to the parent when a child process executes. In general, the possible process states are:

D uninterruptible sleep (usually IO)

Page 79: Shell Scripting

79

R runnable (on run queue) S sleeping T traced or stopped Z a defunct ("zombie") process

Also notice that the PID of the ksh processes is the same in both output displays (30758) but the two ps command PIDs differ (30891 & 31043). Can you explain this? All of the processes on a UNIX system can be listed using several options as follows: $ ps -eaf UID PID PPID C STIME TTY TIME CMD root 1 0 0 Oct12 ? 00:00:04 init root 2 1 0 Oct12 ? 00:00:00 [keventd] root 3 1 0 Oct12 ? 00:00:00 [kapm-idled] root 4 0 0 Oct12 ? 00:00:00 [ksoftirqd_CPU0] root 5 0 0 Oct12 ? 00:00:00 [kswapd] root 6 0 0 Oct12 ? 00:00:00 [kreclaimd] root 7 0 0 Oct12 ? 00:00:00 [bdflush] root 8 0 0 Oct12 ? 00:00:00 [kupdated] root 9 1 0 Oct12 ? 00:00:00 [mdrecoveryd] root 13 1 0 Oct12 ? 00:00:01 [kjournald] root 88 1 0 Oct12 ? 00:00:00 [khubd] root 193 1 0 Oct12 ? 00:00:00 [kjournald] root 649 1 0 Oct12 ? 00:00:00 syslogd -m 0 ntp 749 1 0 Oct12 ? 00:00:00 ntpd -U ntp chandu 30758 30757 0 15:21 pts/0 00:00:00 -ksh chandu 30845 30758 0 16:41 pts/0 00:00:00 ps -eaf

Notice that the owner of most of these processes is root. Notice also that both the PID and the PPID are listed in this output. Looking at this output dissected, one should be able to trace the ancestry of the ps command: $ ps -eaf UID PID PPID C STIME TTY TIME CMD root 1 0 0 Oct12 ? 00:00:04 init . . root 805 1 0 Oct12 ? 00:00:01 /usr/sbin/sshd . . root 30757 805 0 15:21 ? 00:00:00 /usr/sbin/sshd chandu 30758 30757 0 15:21 pts/0 00:00:00 -ksh chandu 30845 30758 0 15:29 pts/0 00:00:00 ps -eaf

You can see the process id of the ps process is 30845, and its' parent is PID

Page 80: Shell Scripting

80

30758. Process number 30758 is the ksh process, and its' parent is process 30757. Process 30757 is sshd (a secure shell client) and its' parent is PID 805. Process 805 is sshd (a secure shell server) whose parent is PID 1, which leads us to our venerable grandparent process, init.

There is another way to view the system process hierarchy, and that is using the

command pstree. This command gives you a top-down view of the process tree as follows: $ pstree

init-+-apmd

|-atd

|-crond

|-gpm

|-kapm-idled

|-keventd

|-khubd

|-2*[kjournald]

|-klogd

|-lpd

|-mdrecoveryd

|-6*[mingetty]

|-ntpd---ntpd

|-sendmail

|-sshd---sshd---ksh---pstree

|-syslogd

|-xfs

`-xinetd

This is an alphabetical listing, with all processes being connected to init. Make

sure you can identify the parent-child connection from the pstree command as describe in the ps output above. $ps -f # -f option is used to display full Listing of process status UID PID PPID C STIME TTY TIME CMD

root 6508 6502 0 Jul07 pts/1 00:00:00 bash

root 6808 6508 0 01:17 pts/1 00:00:00 ps -f

# -u followed by user name is used to display processes owned by that user $ps -u yogish PID TTY TIME CMD

6559 tty1 00:00:00 bash

$ps -e # Used to display system processes PID TTY TIME CMD

Page 81: Shell Scripting

81

1 ? 00:00:01 init

2 ? 00:00:00 migration/0

3 ? 00:00:00 ksoftirqd/0

4 ? 00:00:00 migration/1

5 ? 00:00:00 ksoftirqd/1

6 ? 00:00:00 events/0

7 ? 00:00:00 events/1

8 ? 00:00:00 khelper

9 ? 00:00:00 netlink/0

10 ? 00:00:00 netlink/1

11 ? 00:00:00 kacpid

31 ? 00:00:00 kblockd/0

32 ? 00:00:00 kblockd/1

43 ? 00:00:00 pdflush

42 ? 00:00:00 kirqd

44 ? 00:00:00 pdflush

46 ? 00:00:00 aio/0

45 ? 00:00:00 kswapd0

47 ? 00:00:00 aio/1

630 ? 00:00:00 kseriod

1692 ? 00:00:00 ata/0

1693 ? 00:00:00 ata/1

1695 ? 00:00:00 scsi_eh_0

and so on.

7.3 Mechanism of process Creation 7.3.1 fork and exec Processes are created using two UNIX system calls, namely fork and exec.

7.3.1.1 Introduction

A Process is a program under execution, For example a.out in UNIX or POSIX system. For example, a UNIX shell is a process that is created when a user logs on to a system.

Processes are created with the fork system call (so the operation of creating a new process is sometimes called forking a process). The child process created by fork is a copy of the original parent process, except that it has its own process ID and the PPID .

After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a child process to finish executing before continuing, you must do this explicitly after the fork operation, by calling wait or waitpid.

The exec system call creates a child that immediately replaces (overlays – just like a person changing his job, his personal identifications are remains same but nature of job

Page 82: Shell Scripting

82

is different) the parent process, which includes inheriting the parent process id. The standard method for creating a new process is to use fork to make a copy of the existing process, then using exec to overwrite the copy with the new (child) process. This is commonly referred to as the fork-exec call sequence. Further the following properties of parent are inherited to the child:

• Real user ID or UID: The user ID of a user who created the parent process.

• Real group ID or GID : The group ID of a user who created the parent process.

• An effective user ID: This ia normally the same as the real user ID, except when the file that was executed to create a the process has its set-UID flag turned on. In that case, the process eUID will take on the UID of the file. This allows the process to access and create files with same privileges as the program file owner.

• An effective group ID: This is normally the same as the real group ID, except when the file that was executed to create the process has its set-GID flag turned on. In that case, the process eGID will take on the GID of the file. This allows the process to access and create files with same privileges as the group to which program file belongs.

• Saved set UID and saved set GID: These are the assigned eUID and eGID.

• Process group Identification (PGID) and Session identification number (SID):These identifies a process group and session of which process is a member.

• Supplementary GID: These are set of additional group IDs for a user who created a process.

• Current working directory and root directory.

• File mode creation mask.

• Process signal mask.

• Controlling terminal. In addition to the above attributes, the following attributes are different between the

Parent and Child process.

• Process identification number: PID is an integer identification number that is unique per process in an entire operating system.

• Parent Process identification number: The parent process PPID.

• Pending signals: The set of signals that are pending delivery to the parent process. This is reset to none in the child process.

• Alarm clock time: The process alarm clock time is reset to zero in the child process.

• File Locks: The set of file locks owned by the parent is not inherited by the child process.

7.3.1.2 How Shell is created When the system moves to multi-user mode, init forks and execs a getty for every active communication port(line). Each of these gettys prints the login prompt on the respective terminal and then goes off to sleep.

Page 83: Shell Scripting

83

When a user attempts to lo in, getty wakes up and fork-execs the process representing the login shell. Repeated overlaying ultimately results in init becoming the immediate ancestor of the login shell as can be seen from this sequence.

init ---> getty ---> login ---> shell fork fork-exec fork-exec init goes off sleep, waiting for the death of its children. The other process getty and login have extinguished themselves by overlaying. When the user logs out, his shell is killed and the death is intimated to init. Init then wakes up and swans another getty for that line to monitor the next login. 7.3.2 Internal and External Commands

From the point of process, shell recognizes three types of commands:

7.3.2.1 External Commands These are the commands stored in /bin or in /usr/bin, means these are having

location space on the system. For example cat ls etc. the shell creates a process for each of these commands that it executes while remaining their parent.

For example: $type ls

ls is /bin/ls #ls is the command stored in directory /bin 7.3.2.2 Internal Commands

The shell has a number of built-in commands as well. Like cd echo don’t generate

a process, and are executed directly by the shell. Similarly variable assignment with the statement x=58, for instance doesn’t generate a process either.

For example: $type echo

echo is shell builtin

7.3.2.3 Shell scripts

The shell executes this shell scripts by creating another shell, which then executes

the commands listed in script. The child shell becomes the parent of the commands that feature in the script. 7.4 Zombie Process

A zombie process is a process that has terminated, but is still in the operating systems process table waiting for its parent process to retrieve its exit status.

This is created when child terminates before the parent and parent would not able

Page 84: Shell Scripting

84

to fetch terminated status. The ps command prints the state of a zombie process as Z. $ps -u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND yogish 5577 0.0 0.2 2692 1056 tty1 S 09:01 0:00 su - yogish yogish 5578 0.0 0.3 4104 1792 tty1 S 09:01 0:00 -bash yogish 5638 0.0 0.1 2160 708 tty1 S+ 09:08 0:00 ./a.out yogish 5639 0.0 0.0 0 0 tty1 Z+ 09:08 0:00 [a.out] <defunct> yogish 5640 0.0 0.1 2372 708 tty1 R+ 09:08 0:00 ps -u 7.5 Orphaned Process

A process whose parent terminates is called an orphan and for this orphaned process init process become the parent process. 7.6 Running Jobs in Background

A multitasking can be achieved by executing more than one jobs at a time. Since only one job in the foreground and the rest of the jobs in the background.

Running processes, especially those that do not complete quickly, are often referred to as running jobs, or just jobs. For jobs that do not complete quickly (in a second or two), these sometimes need special care so that other work can be done efficiently. UNIX provides the capability to allow processes (jobs) to run in the foreground where they can allow user interaction or the background where they can run uninterrupted. By default, jobs run in the foreground and do not return control of the terminal until completed.

Run the process in background can be achieved by in two ways one using &

operator and by using the command nohup.

Execute process in background using an operator: & In this case parent process won’t wait for child's death. For example

Starts a job consisting of one process in the foreground. The commands: $ls | wc >out& $ who | wc –l > count &

Here shell starts two jobs in the background. When we start a background job, the shell assigns it a job identifier and prints one or more of the process IDs. The following script shows how the Bash Shell handles this.

Page 85: Shell Scripting

85

$ls | wc >out&

[1] 1475

$

$ who | wc –l > count &

[2] 1490

$ just press RETURN or Just press ENTER KEY [2] + Done who | wc –l > count &

[1] + Done ls | wc >out&

The ls | wc >out is job number 1 and process ID is 1475. The next job number is

2 and the Process ID is 1490. When the jobs are done and when we press RETURN the shell tells us that the jobs are complete. The reason we have to press RETURN is to have the shell print its prompt. $

Prompt is returning hence shell is ready to accept another command, even though previous command not yet completed. In this way you can run any number of jobs in the background by issuing a $ at the end of a command line. By default output of background process is appear on the screen. But we have an option to avoid to it.

This is an option that we can allow or disallow. Normally we use the sty command to change this option. For example: $cat temp.c & # Execute in background

[1] 1719 $hello, world # The output from the background job appears after the prompt, # when we press RETURN [1] + Done cat temp.c &

$ stty tostop #Disable ability of background jobs to output to controlling # Terminal try it again in the background $ cat temp.c &

[1] 1721

$ # When we press RETURN and find the job is stopped [1] + Stopped(tty output) cat temp.c &

$ fg %1 #resume stopped job in the foreground

cat temp.c#the shell tells us which job is now in the foreground and here is its hello, world

Page 86: Shell Scripting

86

7.6.1 Using nohup command

When ever shell terminate, the background processes created by using this shell also be terminated or aborted. UNIX provides an option even shell terminate we can allow background process to continue with the help of init process by using nohup command. $nohup prg1.sh&

[1] 5626

nohup: Appending output to nohup.out

In this case always output of background process send to a file nohup.out. You

can now safely logout of the system without aborting the command.

When you use the ps command after using nohup from another terminal it is displays the following output. When ever kishan invokes the following command: $ps -f -u chandu

UID PID PPID C STIME TTY TIME COMMAND

chandu 5626 1 99 10:34 tty05 00:01:03 sh a

Here observe ppid becomes 1, because parent( shell ) died. 7.7 Job Control

Job control allows us to start multiple jobs (group of processes) from a single terminal and control which jobs can access the terminal and which jobs are to run in the background. A job is just a collection of processes, often a pipeline of processes. When ever you hits the key ^z the system is going to display the following output. $sh a >e

[1]+ Stopped sh a >e

$

7.7.1. Terminate a process

If a process is running, and the terminal (i.e. tty) that started its execution is available, the simple key sequence to terminate a process is [Ctrl-c]. This will result in the process being immediately terminated.

If there is not access to the starting tty, and you wish to terminate a process (i.e. remotely), the command to do this is: $kill pid

Page 87: Shell Scripting

87

Where the signal is optional. The signal can be an actual UNIX signal or can be a numeric representation of a signals value. Some of the common named UNIX signals and their corresponding numeric ids are: SIGHUP 1 SIGINT 2 SIGQUIT 3 SIGTRAP 5 SIGKILL 9 SIGTERM 15 Thus, you can terminate a process in the following way: kill pid # Default signal of SIGTERM kill -KILL pid # use of SIGKILL signal kill -9 pid # also KILL signal

Some processes will ignore certain signals, for example, if you try to kill your shell with the default TERM signal, this will be ignored by the shell. Typically when you want to kill a process, you really want to terminate it and the -9 signal is the easiest to remember. 7.7.2. Suspend a process

Suspending a process is done with key sequence [Ctrl-z]. Once this command is entered, the process becomes a suspended (stopped) process, as follows: $ vi foo [Enter]

[Ctrl-z] [1] + Stopped vi foo

$ps -x [Enter] PID TTY STAT TIME COMMAND

2876 pts/0 S 0:00 -bash

2941 pts/0 T 0:00 vi foo

2946 pts/0 R 0:00 ps -x

7.7.3 Move a process/job to the background (bg)

To move a suspended (stopped) process to the background, you use the bg command. This will allow the suspended job to execute in the background. $bg

[1]+ sh a >e & # & at the end of the command line indicates now job is run in the background 7.7.4 Move a background process/job to the foreground

Page 88: Shell Scripting

88

To move a background process to the foreground, you use the fg command. This will continue the execution of the job in the foreground.

$bg # Brings the current background job to foreground Note: The fg and bg commands can also be used with the job number, job name or a string as arguments, prefixed by %symbol: $fg %1 #Brings First job to foreground $fg %prg1.sh #Brings prg1.sh job to foreground $bg %6 #Sends sixth job to background 7.7.5 jobs: List active jobs

To list active jobs, you simply use the jobs command. The jobs command will show all active jobs for the current user. $jobs

[1] + Running du /

[2] - Running loop_program

[3] - Running cc configs.c

Notice the numbers enclosed within the brackets, []. This indicates the order of

the job, that is the nth job in the job queue. To operate on a specific job, specify the job number preceded by the percent character. Thus to bring the 2nd job to the foreground, the command would be: $ fg %2

7.8 at and batch Using these we can execute our commands later. at : Request that a command be run in background mode at a time but only one time you specify on the command line. A sample format is: $at 8:45am Jun 09 command1 <CR> command2<CR> <^d>

If you use the at command without the date, the command executes within twenty-four hours at the time specified. For example: Execute a command date at 12 O’clock $at 12:00

at>echo -e” todays date is “

Page 89: Shell Scripting

89

at>date

at>^d

$

at is executing at 12 O’clock and result is mailed to the user. Or else user can explicitly redirect the output to file. For example : $at 12:00 at>date > out.dat

at>^d

at also offers the key words now, noon, midnight, today and tomorrow. For example:

at 6pm at noon at now + 1 year at 9am tomorrow at 7:5pm June 07, 2008

batch: Submit command(s) to be processed when the system load is at an acceptable level.

A sample format of this command is: batch<CR> command1 <CR> command2<CR> <^d> You can use a shell script for a command in batch. This may be useful and time saving if you have a set of commands you frequently submit using this command. 7.9 Daemons

Daemons are processes that live for long time and they do not have a controlling terminal hence they are always run in the background. They are often started when the system, is bootstrapped and terminate, when the system is shutdown.

Daemon Characteristics

Let's look at some common system daemons and how they are related to the concepts of process groups, controlling terminals, and sessions. The ps print the status of various processes in the system.

$ps –axj

- a : Option shows the status of all the processes owned by others - x : Shows process that does not have a controlling terminal - j : Option displays the job related information:

Page 90: Shell Scripting

90

The output from ps looks like:

PPID PID PGID SID TT TPGID UID COMMAND 0 0 0 0 ? -1 0 Swapper 0 1 0 0 ? -1 0 /sbin/init 0 2 0 0 ? -1 0 pagedaemon 1 105 37 37 ? -1 0 update 1 108 108 108 ? -1 0 cron

Processes 0, 1, and 2 are the process IDs of swapper, init and pagedaemon

respectively and they are existing for the entire lifetime of the system. They have no Parent process group ID, and no session ID. update is a program that flushes the kernel's buffer cache to disk at regular intervals (usually every 30 seconds). To-do this it just calls the sync function every 30 seconds. The cron daemon executes commands at specified dates and times.

Notice that, all the daemons run with super user privileges (user ID of 1). None

of the daemons has a controlling terminal: The terminal name is set to a question mark and the terminal foreground process group is -1.

All the daemons other than update are process group leaders and session leaders and are the only processes in their process group and session. Update is the only process in its process group (37) and session (37), but the process group leader (which was probably also the session leader) has already exited. Finally, the parent of all these daemons is the init process. 7.10 cron : Running jobs Periodically

It is a system daemon process. at and batch commands are executes a job only once but cron executes a job at regular intervals. Every minutes cron process will wakeup and looks in a control file(the crontab file) in /var/spool/cron/crontabs for instructions to be performed at that instant. After execute them, it goes back to sleep, only to wake up the next minute.

The output produce by a cron command is mail to the owner of the crontab (that

is the use of after whom the crontab file is named. The actual owner of the crontab files is generally root). 7.10.1 The crontab file format

Minute Hour Day Month Weekday Command

The First five fields are separated by whitespace, but within the command field white space is taken literally. Minute, houre, day, month and weekday give information about the times at which command should be run. Their interpretations are shown in table.

Page 91: Shell Scripting

91

Field Description range Minute Minute of the hour 0 to 59 Hour Hour of the day 0 to 23 Day Day of the month 1 to 31 Month Month of the year 1 to 12 Week day Day of the week 0 to 6(0 = Sunday)

Each of the time related fields may contain

• A Star, which matches everything

• A Single Integer which matches exactly

• Two integer separated by a dash matching a range of values

• A Comma-separated series of integers or ranges, matching any listed value

For Example: MIN HOUR DOM MOY DOW COMMAND (0-59) (0-23) (1-31) (1-12) (0-6) (note : 0-Sunday) 0 18 * * * date 30 2 * * * who

The Asterisks act as wild cards. In the crontab example, the date is executed every day at 6 P.M. The command who is executed every day at 2:30 AM. 45 10 * * 1-5 date Means 10.45AM, mondya through Friday. 0, 30 * 13 * 5 date

Means “every Half –hour on Friday, and every half-hour on the 13th of the month,” not “every half –hour on Friday on 13th.”

The command is a valid UNIX command and should not be quoted. Command is considering continuing to the end of the line and may contain blanks and Tabs. And here are some complete examples of crontab entries: 30 8 * * 1 date This entry will be activated at 8:30 each Monday morning. 30 8 * * * date

Page 92: Shell Scripting

92

This command will run at 8.30 each morning. 55 14 * * 0-3, 6 date This line runs date command at 2:55 PM every day except Thursday and Friday. 7.10.2 crontab management crontab file name installs file name as your crontab, replacing any previous version. crontab –e : Checks out a copy of your crontab invokes u r editor on it, and then resubmit it to the crontab directory. crontab –l : List the contents of your standard output. crontab -r : Removes it, living you with no corn tab file at all.

By default all user can submit corntab files to cron. The Two configuration files, called cron.allow and cron.deny, allow you to overwrite this policy. If the allow file exists then it contains a list of all users that may submit crontabs, one per line. Any one not listed can not invoke the crontab command.

Chapter 9. Shell Programming 9.1 Introduction A shell program, sometimes referred to as a shell script, is simply a program constructed of shell commands. Shell programs are interpreted each time they are run. This means each command is processed (i.e. executed) by the shell a single line at a time. This is different from languages such as C or C++, which are translated in their entirety by a compiler program into a binary image. A shell program may be simple and consist of just a few shell commands, or it may be very complex and consist of thousands of shell commands. The complexity of the shell program is in the hand of the programmer. In general, a shell program can be characterized by:

• Shell programs consist of one or more primitive shell commands

• Shell programs are created using your text editor of choice, e.g. vi or emacs

• Shell programs are executed just as shell commands are, by typing the name of the program followed by the [Enter] key

• Shell programs have permission modes as do any other file, and must have the correct permissions set to execute the program

• As with other programming languages, the shell language has the functionality to allow input & output, iteration, logical decision making, file creation and deletion, and system call capability

• Shell programs are free format, as long as the syntax of each shell command is correct. This means that blank lines, indentation and abundant white space can be used freely.

Page 93: Shell Scripting

93

As stated, a shell program is merely a file comprised of shell commands. Thus, if we wanted to write the venerable "hello world" program as a shell script, we would do the following:

Use the editor to create the program, for simplicity we'll call first (recall file extensions are not mandatory) $vi first [Enter]

insert the following shell command in the first file: # This is a comment! echo "Hello World!" save and exit the editor program , run the first program. $sh first [Enter]

Hello World!

You have now written your first successful shell program. 9.2 Executing a shell program There are two different ways you can execute a shell programs.

One way is to execute a shell program is to use sh command, type the sh command followed by name of the script at the prompt as shown below. $sh first.sh

Hello World

The first line tells UNIX that the file is to be executed by /bin/sh. This is the

standard location of the Bourne shell on just about every UNIX system. If you're using GNU/Linux, /bin/sh is normally a symbolic link to bash.

The second line begins with a special symbol: #. This marks the line as a comment, and it is ignored completely by the shell.

The only exception is when the very first line of the file starts with #! - as ours does. This is a special directive which UNIX treats specially (this line is not mandatory). It means that even if you are using csh, ksh, or anything else as your interactive shell, that what follows should be interpreted by the Bourne shell hence this line can be called as interpreter line.

Similarly, a Perl script may start with the line #!/usr/bin/perl to tell your interactive shell that the program which follows should be executed by perl. For Bourne shell programming, we shall stick to #!/bin/sh.

The third line runs a command: echo, with two parameters, or arguments - the first is "Hello"; the second is "World".

Page 94: Shell Scripting

94

Note that echo will automatically put a single space between its parameters. The # symbol still marks a comment; the # and anything following it is ignored by the shell.

Second way is to make the script executable using chmod command and then invoke shell program name. $chmod +x first.sh

$./first.sh

Hello World

Creating another simple shell program We'll begin by creating a simple shell program that will do the following tasks, in order: clear the screen print the current directory Display current login users display this message on your terminal. “this is the end of a shell program” create a file called spg1.sh using an editor or else by using cat command and enter the following. spg1.sh clear pwd who echo -e "\n this is the end of shell program\n" Note: It is not mandatory to assign an .sh extension for shell programs. Now spg1.sh is executed by sh command, and clears the screen, the path name of a current directory is printed, then displays all the login users, and finally, the message this is the end of a shell program as shown below. $sh spg1.sh

/home/yogish

yogish tty1 Jun 6 22:35

root :0 Jun 6 21:57 (console)

root pts/0 Jun 6 21:57

root pts/1 Jun 6 22:32 (:0.0)

this is the end of shell program $chmod +x spg1.sh

$./spg1.sh

/home/yogish

Page 95: Shell Scripting

95

yogish tty1 Jun 6 22:35

root :0 Jun 6 21:57 (console)

root pts/0 Jun 6 21:57

root pts/1 Jun 6 22:32 (:0.0)

this is the end of shell program 9.3 Variables The Bourne Shell has no true numeric variables. It uses string variables to represent numbers, as well as text. String variables are able to take on the value of a string of characters. There are three types of variables in the Bourne Shell.

• Named variables

• Special Variables

• Positional parameters

You can declare, initialize, read, and modify user variables from a Bourne Shell script or from the command line. The Bourne Shell itself declares and initializes shell variables, but you can read and modify them. The Bourne Shell also initializes the read-only shell variables, and you can read but not modify them. 9.3.1 Named Variables

As in other programming languages you can't live without variables. In shell programming all variables have the data type string and you do not need to declare them.

To assign a value to a variable you write:

varname=value Note: There are no spaces on either side of the equal sign(=). To get the value back you just put a dollar sign in front of the variable name. 9.3.1.1 Rules for naming variables

The first character of a variable must be a letter or an under score. The rest of a name can consist of letters, under scores, and digits. Like shell program file names, variable name should not be shell command names.

Also the shell reserves some variable names are called built in shell variable or

environment variables with uppercase letters that you should not use for your variables var.sh #!/bin/sh # assign a value: a="hello world"

Page 96: Shell Scripting

96

# now print the content of "a": echo "A is:" echo $a The script will just print: $sh var.sh

A is:

hello world

Sometimes it is possible to confuse variable names with the rest of the text: $num=2

$echo "this is the $numnd"

This will not print "this is the 2nd" but "this is the " because the shell searches for

a variable called numnd which has no value. To tell the shell that we mean the variable num we have to use curly braces: $num=2

$echo "this is the ${num}nd"

This prints what you want: this is the 2nd To get the value back you just put a

dollar sign in front of the variable: This prints what you want: this is the 2nd

A variable stores a string (try running these commands in a Bourne shell)

$name="Reva It"

$echo $name

The quotes are required in the example above because the string contains a special

character (the space) A variable may store a number num=137

The shell stores this as a string even though it appears to be a number It is legal to assign any sequence of non-blank characters as the name of a variable. The sample session below creates a variable called person and initializes it with the string REVA.

It is important to note that you must NOT precede or follow the equal sign with a space or TAB character. name=REVA A variable can be removed with the unset and protected by readonly commands.

Page 97: Shell Scripting

97

unset name # name is now undefined readonly name # name can't be re assigned 9.3.2 Environment Variables HOME : When you login, the Bourne Shell gets that pathname and assigns it to the HOME variable. When you log in, the working directory is set to home directory, called as Login directory. It is created by the system when new user account is opened. for example when user chandu logs into a system , he lands up in a directory /home/chandu. The shell variable HOME holds your home directory. The user can set HOME to any value. The user can set HOME to any value. When chandu executes the command echo $HOME it returns his home directory. $echo $HOME

/home/chandu

LOGNAME : This is the name that the user used to log in. $echo $LOGNAME

chandu

PATH : A path is a sequence of directory names which is used for searching for a file. The variable PATH holds a path used for searching for programs to be run.

The syntax of a path is a sequence of directory names separated by colons. An empty string instead of a directory name stands for the current directory. A typical value for this environment variable might be a string like: /bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin $echo $PATH /home/chandu:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin This means that if the user tries to execute a program named foo, the system will look for files named foo, /bin/foo, /etc/foo, and so on. The first of these files that exists is the one that is executed. TERM : Identifies your terminal type. LC_ALL : If this environment variable is set it overrides the selection for all the locales done using the other LC_* environment variables. The value of the other LC_* environment variables is simply ignored in this case. LC_COLLATE : This specifies what locale to use for string sorting. LC_CTYPE : This specifies what locale to use for character sets and character

Page 98: Shell Scripting

98

classification. LC_MONETAR : This specifies what locale to use for formatting monetary values. LC_NUMERIC : This specifies what locale to use for formatting numbers. LC_TIME : This specifies what locale to use for formatting date/time values. IFS : Defines the internal field separators(Normally the space, the tab and the carriage return).

Hence it containing non printable characters, use od command along with -bc optins to display the contents. $ echo "$IFS" | od -bc

0000000 040 011 012 012

\t \n \n

$

When you assign the IFS variable to another character, you can also use this character as the field separator. Example: $num_args a:b:c:d

This example shows only one argument, namely a:b:c:d

$IFS=:

$num_args a:b:c:d This example now shows four different arguments; each being separated by the new IFS, (:). MAIL : The MAIL variable contains the name of the file that the mail (and mailx) utilities use to store your mail. Usually, the absolute pathname of this file is /usr/mail/name, where name is your login name. For Example: $echo $MAIL

/usr/mail/chandu

MAILCHECK : This variable specifies how often, in seconds, the Bourne Shell will check for new mail. The default is 600 seconds. If set to 0, it will check for new mail each time before it gives you a prompt. PS1 : This is the Bourne Shell prompt which lets you know that the shell is waiting for you to give it a command. The default Bourne Shell prompt is a dollar sign ($). The shell stores the prompt as a string variable in PS1. When you change the value of this variable, the appearance of the prompt will change. For example:

Page 99: Shell Scripting

99

$PS1=”MY PROMPT> ”

MY PROMPT> #Now your prompt is MY PROMPT> instead $ Sign PS2 : This variable is called the secondary prompt. If the command is not completed on one line and must be continued on the next line, the prompt for that continued line is PS2. The default is >. This prompt indicates that the Bourne Shell is expecting you to finish the previous command line. $echo “ demonstration of prompt

>string PS2”

demonstration of prompt

string PS2 $PS2=”Continue?”' #Now secondary prompt was changed to "Continue? ". $echo “demonstration of prompt

Continue? string PS2”

demonstration of prompt

string 2

SHELL : tells you the shell you are using.

$echo $SHELL

/bin/bash

#ser.sh - program to search pattern and display number of records found clear echo -e "\n Enter pattern to be search :\c " read pat echo -e "\n Enter file name :\c " read filename echo -e "\n\n" grep -i $pat $filename echo -e "\n\n Number of records found `grep -i -c $pat $filename`" echo -e "\n\n" $sh ser.sh

Enter pattern to be search :cse

Enter file name :emp.dat

1223|karun|cse|8000

2187|bhavana|cse|5600

2287|niveditha|cse|5600

Number of records found 3

$sh ser.sh

Enter pattern to be search :mech

Page 100: Shell Scripting

100

Enter file name :emp.dat

2445|chandu|mech|9000

1567|abhi|mech|5777

1667|chethan|mech|8677

1887|raghu|mech|5777

Number of records found 4

$

9.3.3 $0 : Special variable stores command name

The shell will store the name of the command you used to call a program in the variable named $0.

It has the number zero because it appears before the first argument on the command line. zero.sh echo 'The name of the command used' echo 'to execute this script was' $0 $sh zero.sh The name of the command used to execute this script was zero.sh 9.3.4 $1-$9: Positional parameters

A positional parameter is a variable within a shell program, its value is set from an arguments specified on the command line. Positional parameters are numbered and are referenced to with a preceding $: such as $1,$2,$3,$4,$5,$6,$7,$8 and $9. pos.sh clear echo $1 $2 $3 $4 $5 echo $0 is the name of the shell script| $sh pos.sh One Two Hello 123 a One Two Hello 123 a pos.sh is the name of the shell script| Now, if we type the name of the Bourne Shell script with no arguments, we get the following results. $sh pos.sh pos.sh is the name of the shell script|

Page 101: Shell Scripting

101

In this sample session, there were no arguments given so none were printed. $0 is the positional parameter that refers to the name of the script. Since there were no arguments given with this invocation of pos.sh, there were zero arguments listed. 9.3.5 Special Parameters $* Represents all of the command-line arguments. $# Contains the number of arguments on the command line. $? Exit status of last command. $$ PID of the current shell. $! PID of the last back ground job. posspec.sh clear echo -e "\n Name of a shell program is : $0" echo -e "\n first argument is : $1" echo -e "\n All the arguments on a command line are : $*" echo -e "\n Number of arguments on a command line are : $#" echo -e "\n Exit status of pervious command is : $?" echo -e "\n Process id of the current shell is : $$" $sh posspec.sh hello good morning 123 a xyz Name of a shell program is : posspec.sh first argument is : hello All the arguments on a command line are : hello good morning 123 a xyz Number of arguments on a command line are : 6 Exit status of pervious command is : 0 Process id of the current shell is : 5273 9.4 set: Set positional parameters and display environment list

The Set command will display a list of all the variables that are set when it has no arguments. $set

HOME=/user0/teacher

IFS=

LOGNAME=chandu

MAIL=/usr/mail/chandu

MAILCHECK=600

PATH=/home/chandu:/bin:/usr/bin

PS1=$

PS2=>

SHELL=/bin/sh

Page 102: Shell Scripting

102

TERM=vt100

When set is called with arguments, it sets the value of positional parameters($1-$9) to the arguments. $set hello good morning

The above command sets the value $1 with “hello”, $2 with “good “ and so on. to

verify, we can use the echo statement to display their values. $echo $1 $2 $3

hello good morning

On giving another set command, the old values of $1 $2 are discarded and the new values get collected. $set 123 xyz abc

$echo $1 $2 $3

123 xyz abc

Write a shell script to display date in desired format using set command.

Using command substitution, you can assign values to the shell's positional parameters with the output of date. setdate.sh clear set `date` #set command also helps command substitution echo -e "\nThe date is : $*" echo -e "\nThe date using positional parameters is : $2 / $3 / $6 " $sh setdate.sh

The date is : Thu Jun 7 19:50:26 IST 2007

The date using positional parameters is : Jun / 7 / 2007

$

9.5 shift : Shifts positional parameters

The shift command promotes each of the command-line arguments. The second argument, represented by $2, is now the first argument, represented by $1. The third becomes the second and so on until the last argument becomes the next to last. You can access only the first nine command-line arguments (as $1 through $9). The shift command gives you access to the tenth, onwards and the first becomes unavailable. For example consider the following command line. $set you have the capacity to learn from mistakes, you will learn a lot in your life. $echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12

you have the capacity to learn from mistakes, you you0 you1 you2

Page 103: Shell Scripting

103

Observe last three words in the output. The occurred in the output because at a time we can access only nine positional parameters. When you tried to refer to $10 it was interpreted by the shell as $1 value followed by 0 hence the output of $10 is you0, same in the story with $11 and $12. $shift # Now first words shifted out $echo $1 $2 $3 $4 $5 $6 $7 $8 $9

have the capacity to learn from mistakes, you will

$shift 2 #two words shifted out $echo $1 $2 $3 $4 $5 $6 $7 $8 $9

capacity to learn from mistakes, you will learn a

Note: Every time you use shift, the left most variable gets lost; so it should be saved in a variable before using shift. $a=$1 b=$2 c=$3

$shift 3

$echo $a $b $c $1 $2 $3 $4 $5 $6 $7 $8 $9

capacity to learn from mistakes, you will learn a lot in your

9.6 expr: Evaluate an arithmatic expression

The expr command will perform arithmetic operation, expr stands for evaluate expression and handles integer and strings.

The arguments are taken as an expression. After the evaluation has taken place, the result is written to standard output. The terms of the expression must be separated by blanks. Special characters to the shell must be escaped. Strings containing blanks or other special characters must be quoted. $expr 7 + 8 + 10 # space is required in between operator 25

$expr 10 - 8

2

$expr 10 '*' 4 # Astrisk has to be escaped 40

$expr 137/ 5 #Decimal portion truncated 27

$expr 13%5

3

expr will also work with user defined variables as in the following Example:

Page 104: Shell Scripting

104

$x=4 y=2

$expr $x + $y

6

$z=`expr $x + $y`# expr is often used with command substitution to assign a variable $echo $z

6

9.6.1 expr with strings It performs three important string functions:

• used to find length of a string along with regular expression .*

• used to extract sub string

• used to locate a position of a character in a string

The length of a string: the regular expression.* along with expr prints the number of characters in a string. $expr “aiswarya” : '.' # Space on either side of : is required 8

Extracting sub string: it extract a string enclosed by the escaped characters \(and \). For example extract 4 characters from a string aiswarya, you must create a pattern for expr command as shown below. $expr "aiswarya" : '....\(....\)'

arya

$ expr "aiswarya" : '\(......\)'#Extract first six characters from a string aiswar

locating a position of a character in a string, expr command also return the location of the first occurrence of a character in side a string. $ expr "aiswarya" : '[^s]*s'

3

$expr "aiswarya" : '[^a]*a'

1

$ expr "aiswarya" : '[^y]*y'

7

9.7 let and ( ) to evaluate expression

Page 105: Shell Scripting

105

$let x=10 y=3

$let z=x+y #no space in between operator and $ sign not required $echo $z

13

$let sum="3*6+4/2"

$echo $sum

20

here let is replaced by ( ) $x=2 y=3

$((z=x +y))# Space is unimportant these are two different ways of using ( ) $ echo $z

5

$a=3 b=6

$ c=$((a+b))

$echo $c

9

9.8 read : Reading Input Into a Shell Variable from the key board

The Bourne Shell script can read user input from standard input. The read command will read one line from standard input and assign the line to one or more variables. The following example shows how this works. read.sh clear echo "Please enter a string of your choice" read a echo $a This simple script will read one line from standard input (keyboard) and assign it to the variable a. $sh read.sh

Please enter a string of your choice

hello this is read

hello this is read

The line read from standard input can also be assigned to several variables as shown in the following example. If the number of arguments supplied is less than the number of variables accepting them, any left over variables will simply remain unassigned. However, when the number of arguments exceeds the number of variables, the remaining words are assigned to the last variable.

Page 106: Shell Scripting

106

read1.sh clear echo "Please enter three strings" read a b c echo -e “\n a is -> $a \t b is -> $b \t c is ->$c\n” $sh read1.sh Please enter three strings life is short make it sweet a is -> life b is -> is c is ->short make it sweet 9.9 export : Exporting shell Variables

Within a process, you can declare, initialize, read, and modify variables. The variable is local to that process. When a process forks a child process, the parent process does not automatically pass the value of the variable to the child process. Here is an example of the variables not being exported. export.sh clear echo -e "\n the value of x is $x" echo -e "\n Enter new value of x" # x value has been changed read x echo -e "\n the New value of x is $x" $x=chandu # x is now local to parent and assigned a value chandu $echo $x

chandu

$sh export.sh

the value of x is # Value of x is not visible in sub shell Enter new value of x

aish

the New value of x is aish

$echo $x #value set inside the script doesn't affects value outside script chandu

Because x is a local variable it is local to login shell, its value can’t be accessed by

echo in the shell program because it is a sub shell. to make x is available globally, you need to use the export statement before the script has been executed. $x=chandu

$export x

$echo $x

Page 107: Shell Scripting

107

chandu

$sh export.sh

the value of x is chandu # Value of x is now visible in sub shell Enter new value of x

aish

the New value of x is aish $echo $x # value set inside the script doesn't affects value outside script chandu

Exporting variables is only valid from the parent to the child process. The child

process cannot change the parent's variable. 9.10 CONTROL CONSTRUCTS

The Bourne Shell control constructs can alter the flow of control within the script. The Bourne Shell provides simple two-way branch if statements and multiple-branch case statements, plus for, while, and until statements. 9.10.1 Conditional control The test utility evaluates expressions and returns a condition indicating whether or not the expression is true (equal to zero) or false (not equal to zero), which is held in the variable $?. The format for this utility is as follows: test expression Expression - composed of constants, variables, and operators. Expressions can contain one or more evaluation criteria that test will evaluate. A -a that separates two criteria is a logical AND operator. In this case, both criteria must evaluate to true in order for test to return a value of true.

The -o is the logical OR operator. When this operator separates two criteria, one or the other (or both) must be true for test to return a true condition. You can negate any criterion by preceding it with an exclamation mark (!). Parentheses can be used to group criteria. If there are no parentheses, the -a (logical AND operator) takes precedence over the -o (logical OR operator). The test utility will evaluate operators of equal precedence from left to right. Another way to do the test evaluation is to surround the expression with left and right brackets. A space character must appear after the left bracket and before the right bracket. [ expression ]

Page 108: Shell Scripting

108

9.10.2 Test on Numeric Values

Test expressions can be in many different forms. The expressions can appear as a set of evaluation criteria.

The general form for testing numeric values is:

test int1 op int2 This criterion is true if the integer int1 has the specified algebraic relationship to

integer int2. The valid operators (op) are: -eq equal -ne not equal -gt greater than -lt less than -ge greater than or equal -le less than or equal $x=2 ; y=3

$test $x -gt $y

$echo $?

1 # false $test $y -gt $x

$echo $?

0 # True 9.10.3 Test on Character Strings

The evaluation criterion for character strings is similar to numeric comparisons. The general form is: string1 op string2 The operators (op) are: string1 = string2 true if string1 and string 2 are equal. string1 != string2 true if string1 and string2 are not equal. -n string1 true if string1 is not a null string. -z string1 true if string1 is a null string. $s1="aish"

$test -n $s1

$echo $?

0

$test -z $s1

$echo $?

1

$s2="aish"

$test $s1 = $s2

Page 109: Shell Scripting

109

$echo $?

0

$test $s1 != $s2

$echo $?

1

9.10.4 Test on File Types The test utility can be used to determine information about file types. Such as -f file true if file exists and it is a plain file -r file true if file exists and is readable -w file true if file exists and is writable -x file true if file exists and is executable -d file true if file exists and it is a directory. -L file true if file exists and it is symbolic link file -s file true if file exits and it contains data(size greater than 0 bytes) $ ls -l

total 8

-rw-r--r-- 1 yogish users 264 2007-06-06 23:09 first2.sh

drwxr-xr-x 2 yogish users 48 2007-06-10 21:46 shelldir

-rwxr-xr-x 1 yogish users 64 2007-06-06 22:35 spg1.sh

$ test -f first2.sh #first2.sh is existing $ echo $?

0

$test -r first2.sh

$ echo $? #first2.sh it is readable 0

$test -w first2.sh #first2.sh it is writable $echo $?

0

$test -x first2.sh

$echo $? #first2.sh it is not executable 1

$test -f shelldir

$echo $? #it is exsting but it is a directory 1

Frequently used test operators are listed in the table below.

Page 110: Shell Scripting

110

File Operators returns TRUE if

-d file file exists and is a directory

-f file file exists and is an ordinary file

-r file file exists and is readable

-w file file exists and is writable

-x file file exists and is executable

-s file file exists and its size is non-zero

Integer Operators returns TRUE if

int1 -eq int2 int1 is equal to int2

int1 -ne int2 int1 is not equal to int2

int1 -lt int2 int1 is less than int2

int1 -le int2 int1 is less than or equal to int2

int1 -gt int2 int1 is greater than int2

int1 -ge int2 int1 is greater than or equal to int2

String Operators returns TRUE if

string1 = string2 string1 is equal to string2

string1 != string2 string1 is not equal to string2

-z string string is null (and must be seen)

Logical Operators returns TRUE if

! expr expr is FALSE, otherwise returns TRUE

expr1 -o expr2 expr1 is TRUE OR expr2 is TRUE

expr1 -a expr2 expr1 is TRUE AND expr2 is TRUE

9.10.5 Logical Operators : && and ||

The && operator is used by the shell in the same sense as it is used in C. It combines two commands.

These constructs enable you to execute a command based on whether or not the previous command succeeds or fails General Form: command1 && command2 execute command2 if command1 succeeds . For example: patfile you have the ability to learn from the mistakes. there is no substitute for hard work. you will learn a lot in life $grep learn patfile && echo -e "pattern found in a file"

you have the ability to learn from the mistakes.

you will learn a lot in life

Page 111: Shell Scripting

111

pattern found in a file

$ grep soft patfile && echo "pattern found in a file"

$ #there is no pattern with the name soft hence command fails so that command2 wont execute.

The || operator is used by the shell in the same sense as it is used in C. It combines two commands. General Form: command1 || command2 Executes command2 if command1 fails $grep learn patfile || echo -e "pattern not found in a file"

you have the ability to learn from the mistakes.

you will learn a lot in life

# In the above example command1 succeed, hence it wont execute second command2. $grep soft patfile && echo "pattern not found in a file"

pattern not found in a file

In this example command1 fails, hence it execute second command2.

9.10.6 if then The format for this construct is: if expression then command(s) fi

The if statement evaluates the expression and then returns control based on this status. The fi statement marks the end of the if, notice that fi is if spelled backward.

The if statement executes the statements immediately following it if the expression returns a true status. If the return status is false, control will transfer to the statement following the fi. big3.sh - Program to find biggest of three numbers using simple if clear echo -e "Enter the 1st num:\c" read num1 echo -e "Enter the 2nd num: \c" read num2 echo -e "Enter the 3rD num: \c"

Page 112: Shell Scripting

112

read num3 big=$num1 if [ $big -lt $num2 ] then big=$num2 fi if [ $big -lt $num3 ] then big=$num3 fi echo -e "The largest of three numbers $num1 , $num2 and $num3 is $big\n\n" $sh big3.sh

Enter the 1st num:22

Enter the 2nd num: 58

Enter the 3rD num: 19

The largest of three numbers 22 , 58 and 19 is 58

$sh big3.sh

Enter the 1st num:9

Enter the 2nd num: 15

Enter the 3rD num: 19

The largest of three numbers 9 , 15 and 19 is 19

$sh big3compuond.sh

Enter the 1st num:11

Enter the 2nd num: 14

Enter the 3rD num: 15

The largest of three numbers 11 , 14 and 15 is 15

9.10.7 set -x: Debugging shell scripts Set serves as a debugging tool with an option -x. When used inside a script, it echoes each statement on the terminal, preceded by a + as it is executed. set +x turns off set -x. $sh big3.sh

+ sh big

+ echo -e 'Enter the 1st num:\c'

Enter the 1st num:+ read num1

14

+ echo -e 'Enter the 2nd num: \c'

Page 113: Shell Scripting

113

Enter the 2nd num: + read num2

11

+ echo -e 'Enter the 3rD num: \c'

Enter the 3rD num: + read num3

15

+ big=14

+ '[' 14 -lt 11 ']'

+ '[' 14 -lt 15 ']'

+ big=15

+ echo -e 'The largest of three numbers 14 , 11 and 15 is

15\n\n'

The largest of three numbers 14 , 11 and 15 is 15

This is an ideal tool to use if you truble finding out why scripts that accept input from the user. Note how the shell prints each statement as it is being executed, affixing a + to each. largest of three numbers using - logical operator clear echo -e "Enter the 1st num:\c" read num1 echo -e "Enter the 2nd num: \c" read num2 echo -e "Enter the 3rD num: \c" read num3 big=0 if [ $num1 -gt $num2 -a $num1 -gt $num3 ] then big=$num1 fi if [ $num2 -gt $num1 -a $num2 -gt $num3 ] then big=$num2 fi if [ $num3 -gt $num1 -a $num3 -gt $num1 ] then big=$num3 fi echo -e "The largest of three numbers $num1 , $num2 and $num3 is $big\n\n" $sh big3compuond.sh

Enter the 1st num:15

Enter the 2nd num: 11

Enter the 3rD num: 14

The largest of three numbers 15 , 11 and 14 is 15

Page 114: Shell Scripting

114

9.10.8 if then else The format for this construct is: if expression then command(s) else command(s) fi

The else part of this structure makes the single-branch if statement into a two-way branch. If the expression returns a true status, the commands between the then and the else statement will be executed. After these have been executed, control will start again at the statement after the fi.

If the expression returns false, the commands following the else statement will be executed. #lar2.sh - Program to find largest of 2 numbers clear echo -e "\n enter two numbers\n" read num1 read num2 if test $num1 -gt $num2 then echo -e "\n $num1 is greater than $num2" else echo -e "\n $num2 is greater than $num1 " fi $sh lar2.sh

enter two numbers

19

9

19 is greater than 9

$sh lar2.sh

enter two numbers

22

58

Page 115: Shell Scripting

115

58 is greater than 22

#numdev2.sh - Program to check number divisible by 2 clear echo -e "\n enter number to be check : \c " read num if test `expr $num % 2 ` -eq 0 then echo -e "\n $num is Divisible by 2" else echo -e "\n $num is not divisible by 2 " fi $sh div.sh

enter number to be check : 22

22 is Divisible by 2

$sh div.sh

enter number to be check : 58

58 is Divisible by 2

$sh div.sh

enter number to be check : 19

19 is not divisible by 2

write a shell scripts to Check permissions of two files are equal or

not

clear

echo enter two file names

read f1

read f2

ls -l $f1 $f2

p1=`ls -l $f1 | cut -c 2-10`

p2=`ls -l $f2 | cut -c 2-10`

if test $p1 = $p2

then

echo -e "\n Permissions are Equal : $p1\n"

else

echo -e "\n Permissions are not-Equal \n"

echo -e " hence $f1 permissions are:$p1"

echo -e " \t$f2 permissions are:$p2\n"

fi

$sh perm.sh

enter two file names

f1

Page 116: Shell Scripting

116

f2

-rw-r--r-- 1 kishan kishan 4 Jul 27 13:54 f1

-rw-r--r-- 1 kishan kishan 0 Jul 27 13:54 f2

Permissions are Equal : rw-r--r--

$sh perm.sh enter two file names f1 f2 -r--r--r-- 1 kishan kishan 4 Jul 27 13:54 f1 -rw-r--r-- 1 kishan kishan 0 Jul 27 13:54 f2 Permissions are not-Equal hence f1 permissions are:r--r--r-- f2 permissions are:rw-r--r-- # Program to find file existing and is read permissin and have write permission' clear echo -e "\n Enter file name " read fname if [ -e $fname ] then echo $fname is existing and its attributes are: ls -l $fname if [ -r $fname ] then echo $fname having Read permission else echo $fname Does not have, Read permission fi if [ -w $fname ] then echo $fname having Write permission else echo $fname Does not have, Write permission fi if [ -x $fname ] then echo $fname having Executable permission else echo $fname Does not have, Executable permission fi if [ -s $fname ]

Page 117: Shell Scripting

117

then echo $fname have size greater than Zero else echo $fname have size 0 bytes fi else echo $fname is not-Existing fi

$sh fileser.sh

Enter file name a

a is existing and its attributes are:

-rw-rw-r-- 1 yogish yogish 0 Jul 18 12:12 a

a having Read permission

a having Write permission

a Does not have, Executable permission

a have size 0 bytes

$sh fileser.sh #After chmod u+x a

Enter file name a

a is existing and its attributes are:

-rwxrw-r-- 1 yogish yogish 0 Jul 18 12:12 a

a having Read permission

a having Write permission

a having Executable permission

a have size 0 bytes

$sh fileser.sh #After chmod u-r a

Enter file name a #and appending data to a

a is existing and its attributes are:

--wxrw-r-- 1 yogish yogish 6 Jul 18 12:14 a

a Does not have, Read permission

a having Write permission

a having Executable permission

a have size greater than Zero

9.10.9 if then elif The format for this construct is: if expression then command(s) elif expression then command(s)

Page 118: Shell Scripting

118

else command(s) fi

The elif construct combines the else and if statements and allows you to construct a nested set of if then else structures.

#nameday.sh - Program to display name of a day using nesting of if -- elif clear echo -e "Enter day number: \c " read day if test $day -eq 1 then echo -e "\nsunday\n" elif test $day -eq 2 then echo -e "\nmonday\n" elif test $day -eq 3 then echo -e "\ntuesday\n" elif test $day -eq 4 then echo -e "\nwednesday\n" elif test $day -eq 5 then echo -e "\nthursday\n" elif test $day -eq 6 then echo -e "\nfriday\n" elif test $day -eq 7 then echo -e "\nsaturday\n" else echo -e "\ninvalid day\n" fi $sh nameday.sh

Enter day number: 1

sunday

$sh nameday.sh

Enter day number: 3

tuesday

Page 119: Shell Scripting

119

$sh nameday.sh

Enter day number: 7

saturday

#grade.sh - Program to check the grade clear echo "enter the marks of 3 subjects:" read a read b read c if [ $a -ge 35 -a $b -ge 35 -a $b -ge 35 ] then total=`expr $a + $b + $c` avg=`expr $total / 3` echo -e "\ntotal= $total\n" echo -e "\nAverage= $avg\n" if [ $avg -ge 70 ] then echo "Passed with Distinction" elif [ $avg -ge 60 -a $avg -lt 70 ] then echo "Passed with First Class" elif [ $avg -ge 50 -a $avg -lt 60 ] then echo "Passed with Second Class" elif [ $avg -ge 35 -a $avg -lt 50 ] then echo "Just Pass" else echo " Fail...!!!!!!!" fi else echo " Fail...!!!!!!!" fi $sh grade enter the marks of 3 subjects:

2

46

99

Fail...!!!!!!!

$sh grade

enter the marks of 3 subjects:

-23

69

Page 120: Shell Scripting

120

90

Fail...!!!!!!!

$sh grade

enter the marks of 3 subjects:

35

68

85

total= 188

Average= 62

Passed with First Class

$sh grade

enter the marks of 3 subjects:

45

65

89

total= 199

Average= 66

Passed with First Class

$sh grade

enter the marks of 3 subjects:

65

75

85

total= 225

Average= 75

Passed with Distinction

$sh grade

enter the marks of 3 subjects:

35

45

50

total= 130

Average= 43

Just Pass

$

9.10.10 case

Page 121: Shell Scripting

121

The format for this construct is: case test-string in

pattern-1 ) command(s) ;; pattern-2 ) command(s) ;; pattern-3 ) command(s) ;; . . . *) command(s) ;; esac

The case structure allows a multiple-branch decision mechanism. The path that is taken depends on a match between the test-string and one of the patterns. Sample Session: case1.sh echo 'Enter A, B, or C: \c' read letter case $letter in A) echo 'You entered A' ;; B) echo 'You entered B' ;; C) echo 'You entered C' ;; *) echo 'You did not enter A, B, or C' ;; esac $sh case1.sh

Enter A, B, or C: B

You entered B

$sh case1.sh

Enter A, B, or C: z

You did not enter A, B, or C

This example uses the value of a character that the user entered as the test string.

The value is represented by the variable letter. If letter has the value of A, the structure will execute the command following A. If letter has a value of B or C, then the appropriate commands will be executed. The asterisk indicates any string of characters; and it, therefore, functions as a catchall for a no-match condition. The lowercase b in the second sample session is an example of a no match condition.

Page 122: Shell Scripting

122

#day.sh - Program to display name of a day in a week clear echo -e "***********MENU************ \n" echo -e " 1 - Sunday" echo -e " 2 - Monday" echo -e " 3 - Tuesday" echo -e " 4 - Wednesday" echo -e " 5 - Thursday" echo -e " 6 - Friday" echo -e " 7 - Saturday.." echo -e " Enter day : \c " read ch case $ch in 1|s|sun ) echo -e "Sunday" ;; 2|m|mon ) echo -e "\n Monday" ;; 3|t|tue ) echo -e "\nTuesday" ;; 4|w|wed ) echo -e "\nWednesday" ;; 5|tt|thu) echo -e "\nThursday" ;; 6|f|fri ) echo -e "\nfriday" ;; 7|st|sat) echo -e "\nsathurday" ;; *) echo -e "\n Invalid Day..." esac $ sh day.sh

***********MENU************

1 - Sunday

2 - Monday

3 - Tuesday

4 - Wednesday

5 - Thursday

6 - Friday

Page 123: Shell Scripting

123

7 - Saturday..

Enter day : 1

Sunday

$ sh day.sh

***********MENU************

1 - Sunday

2 - Monday

3 - Tuesday

4 - Wednesday

5 - Thursday

6 - Friday

7 - Saturday..

Enter day : 4

Wednesday

$sh day.sh

***********MENU************

1 - Sunday

2 - Monday

3 - Tuesday

4 - Wednesday

5 - Thursday

6 - Friday

7 - Saturday..

Enter day : 7

sathurday

$ sh day.sh

***********MENU************

1 - Sunday

2 - Monday

3 - Tuesday

4 - Wednesday

5 - Thursday

6 - Friday

7 - Saturday..

Enter day : 8

Invalid Day...

#arith.sh - Simple Calculator

Page 124: Shell Scripting

124

clear echo "Enter two numbers:" read a read b echo -e "\nEnter your choice:\n1.Add\n2.Subtract\n3.Product\n4.Quotient\n" read ch case $ch in 1|a|add) c=`expr $a + $b` echo -e "\nSum= $c\n" ;; 2|s|sub) c=`expr $a - $b` echo -e "\nDifference= $c\n" ;; 3|m|mul) c=`expr $a \* $b` echo -e "\nProduct= $c\n" ;; 4|d|div) if [ $b -eq 0 ] then echo -e "\n Devide error ..." else c=`expr $a / $b` echo -e "\nQuotient= $c\n" fi ;; *) echo -e "\ninvalid day\n" esac $sh arith.sh

Enter two numbers:

22

58

Enter your choice:

1.Add

2.Subtract

3.Product

4.Quotient

1

Page 125: Shell Scripting

125

Sum= 80

$sh arith.sh

Enter two numbers:

22

58

Enter your choice:

1.Add

2.Subtract

3.Product

4.Quotient

s

Difference= -36

$ sh arith.sh

Enter two numbers:

09

19

Enter your choice:

1.Add

2.Subtract

3.Product

4.Quotient

mul

Product= 171

$sh arith.sh

Enter two numbers:

58

0

Enter your choice:

1.Add

2.Subtract

3.Product

4.Quotient

4

Devide error ...

$sh arith.sh

Page 126: Shell Scripting

126

Enter two numbers:

58

14

Enter your choice:

1.Add

2.Subtract

3.Product

4.Quotient

4

Quotient= 4

9.10.11 Loops

A loop involves repeating some portion of the program either a specified number of times or until a particular is being satisfied.

There are three methods by way of which we can repeat a part of a program. They are

• Using a for loop

• using a while loop

• using an until loop 9.10.11.1 for The format for this construct is: for loop-index in argument-list do command(s) done

This structure will assign the value of the first item in the argument list to the loop index and executes the commands between the do and done statements. The do and done statements indicate the beginning and end of the for loop.

After the structure passes control to the done statement, it assigns the value of the second item in the argument list to the loop index and repeats the commands. The structure will repeat the commands between the do and done statements once for each argument in the argument list. When the argument list has been exhausted, control passes to the statement following the done.

#for1.sh

clear

Page 127: Shell Scripting

127

for var in karu jun 15 deepu aug 11 yogi jun 14 do echo -e "\t Var value is : $var " done

$sh for1.sh

Var value is : karu

Var value is : jun

Var value is : 15

Var value is : deepu

Var value is : aug

Var value is : 11

Var value is : yogi

Var value is : jun

Var value is : 14

Possible sources of argument-list 9.10.11.1.1. List from Variables forlist1.sh clear for x in $HOME $LOGNAME do echo $x done $sh forlist1.sh /home/chandu

chandu

9.10.11.1.2 List from Command Substitution

# Program to display files in a current directory # forlist.sh clear for x in `ls` do echo $x done $sh forlist.sh break.sh

cont.sh

deletefile.sh

emp.dat

Page 128: Shell Scripting

128

exit.sh

for1

for3.sh

forlist.sh

sqr.sh

u1.sh

u2.sh

u3.sh

w1.sh

w2.sh

9.10.11.1.3 List from Wild-Cards forlist3.sh clear for x in $* $# do echo $x done $sh forlist3.sh hello this is an example for, for with wild cards hello

this

is

an

example

for,

for

with

wild

cards

10

9.10.11.1.4 List from Positional Parameters forlist4.sh clear for x in $1 $2 $3 $4 do echo $x done $sh forlist4.sh hello this is an example for, for with positional parameters hello this is an $ # Write a shell script to display command line arguments in reverse order # For example $File A b C output should be C b A # revarg.sh

Page 129: Shell Scripting

129

clear app=" " for var in $* do rev="$var $app"

app=$rev done echo -e "The original args are: $*" echo -e "\nReversed command line args are:$rev" $sh revarg.sh karu jun 15 deepu aug 11 yogi jun 14

The original args are: karu jun 15 deepu aug 11 yogi jun 14

Reversed command line args are:14 jun yogi 11 aug deepu 15

jun karu

9.10.11.2 while The format for this construct is: while expression do command(s) done

As long as the expression returns a true exit status, the structure continues to execute the commands between the do and the done statement. Before each loop through the commands, the structure executes the expression. When the exit status of the expression is false (non-zero), control is passed to the statement following the done statement.

The commands to be executed must change the expression test or an infinite loop can result. Write a shell script to display numbers between 1 to 10. #wdisp1to10.sh clear count=1 while [ $count -le 10 ] do echo $count count=`expr $count + 1` #command substitution done $sh wdisp1to10.sh

1

2

Page 130: Shell Scripting

130

3

4

5

6

7

8

9

10

$

# Program to Genarate Multiplication table of a Given Number # w2.sh clear echo -e " Enter a number : \c" read num i=1 while test $i -le 10 do echo "$num X $i = `expr $num \* $i`" i=`expr $i + 1` done $sh w2.sh

Enter a number : 8

8 X 1 = 8

8 X 2 = 16

8 X 3 = 24

8 X 4 = 32

8 X 5 = 40

8 X 6 = 48

8 X 7 = 56

8 X 8 = 64

8 X 9 = 72

8 X 10 = 80

# Program to genarate first N fibonacci numbers #w3.sh clear echo -e "Enter any number :\c " read n fib1=0 fib2=1 count=3 echo -e "\n\t$fib1\n\t$fib2" while [ $count -le $n ] do fib3=`expr $fib1 + $fib2`

Page 131: Shell Scripting

131

echo -e "\t$fib3" fib1=$fib2 fib2=$fib3 count=`expr $count + 1` done $sh w3.sh

Enter any number :7

0

1

1

2

3

5

8 # Program to find Sum of First N numbers # w4.sh clear echo -e "\n Enter value of N :\c" read n i=1 sum=0 while test $i -lt $n do sum=`expr $sum + $i` echo $i i=`expr $i + 1` done echo -e "\n\n Sum of above said numbers is : $sum\n\n" $sh w4.sh

Enter value of N :5

1

2

3

4

Sum of above said numbers is : 10

# Program to find OddSum and EvenSum of First N numbers # w5.sh clear echo -e "\n Enter value of N :\c" read n

Page 132: Shell Scripting

132

i=1 esum=0 osum=0 while test $i -lt $n do if test `expr $i % 2` -eq 0 then esum=`expr $esum + $i` echo $i else osum=`expr $osum + $i` echo $i fi i=`expr $i + 1` done echo -e "\n\n Even Sum = $esum and Odd Sum = $osum\n\n" $sh w5.sh

Enter value of N :8

1

2

3

4

5

6

7

Even Sum = 12 and Odd Sum = 16

# Program to Check given Number is Prime or Not # w6.sh clear echo -e "\n Enter value of N :\c" read n i=2 while test $i -lt $n do if test `expr $n % $i` -eq 0 then echo -e "\n Given Number $n is not a Prime\n" exit fi i=`expr $i + 1` done echo -e "\n Given Number $n is a Prime\n"

Page 133: Shell Scripting

133

$sh w6.sh

Enter value of N :5

Given Number 5 is a Prime

$sh w6.sh

Enter value of N :6

Given Number 6 is not a Prime

$sh w6.sh

Enter value of N :9

Given Number 9 is not a Prime

#Program to check named user is loin to the system or not #else display the message not logged in until he logs into the system #lo.sh clear echo enter login name read name while true do count=`who | grep -cw $name` if [ $count -gt 0 ] then echo -e "\The user $name has been logged into the system" exit else echo -e "\n Not Logged in" fi done $sh lo.sh enter login name yogish Not Logged in Not Logged in Not Logged in . . . # assume at this moment he logged into the system The user yogish has been logged into the system $

Page 134: Shell Scripting

134

# Shell script to find all the numbers divisible by given number # divall.sh clear echo -e "\n Enter Lower range and Upper range " read low read up echo -e "\n Enter a num to devide :\c " read num echo -e "\n Numbers divisible by : $num Between $low to $up are" while test $low -le $up do if test `expr $low % $num` -eq 0 then echo -e " \t $low " fi low=`expr $low + 1` done

$sh divall.sh

Enter Lower range and Upper range:

2

10

Enter a num to devide :2

Numbers divisible by : 2 Between 2 to 10 are

2

4

6

8

10

$sh divall.sh

Enter Lower range and Upper range:

2

10

Enter a num to devide :3

Numbers divisible by : 3 Between 2 to 10 are

3

6

9

$sh divall.sh

Page 135: Shell Scripting

135

Enter Lower range and Upper range

2

10

Enter a num to devide : 5

Numbers divisible by : 5 Between 2 to 10 are

5

10

# Write a shell script to find square of a given number using Successive Addition

clear echo -e "\n Enter a number :\c " read n sqr=0 i=0 while test $i -lt $n do sqr=`expr $sqr + $n` i=`expr $i + 1` done echo -e "\n The square of the given number $n is $sqr\n\n" $ sh sqr.sh

Enter a number :4

The square of the given number 4 is 16

$ sh sqr.sh

Enter a number :5

The square of the given number 5 is 25

$ sh sqr.sh

Enter a number :6

The square of the given number 6 is 36

Write shell script to display calendar for current month with current date replaced by * or ** depending on whether data has one digit or two digits.

clear

day=`date +%d`

if [ $day -lt 10 ]

then

cal | sed "s/$day/*/"

else

cal | sed "s/$day/**/"

fi

Page 136: Shell Scripting

136

$sh cal.sh

July 2007

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 ** 28

29 30 31

# Program to delete file -- Depending upon date enter by the user #deletefile.sh clear echo -e "\n\n\t Enter the date (Mmm DD) : \c" read m while [ 0 ] do c=`ls -l | grep -ic -e"$m"` clear echo "Number of files created/modified on "$m" is $c" if [ $c -eq 0 ] then echo -e "\n No Files are Created/Modified on "$m"" exit else ls -l | grep -i "$m" | tr -s " " | cut -d" " -f 9 >temp echo -e "\n The Files Created on "$m" are : " cat temp echo -e "\n Choice \t Function" echo -e "\n a \t Delete one by one" echo -e "\n b \t Delete all" echo -e "\n Enter Choice :\c" read cho case $cho in a) cat temp echo -e " Enter file name to be deleted " read file rm -i $file ;; b) rm -i `cat temp` ;; *) echo -e "\n Wrong input choice " exit

Page 137: Shell Scripting

137

esac rm temp fi done echo -e "\n\n" $sh deletefile.sh

Number of files created/modified on Jul 4 is 3

The Files Created on Jul 4 are :

ab.sh

mn.sh

xy.sh

Choice Function

a Delete one by one

b Delete all

Enter Choice :a

ab.sh

mn.sh

xy.sh

Enter file name to be deleted

mn.sh

rm: remove regular file `mn.sh'?y

Number of files created/modified on Jul 4 is 2

The Files Created on Jul 4 are:

ab.sh

xy.sh

Choice Function

a Delete one by one

b Delete all

Enter Choice: a

ab.sh

xy.sh

Enter file name to be deleted

Page 138: Shell Scripting

138

ab.sh

rm: remove regular file `ab.sh'?n

Number of files created/modified on Jul 4 is 2

The Files Created on Jul 4 are:

ab.sh

xy.sh

Choice Function

a Delete one by one

b Delete all

Enter Choice: b

rm: remove regular file `ab.sh'? y

rm: remove regular file `xy.sh'? y

Number of files created/modified on Jul 4 is 0

No Files are Created/Modified on Jul 4

#numeric.sh - Program to check the number is NUMERIC clear echo -e "enter a number : \c" read a echo $a > numcheck i=1 m=`expr length $a` while test $i -le $m do c=`echo $a | cut -c $i` case $c in [!\0-9]) echo -e "\n $a is not numeric \n" exit ;; esac i=`expr $i + 1` done echo -e "\n $a Is numeric \n" $sh numeric.sh

Page 139: Shell Scripting

139

enter a number : 12

12 Is numeric

$sh numeric.sh

enter a number : q12

q12 is not numeric

$sh numeric.sh

enter a number : 12asd12

12asd12 is not numeric

9.10.11.3 until The format for this construct is: until expression do command(s) done

The until and while structures are very similar. The only difference is that the test is at the top of the loop. The until structure will continue to loop until the expression returns true or a no error condition. The while loop will continue as long as a true or no error condition is returned.

The commands to be executed must change the expression test or an infinite loop can result. # Program to Display numbers from 1 to 10 i=1 until test $i -gt 10 do echo $i i=`expr $i + 1` done $sh u1.sh

1

2

3

4

5

6

7

8

Page 140: Shell Scripting

140

9

10

# Program to Genarate Multiplication table of a Given Number clear echo -e " Enter a number : \c" read num i=1 until test $i -gt 10 do echo "$num X $i = `expr $num \* $i`" i=`expr $i + 1` done

$sh u2.sh

Enter a number : 7

7 X 1 = 7

7 X 2 = 14

7 X 3 = 21

7 X 4 = 28

7 X 5 = 35

7 X 6 = 42

7 X 7 = 49

7 X 8 = 56

7 X 9 = 63

7 X 10 = 70

# Program to genarate first N fibonacci numbers # u3.sh clear echo -e "Enter any number :\c " read n fib1=0 fib2=1 count=3 echo -e "\n\t$fib1\n\t$fib2" until [ $count -gt $n ] do fib3=`expr $fib1 + $fib2` echo -e "\t$fib3" fib1=$fib2 fib2=$fib3 count=`expr $count + 1` done

$sh u3.sh

Page 141: Shell Scripting

141

Enter any number :7

0

1

1

2

3

5

8

# Program to find Sum of First N numbers # u4.sh clear echo -e "\n Enter value of N :\c" read n i=1 sum=0 until test $i -ge $n do sum=`expr $sum + $i` echo $i i=`expr $i + 1` done echo -e "\n\n Sum of above said numbers is : $sum\n\n"

$sh u4.sh

Enter value of N :8

1

2

3

4

5

6

7

Sum of above said numbers is : 28

# Program to find OddSum and EvenSum of First N numbers # u5.sh clear echo -e "\n Enter value of N :\c" read n i=1 esum=0

Page 142: Shell Scripting

142

osum=0 until test $i -ge $n do if test `expr $i % 2` -eq 0 then esum=`expr $esum + $i` echo $i else osum=`expr $osum + $i` echo $i fi i=`expr $i + 1` done echo -e "\n\n Even Sum = $esum and Odd Sum = $osum\n\n" $sh u5.sh

Enter value of N :10

1

2

3

4

5

6

7

8

9

Even Sum = 20 and Odd Sum = 25

# Program to Check given Number is Prime or Not # u6.sh clear echo -e "\n Enter value of N :\c" read n i=2 until test $i -ge $n do if test `expr $n % $i` -eq 0 then echo -e "\n Given Number $n is not a Prime\n" exit fi i=`expr $i + 1` done echo -e "\n Given Number $n is a Prime\n"

Page 143: Shell Scripting

143

$sh u6.sh

Enter value of N :9

Given Number 9 is not a Prime

$ sh u6.sh

Enter value of N :17

Given Number 17 is a Prime

$ sh u6.sh

Enter value of N :22

Given Number 22 is not a Prime

# Shell script to find all the numbers divisible by given number # divall.sh clear echo -e "\n Enter Lower range and Upper range " read low read up echo -e "\n Enter a num to devide :\c " read num echo -e "\n Numbers divisible by : $num Between $low to $up are" until test $low -gt $up do if test `expr $low % $num` -eq 0 then echo -e " \t $low " fi low=`expr $low + 1` done

$sh divall.sh

Enter Lower range and Upper range:

2

10

Enter a num to devide :2

Numbers divisible by : 2 Between 2 to 10 are

2

Page 144: Shell Scripting

144

4

6

8

10

$sh divall.sh

Enter Lower range and Upper range:

2

10

Enter a num to devide :3

Numbers divisible by : 3 Between 2 to 10 are

3

6

9

$sh divall.sh

Enter Lower range and Upper range

2

10

Enter a num to devide : 5

Numbers divisible by : 5 Between 2 to 10 are

5

10

# Program to Display numbers.... Using break from 1 to 10 # break.sh i=1 while test $i -le 10 do if [ $i -eq 5 ] then break fi echo -e "\t Value of i is $i" i=`expr $i + 1` done echo “end of program after break” $sh break.sh

Value of i is 1

Value of i is 2

Value of i is 3

Value of i is 4

Page 145: Shell Scripting

145

end of program after break

# Program to Display numbers.... Using continue from 1 to 10 # cont.sh i=1 while test $i -le 10 do if [ $i -eq 5 ] then i=`expr $i + 1` continue fi echo -e "\t Value of i is : $i" i=`expr $i + 1` done echo “end of program after continue” $sh cont.sh

Value of i is : 1

Value of i is : 2

Value of i is : 3

Value of i is : 4

Value of i is : 6

Value of i is : 7

Value of i is : 8

Value of i is : 9

Value of i is : 10

end of program after continue

# Program to Display numbers using -- exit # exit.sh i=1 while test $i -le 10 do if [ $i -eq 5 ]

then exit

fi echo -e "\t Value of is : $i" i=`expr $i + 1` done echo “end of program” $sh exit.sh

Page 146: Shell Scripting

146

Value of is : 1

Value of is : 2

Value of is : 3

Value of is : 4

9.11 The trap command Syntax: $ trap commands signals

This command tells the shell to execute commands whenever it receives one of the signals listed in signals. The listed signals can be specified by name or number. Trap with no arguments prints a list of the current trap assignments. If the first argument is the null string, as in $trap “ “ signals

The signals in signals are ignored when received by the shell. If the format: $trap signals

Is used, processing of each signal listed in signals is reset to the default action. Examples: $trap “echo hangup >> $errfile; exit” 2 Log message and exit on hangup

$trap “rm $TMPFILE; exit” 1 2 15 Removes $TMPFILE on signals 1, 2 or 15

$trap “ “ 2 Ignores interrupts $trap 2 Reset default processing of interrupts Signal number Signal name Generated for

0 EXIT Exit from the shell 1 HUP Hang-up 2 INT Interrupt – Example Delete key, ^c 3 QUIT Quit 6 ABRT Abort 9 KILL Kill

14 ALRM Alarm timeout 15 TERM Software termination signal(sent by kill by default)

Table: Signal number and names for trap 9.12 Arrays

Page 147: Shell Scripting

147

UNIX provides one-dimensional array variables. Any variable may be used as an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Array indexing starts at zero. An array element is accessed with a subscript, which is an integer valued expression enclosed inside a pair of brackets.

To create an array variable you can use either, 1. arr_name[subscript]=value 2. arr_name=(values) In the first method, the subscript is treated as an arithmetic expression that must

be evaluated to a number greater than or equal to zero. arr_name[11]=23 arr_name[15]=a arr_name[10]=hello

In the second method, the value needs to be separated by a space.

days=(Sun Mon Tue Wed Thu Fri Sat)

Any element of an array may be referenced using: ${arr_name[subscript]} Referencing an array variable without a subscript is equivalent to referencing element zero. $echo “arr_name[11] =${arr_name[11]}”

23

$echo “Contents of arr_name[15] is ${arr_name[15]}”

a

Contents of un-initialized array variable print blank (null variable). $echo “arr_name[9] =${arr_name[9}” # Displays NULL The construct [*] can be used as a subscript to substitute all the elements of the array on the command line, with each element delimited by a single space character. $echo ${arr_name[*]}

Sun Mon Tue Wed Thu Fri Sat

Or All the elements of an array is displayed by using : echo {arr_name[@]} Or echo {arr_name[*]}

Page 148: Shell Scripting

148

$echo –e “ \t Contents of an array is : {days[@]}”

Sun Mon Tue Wed Thu Fri Sat

To find the size or number of elements in an array: echo {#arr_name[@]}

Or echo {#arr_name[*]}

$echo –e “ \t Size of an array is : {#days[@]}”

7

The unset command is used to destroy complete array or individual array element.

• unset arr_name[subscript] # Destroys the array element at index subscript.

• Unset arr_name # Removes the entire array

1. Program to Read and Display N elements clear echo -e " Enter size of an array : \c" read size i=0 echo -e "\n Enter elements of an array : " while test $i -lt $size do

read a[i] i=`expr $i + 1` done echo -e "\n\n Contets of an array are using loop\n" i=0 while test $i -lt $size do echo -e " \t\t a[ $i ] = ${a[i]} " i=`expr $i + 1` done echo -e "\n Number of elements in an array is : ${#a[@]} \n\n" echo -e "\n Contents of an array without loop is : ${a[@]} \n\n" $sh arr1.sh

Enter size of an array : 7

Enter elements of an array

Dob

of

Page 149: Shell Scripting

149

Kishan

is

15

June

2007

Contets of an array are using loop

a[ 0 ] = Dob

a[ 1 ] = of

a[ 2 ] = Kishan

a[ 3 ] = is

a[ 4 ] = 15

a[ 5 ] = June

a[ 6 ] = 2007

Number of elements in an array is : 7

Contents of an array without loop is : Dob of Kishan is

15 June 2007

2. Program to Find Sum of N elements clear echo -e " Enter size of an array : \c" read size i=0 echo -e "\n Enter elements of an array " while test $i -lt $size do read a[i] i=`expr $i + 1` done echo -e "\n Contents of an array is : ${a[@]} \n\n" i=0 sum=0 while test $i -lt $size do sum=`expr $sum + ${a[i]}` i=`expr $i + 1` done echo -e "\n Sum of $size elements in the array is : $sum \n\n"

Page 150: Shell Scripting

150

$sh arrsum.sh

Enter size of an array : 5

Enter elements of an array

9

19

11

15

14

Contents of an array is : 9 19 11 15 14

Sum of 5 elements in the array is : 68

3. Program to Find Even Sum and Odd sum of N elements clear echo -e " Enter size of an array \c" read size i=0 echo -e "\n Enter elements of an array: " while test $i -lt $size do read a[i] i=`expr $i + 1` done echo -e "\n Contents of an array is : ${a[@]} \n\n" i=0 esum=0 osum=0 while test $i -lt $size do if test `expr ${a[i]} % 2` -eq 0 then esum=`expr $esum + ${a[i]}` else osum=`expr $osum + ${a[i]}` fi i=`expr $i + 1` done echo -e "\n Even sum is : $esum \t Oddsum is : $osum \n\n"

Page 151: Shell Scripting

151

$sh esumosum

Enter size of an array: 5

Enter elements of an array :

9

19

11

14

15

Contents of an array is : 9 19 11 14 15

Even sum is : 14 Oddsum is : 54

4. Program to find large element in an array clear echo -e "Enter size of an array: \c" read size echo -e "Enter elements of an array: " i=0 while test $i -lt $size do read a[i] i=`expr $i + 1` done echo -e "\n Contents of an array is : ${a[@]} \n\n" i=1 lar=${a[0]} while test $i -lt $size do if test $lar -lt ${a[i]} then lar=${a[i]} fi i=`expr $i + 1` done echo -e "\n Largest element of an array is : $lar"

Page 152: Shell Scripting

152

$sh lar.sh

Enter size of an array:5

Enter elements of an array:

9

19

11

14

15

Contents of an array is : 9 19 11 14 15

Largest element of an array is : 19

5. Program to search an element using Linier search echo -e " Enter size of an array: \c" read size echo -e " Enter elements of an array: " i=0 while test $i -lt $size do read a[i] i=`expr $i + 1` done echo -e "\n Contents of an array is : ${a[@]} \n\n" echo -e "Enter element to be search :\c" read key i=0 while test $i -lt $size do if test $key -eq ${a[i]} then echo $key is Found exit fi i=`expr $i + 1` done echo $key is Not Found $sh lsearch.sh

Enter size of an array: 5

Enter elements of an array:

Page 153: Shell Scripting

153

14

11

15

9

19

Contents of an array is : 14 11 15 9 19

Enter element to be search :15

15 is Found

$sh lsearch.sh

Enter size of an array: 3

Enter elements of an array:

11

15

14

Contents of an array is : 11 15 14

Enter element to be search :19

19 is Not Found

6. write a shell script to arrange the elements in ascending order using - Bubble sort clear echo -e " Enter size of an array: \c" read size i=0 echo -e "\n Enter elements of an array " while test $i -lt $size do read arr[i] i=`expr $i + 1` done echo -e "\n Initial Contents of an array is : ${arr[@]} \n\n" j=0 while [ $j -lt $size ] do

Page 154: Shell Scripting

154

i=0 while [ $i -lt $size ] do if [ ${arr[$i]} -gt ${arr[$j]} ] then temp=${arr[$i]} arr[$i]=${arr[$j]} arr[$j]=$temp fi i=`expr $i + 1` done j=`expr $j + 1` done echo -e "\n Sorted array is : ${arr[@]} \n\n" $sh bubble.sh

Enter size of an array : 5

Enter elements of an array

14

11

15

9

19

Initial Contents of an array is : 14 11 15 9 19

Sorted array is : 9 11 14 15 19

7. Alternate way - write a shell script to arrange the elements in ascending order using - Bubble sort declare -a arr if [ $# = 0 ] then echo "Enter the elements seperated by space" read -a arr else arr=( $@ ) fi # get number of elements arr_size=${#arr[@]}

Page 155: Shell Scripting

155

j=0 while [ $j -lt $arr_size ] do i=0 while [ $i -lt $arr_size ] do if [ ${arr[$i]} -gt ${arr[$j]} ] then temp=${arr[$i]} arr[$i]=${arr[$j]} arr[$j]=$temp fi i=`expr $i + 1` done j=`expr $j + 1` done echo -e "Sorted Array is : ${arr[@]}" echo -e "\n\n" $sh bubble.sh

Enter the elements seperated by space

14 11 15 19 9

Sorted Array is : 9 11 14 15 19

8. Write a shell script to search an element in an array containing Elements in an random order using binary search # Bubble sort clear echo -e " Enter size of an array : \c" read size i=0 echo -e "\n Enter elements of an array " while test $i -lt $size do read arr[i] i=`expr $i + 1` done echo -e "\n Initial Contents of an array is : ${arr[@]} \n\n" j=0

Page 156: Shell Scripting

156

while [ $j -lt $size ] do i=0 while [ $i -lt $size ] do if [ ${arr[$i]} -gt ${arr[$j]} ] then temp=${arr[$i]} arr[$i]=${arr[$j]} arr[$j]=$temp fi i=`expr $i + 1` done j=`expr $j + 1` done echo -e "\n Sorted array is : ${arr[@]} \n\n" # search an element using - Binary search echo -e "Enter element to be search :\c" read key low=0 high=`expr $size - 1` i=0 while test $low -le $high do mid=`expr $low + $high` mid=`expr $mid / 2` if test $key -eq ${arr[$mid]} then echo $key is Found exit fi if test $key -lt ${arr[$mid]} then high=`expr $mid - 1` else low=`expr $mid + 1` fi done

Page 157: Shell Scripting

157

echo $key is Not Found $sh bsearch.sh

Enter size of an array : 5

Enter elements of an array

14

11

15

19

9

Initial Contents of an array is : 14 11 15 19 9

Sorted array is : 9 11 14 15 19

Enter element to be search :11

11 is Found

$sh bsearch.sh

Enter size of an array : 5

Enter elements of an array

19

11

15

14

9

Initial Contents of an array is : 19 11 15 14 9

Sorted array is : 9 11 14 15 19

Enter element to be search :2258

2258 is Not Found

Page 158: Shell Scripting

158

Chapter 10. Advanced filter The AWK 10.1 Introduction There are three variations of AWK: AWK - The original from AT&T NAWK - A newer, improved version from AT&T GAWK - The Free Software foundation's version

It is an excellent filter and report writer. Many UNIX utilities generate rows and columns of information. AWK is an excellent tool for processing these rows and columns, and is easier to use AWK than most conventional programming languages. It can be considered to be a pseudo-C interpreter, as it understands the same arithmetic operators as C. AWK also has string manipulation functions, so it can search for particular strings and modify the output. AWK also has associative arrays, which are incredible useful, and is a feature most computing languages lack. Associative arrays can make a complex problem a trivial exercise.

Many UNIX utilities have strange names. AWK is one of those utilities. It is not an abbreviation for awkward. In fact, it is an elegant and simple language. The work "AWK" is derived from the initials of the language's three developers: A. Aho, B. W. Kernighan and P. Weinberger. 10.2 Basic Structure

The essential organization of an AWK program follows the form: Syntactically, a rule consists of a pattern followed by an action. The action is enclosed in curly braces to separate it from the pattern. New lines usually separate rules. Therefore, an awk program looks like this: pattern { action } pattern { action } ...

The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input. If the condition is true, then the action is taken. The default pattern is something that matches every line. This is the blank or null pattern. Two other important patterns are specified by the keywords "BEGIN" and "END." As you might expect, these two words specify actions to be taken before any lines are read, and after the last line is read.

AWK program below: along with BEGIN and END sections

• A complete AWK can have three sections: BEGIN, awk body and End sections

• BEGIN section is executed only once and before execution of body

Page 159: Shell Scripting

159

• Awk body comprises of expressions and action and will be executed for number of records present in file parameter

• END section is executed after the execution of awk body BEGIN{ print “Wel-come to AWK”} { expression and awk } END { print “End of AWK”} 10.3 How to Run awk Programs

There are several ways to run an awk program. If the program is short, it is easiest to include it in the command that runs awk, like this: $awk 'program' input-file1 input-file2 ...

When the program is long, it is usually more convenient to put it in a file and run it with a command like this: $awk -f program-file input-file1 input-file2 ...

The -f instructs the awk utility to get the awk program from the file source-file. Any file name can be used for source-file. The third form is to store the commands in a file, and execute $awk -f filename 10.4 Comments in awk Programs

A comment is some text that is included in a program for the sake of human readers; it is not really an executable part of the program. Comments can explain what the program does and how it works. Nearly all programming languages have provisions for comments, as programs are typically hard to understand without them.

In the awk language, a comment starts with the sharp sign character (`#') and continues to the end of the line. The `#' does not have to be the first character on the line. The awk language ignores the rest of a line following a sharp sign. For example, we could have put the following into advice: # This program prints a nice friendly message. It helps keep novice users from being afraid of the computer. BEGIN { print "Don't Panic!" }

Page 160: Shell Scripting

160

10.5 AWK operators

The awk can handle numbers, both integer and float type, and all the relational tests can be handled by awk.

• Arithmetic operators : +, -,*,/,%,++,--

• Assignment operators: =,+=,-=, *=,/=

• Relational operators : <,<=,>,>=,==,!=

• String comparison : ~, !~

• Logical operators : &&, ||,! The statement: x=1+2*3 4; is the same as: x = (1 + (2 * 3)) "4"; Both print out "74." 10.6 Fields

• Each record is separated into fields named $1, $2, etc

• $0 is the entire record

• NF contains the number of fields in the current line

• FS contains the field separator RE; it defaults to the white space RE, /[<SPACE><TAB>]*/

• Fields may be accessed either by $n or by $var where var contains a value between 0 and NF.

10.7 Variables

Need not be declared, May contain any type of data, their data type may change over the life of the program. Are named as any token beginning with a letter and continuing with letters, digits and underscores .As in C, case matters; since all the built-in variables are all uppercase, avoid this form. 10.8 Built in variable

NR : Cumulative number of records read / Line number FS : Input field separator OFS : Output field separator NF : Number of fields in current record RS : The Record Separator Variable FILENAME: Current input file

10.9 print / printf - formatting output

print prints each of the values of $1 through $NF separated by OFS then prints a \n onto stdout; the default value of OFS is a blank .

Page 161: Shell Scripting

161

print value value ... prints the value(s) in order and then puts out a \n onto stdout;

The printf is very similar to the C function with the same name. C programmers

should have no problem using printf function. printf has one of these syntactical forms:

printf ( format);

printf ( format, arguments...);

printf ( format) >expression;

printf ( format, arguments...) > expression;

The parenthesis and semicolon are optional. The first argument to the printf

function is the format. This is a string, or variable whose value is a string. This string, like all strings, can contain special escape sequences to print control characters. 10.9.1 Escape Sequences The character "" is used to "escape" or mark special characters. The list of these characters is in table below:

\b Backspace \f Formfeed \n Newline \r Carriage Return \t Horizontal tab

10.9.2 Format Specifiers The power of the printf statement lies in the format specifiers, which always start with the character "%." The format specifiers are described in table 6: %c ASCII Character %d Decimal integer %e Floating Point number (engineering format) %f Floating Point number (fixed point format) %g The shorter of e or f, with trailing zeros removed %o Octal %s String %x Hexadecimal %% Literal %

Operators - awk has many of the same operators as C, excepting the bit operators. It also adds some text processing operators.

10.10 Explicit File output

Page 162: Shell Scripting

162

Instead of sending output to standard output, you can send output to a named file. The format is printf("stringn") > "/tmp/file"; You can append to an existing file, by using ">>:" printf("stringn") >> "/tmp/file"; 10.11 OFS - The Output Field Separator Variable There is an important difference between: print $2 $3 print $2,$3

The first example prints out one field, and the second prints out two fields. In the first case, the two positional parameters are concatenated together and output without a space.

In the second case, AWK prints two fields, and places the output field separator between them. Normally this is a space, but you can change this by modifying the variable "OFS." 10.12 Examples: Let emp.dat is a file containing employees records in the format empno,ename, edept, and basicsal. The fields are separated by delimiter ‘|’ as shown below.

$cat emp.dat

123|kishan|cse|6000

345|chandu|mech|5000 134|sindu|ece|7000 216|deepu|ece|2300 567|yogish|ise|5670 Following examples illustrates AWK commands to be executed on this file

• $awk '/kishan/{print }' emp.dat 123|kishan|cse|6000

• $ awk '/kishan/' emp.dat 123|kishan|cse|6000

• $awk '/kishan/{print $0 }' emp.dat 123|kishan|cse|6000

• $awk –F “|” ‘/kishan/ {print }’ emp.dat

Page 163: Shell Scripting

163

123|kishan|cse|6000

• $ awk -F"|" '/kishan/||/chandu/ {print $0}' emp.dat 123|kishan|cse|6000 345|chandu|mech|5000

• $ awk -F "|" '/kishan/ {print $1, $2 ,$4}' emp.dat 123 kishan 6000

• $ awk -F "|" '/kishan/ {print $1$2$4}' emp.dat 123kishan6000

• $ awk -F"|" '$2 == "kishan" || $2=="chandu" {print $1,$2,$4}' emp.dat 123 kishan 6000 345 chandu 5000

• $awk -F"|" '$2 == "kishan" || $2=="chandu" {print $1$2$4}' emp.dat o 123kishan6000 o 345chandu5000

• $awk -F"|" 'NR==2, NR==4 {print NR,$1,$2,$4 }' emp.dat 2 123 kishan 6000 3 345 chandu 5000 4 134 sindu 7000

• $awk -F"|" '/kishan/{printf "%3d %-20s %d \n", NR, $2 ,$4}' emp.dat 3 kishan 6000

• $awk -F"|" '/kishan/||/chandu/{printf "%3d %-20s %d \n", NR, $2 ,$4}' emp.dat

2 kishan 6000 3 chandu 5000

• $awk -F"|" '$4 > 5000 {printf "\n%3d %-20s %d\n", NR, $2 ,$4}' emp.dat

2 kishan 6000 4 sindu 7000 6 yogish 5670

• Print the total number of bytes used by files

$ls -l emp.dat | awk '{ x += $5 } END { print "total bytes: " x }'

total bytes: 83

• Print the total number of kilobytes used by files

$ls -l files | awk '{ x += $5 }

Page 164: Shell Scripting

164

END { print "total K-bytes: " (x + 1023)/1024 }'

total K-bytes: 1.08008

• Print a sorted list of employee names in emp.dat

$awk –F”|” '{ print $2 }' emp.dat | sort

chandu

deepu

kishan

sindu

yogish

• Count the lines in a file

$awk 'END { print NR }' emp.dat

5

• Print the even-numbered lines in the data file

$awk 'NR % 2 == 0' emp.dat

345|chandu|mech|5000

216|deepu|ece|2300

If you use the expression `NR % 2 == 1' instead, the program would print the odd-numbered lines.

$awk 'NR % 2 == 1' emp.dat

123|kishan|cse|6000

134|sindu|ece|7000

567|yogish|ise|5670

10.13 User Defined variables

• Awk allow us to create variables in the command line itself

• $awk –F”|” ‘$3 == “kishan” && $4 >400 { count = count+1 printf “%3d %-20s %d “, NR, $2 ,$4}’ emp.dat

# awk program illustrate use of BEGIN and END block to find sum of salaries # greater than 4000 and display corresponding employee id , dept and sal. #empsal.awk BEGIN { FS ="|" print "\n\n\t Emp data base ...\n\n "

Page 165: Shell Scripting

165

}$4 > 4000 { sum+=$4 printf "\n\t%d %s = %d \n", $1, $3,$4 } END { printf "\tSum of salaries greater than 4000 = %d \n\n", sum }

$awk –f empsal.awk emp.dat

Emp data base ...

123 cse = 5000

345 mech = 6000

1345 ece = 6000

Sum of salaries greater than 4000 = 17000

10.13.1 Parameter Passing

• One can pass parameters to AWK from command line

• The parameters passed from command line will be collected in ARGV array

• The count of parameters will be assigned to ARGC

• ARGV[] and ARGC can be used in BEGIN section

• The positional parameters have to be placed in single quotes. This would enable awk to distinguish between a positional parameter and a field identifier.

Example : $5 >’$1’

• Checks fifth field with the first positional parameter

• Parameters can be named parameters

$awk –F “|” –f sample.awk “ename=karun” emp.dat

Inside the sample.awk, one can refer the parameter value by name “ename” # awk Program to take input values through command line and to # check the record in emp.dat data base #emp1.awk { FS="|" if (ename==$2)

Page 166: Shell Scripting

166

{ printf "\n\n\t Employee id : %d \t\t Name : %s\n\n",$1,$2 printf "\n\t value of x entered at command line is :%d\n\n",x } } $awk -f emp1.awk ename="karun" x="1234" emp.dat

Employee id : 123 Name : karun

value of x entered at command line is :1234

# Awk Program to Genarate Pay-Slip of an Employee through shell script

emp.sh clear echo -e “Enter employee name : \c ” read name count=`grep -c $name emp.dat` if [ $count -gt 0 ] then awk -f employee.awk -v ename=$name -v M=`date +%h` -v Y=`date +%Y` emp.dat else echo emp not found fi employee.awk # Awk Program to Generate Pay-Slip of an Employee # employee.awk { FS="|" if (ename==$2) { h=$4*0.13 da=$4*0.74 pf=1200 total=150+982+pf g=$4+h+da n=g-total printf "\n\n\t\t\t KARUN INFO-TECH, HULLUKERE-MANDYA" printf "\n\n\t\t\t SALARY SLIP OF MONTH - %s %s ",M,Y printf "\n\t\t---------------------------------------------------\n" printf "\n\t\t Employee id : %d \t\t Name : %s",$1,$2 printf "\n\n\t\t BASIC : %10.2f",$4 printf "\n\t\t HRA : %10.2f",h printf "\n\t\t DA : %10.2f",da printf "\n\t\t PF : %10.2f",pf

Page 167: Shell Scripting

167

printf "\n\t\t GROSS : %10.2f \t NET : Rs. %8.2f",g,n printf "\n\t\t---------------------------------------------------\n" } } $sh emp.sh

Enter employee name: kishan

KARUN INFO-TECH, HULLUKERE-MANDYA

SALARY SLIP OF MONTH - Jul 2007

---------------------------------------------------

Employee id : 123 Name : kishan

BASIC : 6000.00

HRA : 780.00

DA : 4440.00

PF : 1200.00

GROSS : 11220.00 NET : Rs. 8888.00

---------------------------------------------------

10.14 Built in functions

Awk as several built-in functions for arithmetic and string operation int(x) returns integer value of x sqrt(x) returns square root of x length returns length of complete record length(x) returns length of x substr(s1,m,n) returns portion of string s1 of length n, starting from position m in str s1. index(s1,s2) the first location of s2 within s1; 0 if not found

system executes a Unix command, system(“clear”) to clear the screen Note: double quotes around the UNIX command.

split(str, arr, fs) used to split strings into fields, 1. str is the input string you

want split 2. arr is the name of the array

that will contain the results 3. fs is the field separator used

for splitting, FS is the default if fs is not given

10.15 getline Function

How do you get input into your awk script other than on the command line?. The getline function provides input capabilities. getline is used to read input from either the current input or from a file or pipe. getline returns 1 if a record was present, 0 if an

Page 168: Shell Scripting

168

end-of-file was encountered, and –1 if some error occurred.

Expression Sets getline $0, NF, NR, FNR getline var var, NR, FNR getline <"file" $0, NF getline var <"file" var "cmd" |getline $0, NF "cmd"|getline var var

10.16 Regular Expression

Awk uses the same regular expressions we’ve been using pattern matching meta-characters:

^ : Beginning of field $ : End of field . : Any character ? : Zero or one occurrence of preceding character

* : Zero or more occurrences of preceding characters or group + : One or more occurrences of preceding characters or group

[abcd] : Character class – Matches a or b or c or d. [^abcd]: Negated character class - Matches other than a or b or c or d. [a-z] : Range of characters - Matches a or b or c or d -----or z.

(exp) (exp1|exp2): Matches exp1 or exp2 $awk –F”|” ‘$2 ~/^kishan/ {print $2}’ emp.dat

123|kishan|cse|6000

$awk –F”|” ‘$2 !~/kishan$/ {print $2}’ emp.dat

345|chandu|mech|5000

134|sindu|ece|7000

216|deepu|ece|2300

567|yogish|ise|5670

10.17 if – else if (expression is true or non-zero){

statement1 } else {

statement2 }

Page 169: Shell Scripting

169

Where statement1 and/or statement2 can be multiple statements enclosed in curly braces { }s the else and associated statement2 are optional. if ($5 > 2000) interest=0.10*$5 else interest=0.05*$5 1. Print the length of the longest input line: $awk '{ if (length($0) > max) max = length($0) }

END { print max }' emp.dat

20

2. Print every line that is longer than 15 characters: $awk 'length($0) > 15' emp.dat

123|kishan|cse|6000

345|chandu|mech|5000

567|yogish|ise|5670

The sole rule has a relational expression as its pattern and it has no action—so the

default action, printing the record, is used. 3. Print the length of the longest line in data: $expand emp.dat | awk '{ if (x < length()) x = length() }

END { print "maximum line length is " x }'

maximum line length is 20

The input is processed by the expand utility to change tabs into spaces, so the

widths compared are actually the right-margin columns. 4. Print every line that has at least one field: $cat emp.dat

123|kishan|cse|6000

345|chandu|mech|5000

134|sindu|ece|7000

216|deepu|ece|2300

567|yogish|ise|5670

Page 170: Shell Scripting

170

$awk 'NF > 0' emp.dat

123|kishan|cse|6000

345|chandu|mech|5000

134|sindu|ece|7000

216|deepu|ece|2300

567|yogish|ise|5670

his is an easy way to delete blank lines from a file (or rather, to create a new file similar to the old file but from which the blank lines have been removed).

• Print seven random numbers from 0 to 100, inclusive:

$awk 'BEGIN { for (i = 1; i <= 7; i++)

print int(101 * rand()) }'

24

29

85

15

59

19

81

10.18 while loop and for loops while loop : while (condition) { statements

} For loop:

for( initialization; condition; increment/decrement) {

statements }

for(;;) is an infinite loop

Page 171: Shell Scripting

171

Awk is useful for performing simple iterative computations for which a more sophisticated language like C might prove overkill. Consider the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 ...

Each element in the sequence is constructed by adding the two previous elements together, with the first two elements defined as both "1". It's a discrete formula for exponential growth. It is very easy to use Awk to generate this sequence:

$awk 'BEGIN {a=1;b=1; while(++x<=10){print a;

t=a;a=a+b;b=t}; exit}'

This generates the following output data:

1

2

3

5

8

13

21

34

55

89

Write awk program to find square of all the numbers from 0 to 10 #sq.awk BEGIN { # Print the squares from 1 to 10 the first way printf “Using While Loop”; i=1; while (i <= 10) { printf "The square of %d is %d", i, i*i; i = i+1; } # do it again, using more concise code printf “Using For Loop”; for (i=1; i <= 10; i++) { printf "The square of %5d is %d ", i, i*i; } # now end exit; } # awk -f sq

Page 172: Shell Scripting

172

Using While Loop The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49 The square of 8 is 64 The square of 9 is 81 The square of 10 is 100 Using For Loop The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49 The square of 8 is 64 The square of 9 is 81 The square of 10 is 100 The following asks for a number, and then squares it: #sq2.awk BEGIN { print "Type a number"; } { print "The square of ", $1, " is ", $1*$1; print "Type another number .. else ^d to exit"; } END { print "Done"

}

$awk -f sq2 Type a number 6 The square of 6 is 36 Type another number .. else ^d to exitDone 8 The square of 8 is 64 Type another number .. else ^d to exit Done

10.19 Arrays

Page 173: Shell Scripting

173

An array is a grouping of elements (data values) that usually are related in some way. Arrays are usually implemented as contiguous locations in memory. This is true whether it is a single or multi-dimension array. Awk provides one-dimensional arrays for storing strings or numbers.

Like other variables in awk, arrays and their elements do NOT have to be declared before use. Simply using an array element causes it to be created and initialized (either 0 or "").

Awk arrays are different from the arrays in most languages - instead of using contiguous numeric subscripts to reference the elements, awk uses strings for subscripts. Because of this, awk arrays are known as associative arrays.

If we wish to find the sum of three separate fields, we can write: {sum[1] += $1; sum[2]+=$2; sum[3] += $3} END {print sum[1],sum[2],sum[3]}

This creates an array of three elements: sum[1], sum[2] and sum[3].

10.19.1 Subscripts

In awk, any expression can be used in an array subscript.

count[$3] uses the value of the third field of the current line as an array subscript. However, you have to be careful because of this what the difference is between:

balloons["red"] and balloons[red] ?

10.19.2 Deleting Array Elements

Elements can be deleted from an array via the delete(array[subscript]) statement.

For example: Following example takes list of lines each line begins with line number in some random order. But arrange the lines in ascending order. #arr1.awk { if ($1 > max) max=$1; arr[$1]=$0; } END{ for (x=1; x<=max ;x++)

Page 174: Shell Scripting

174

print arr[x]; } $cat inp.dat 5 am the fifth person 3 am the third person 1 am the first person 4 am the fourth person 2 am the second person $awk -f arr1.awk inp.dat 1 am the first person 2 am the second person 3 am the third person 4 am the fourth person 5 am the fifth person

10.20 User Defined Functions Writing your own functions, a function begins with a function header of the form: function name(argument(s), localvar(s)) {

and ends with the matching }

• The value of the function is returned via a statement of the form:

return value

• Functions do not have to return a value and the value returned by a function (either built-in or written locally) may be ignored by just placing the function with its arguments as a whole, separate statement

• The local variables indicated in the localvars of the heading replace the global variables of the same name until the function completes, at which time the globals are restored.

Calling Functions

A function can be used in any expression in a pattern-action pair or the body of another function definition. When calling a function, there cannot be any spaces between the function name and the left parenthesis enclosing the function arguments.

When a function is called with normal variable arguments, they are "passed by value". The function is given a "copy" of the variable and therefore cannot actually modify the variable. However, arrays are "passed by reference" (not copied) and so a function can modify elements of an array or even create new ones.

Page 175: Shell Scripting

175

# awk Function with out Argument and Return value #fun1.awk function disp(){ printf "\t This is user defined function...\n\n" } { print "\t This is main...\n\n" disp() exit }

$awk –f fun1.awk

This is main...

This is user defined function...

# Awk with User defined Function with Arguments #fun2.awk function disp(a,b,c){ printf "\n\tThis is User defined function with arguments..." printf "\n\t a=%d\tb=%d and c=%d\n\n",a,b,c } { print "\n\t This is main...\n\n" disp(2,3,4) exit } $awk –f fun2.awk

This is main...

This is User defined function with arguments...

a=2 b=3 and c=4

# Awk with User defined Function with Arguments and Return value # awk program to find factorial of a given number # fun3.awk

Page 176: Shell Scripting

176

function disp(n){ if (n<=1) return 1 else return n*disp(n-1) } { printf "\n\tEnter Value : " getline x printf "\n\tFactorial of a given number %d is : %d \n\n",x,disp(x) exit }

$awk –f fun2.awk

Enter Value : 5

Factorial of a given number 5 is : 120

Write awk function to find largest and smallest of two numbers { print larger($1,$2), “ >”, smaller($1,$2) } function larger(m,n) {

return m>n ?m:n } function smaller(m,n) {

return m<n ?m:n } This function demonstrates a way to print error messages, including the filename and line number, if appropriate: { if (NF != 4) { error("Expected 4 fields"); } else { print; } } function error ( message ) {

Page 177: Shell Scripting

177

if (FILENAME != "-") { printf("%s: ", FILENAME) > "/dev/tty"; } printf("\nline # %d, %s, line: %sn", NR, message, $0) >> "/dev/tty"; } $ awk -F"|" -f n1.awk emp.dat

123|kishan|cse|6000

345|chandu|mech|5000

134|sindu|ece|7000

216|deepu|ece|2300

567|yogish|ise|5670

$awk -f n1.awk emp.dat

emp.dat: line # 1, Expected 4 fields, line: 123|kishan|cse|6000

emp.dat: line #2, Expected 4 fields, line: 345|chandu|mech|5000

emp.dat: line # 3, Expected 4 fields, line: 134|sindu|ece|7000

emp.dat: line # 4, Expected 4 fields, line: 216|deepu|ece|2300

emp.dat: line # 5, Expected 4 fields, line: 567|yogish|ise|5670