You are on page 1of 177

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. User 1 User 2 User 3
User N

Compiler Assembler Text Editor base System and Application Programs Operating System Computer Hardware

Data system

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

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, always keep the following in mind UNIX is case 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 U. Cal. Berkley Sun IBM HP 2 Proprietary Name 1 BSD Solaris AIX HP-UX

SGI IRIX 2 Digital Equipment 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 DOS Single User Poor security Batch Programming Limited File Manipulation Utilities Text manipulation is difficult No Message facility Commands are not case sensitive Root is \

UNIX Multi User Strong Security Shell and AWK programming Strong File Manipulation Utilities Strong Text manipulation facility(Filters) Message facility Commands are case sensitive Root is / 1.6 UNIX system Architecture

The UNIX system has four major components:

Figure: Model of a UNIX system 3

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. 2. 3. 4. Manages computer memory Maintains the file system Allocates the computer resources among users 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.

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.

Example: list all the file name begins with a name a a ax a1 a2 a3 aa ab.abcMatches 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 computers 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

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:

$ Which would serve to indicate a users 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

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 1.12 cal : The calendar clear : Clears the terminal screen.

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 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 # displays calendar for the Month of August 1947

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.

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 +%h +%m +%d +%y +%Y +%H +%M +%S +%D $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 abbreviated weekday name(MonSun) month Name (Jan Dec) month Number (112) day of month (131) last two digits of year (00...99) year (four digits) hours minutes seconds date (mm/dd/yy)

This command is used to change the password. Each password should satisfy following rules.

10

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 $tput init clear #initializes the terminal #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, &dont 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.

11

$lock 1.17

-45

#Locks it for 45 minutes

bc : The calculator

bc is actually a preprocessor for the desk calculator program. UNIX provides two calculators: 1. 2. a graphical object (The xcalc command) xcalc: is available in the Xwindow system and quite easy to use. 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 17 ^d $ # Value displayed after computation

You can also ask bc to perform the calculations together. $bc 12*2 ; 2^4 24 16 9/5 1 # ^indicates to the power of

#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 17/7 2.42 # Truncate to 2 decimal places # 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:

12

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

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 Hello how are you
^d

# Only a command
# Reading data from keyboard #Terminating input from keyboard press ^d # Number of lines, words and characters

1 4 18 $wc -l Hello how are you ^d 1 $wc -l -w Hello how are you ^d 1 4 $wc file 4 14 124 $wc 4 -l

# command with an option

# Only number of lines # Command with an options $ wc -lw

# number of lines and words # command with an argument (file name)

file # command with an argument and an option

file

$wc -l filea fileb 2 filea 3 fileb 5 Total $wc -lw 2 5 filea filea

# command with an option and arguments

# command with an options and an argument

14

$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. 2. 3. 4. Used for creating a small file Display the contents of file Used for appending data 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

15

$ $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 b 12f 10b /pat q Move forward one page Move backward one page Scrolling 12 pages forward Scrolling 10 pages backward Searching for a pattern pat Used to exit more 16

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

17

$date Tue Jun $who root chandu root root root root

5 18:08:11 IST 2007

tty1 tty2 pts/0 :1 pts/1 pts/2

Jun Jun Jun Jun Jun Jun

5 5 5 5 5 5

18:01 16:16 18:03 16:30 (localhost) 16:30 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.

18

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

/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 -e do not output the trailing new line enable interpretation of the backslash-escaped characters listed below Escape Sequence Meaning \\ \a \b \c \f \n Backslash Alert (BEL) Backspace Suppress trailing new line Form feed New line

20

\r \t \v \033[0m \033[1m \033[4m \033[5m \033[7m

Carriage return Horizontal tab Vertical tab Normal characters Bold characters Underlined characters Blinking characters 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 hello how are you how are you

Prints the sentence hello how are you on the terminal but removes number of blanks by single blank. $echo "hello hello $ echo 'hello hello how how how how are you are you" are you 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 21

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. 2.1 The Editors

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: 2.2 Create a file Open an existing file Copy & Pas te text Search for text Handle a large amount of data 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). 22

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:

Input/Insert Mode

Ex-Mode

ESC a,A,i,I,o,O r,R,s,S Command Mode $vi filename Shell

ESC , w

q, q!, wq, x

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

23

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

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

24

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 Function Inserts text to left of cursor (Existing text shifted right) Inserts text at beginning of line (Existing text shifted right) Inserts text to right of cursor (Existing text shifted right) Inserts text at end of line Opens line below Opens line Above Replaces single character under cursor with ch(No Esc is required) Replaces text from cursor to right (Existing text overwritten) Replaces single character under cursor by any number of characters Replaces entire line

Command i I a A o O rch R s S 2.4

Exit Mode Commands Action Saves file and remain in editing mode Saves file and quits editing mode Write and quit editing mode(same as x) Write to file (like save as in windows) Same as above but overwrites existing file Quits editing mode when no changes are made to file Quits editing mode but only after abandoning the changes Writes lines n1 to n2 to file Writes current line to file Writes last line to file Escapes to UNIX shell Place a cursor at line 13 Reads file ab.c below current line Reads first three lines of ab.c below current line Appends buffer contents to a file xy.c Stops editing current file and edits file xy.c As above but after abandoning the changes made to current file

Command :w :x :wq :w file :w! file :q :q! :n1 ,n2w file :.w file :$w file :sh :13 :r ab.c :r !head -3 ab.c :w >> xy.c :e xy.c :e! xy.c

25

:n :rew 2.5

Edits next file(if vi invoked with more than one filename) Rewinds file list to start editing from first file specified in command line 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

26

n or N !!tr [a-z] [A-Z] 2.6

Repeating the last pattern search. n repeats search in same direction of original search N - repeats search in opposite direction of original search Converts current line to uppercase

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 youll delete all instances of the source pattern in all lines matched by the address: :1,50s/UNIX//g :.s/UNIX/Linux :$s/UNIX/Linux/g 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 doesnt. This sequence is repeated for each of the matched lines. Deletes UNIX everywhere in lines 1 to 50 Substitute only the current line Substitute only the last line

Interactive substitution: by adding c (confirmatory) parameter as the flag at the

27

Chapter 3. 3.1

The UNIX File System

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 date cat who dsk

/dev

/etc

/home bindu

/temp

/usr include lib

rdsk passwd shadow chandu Reva cse xyz.c ise

ab.c

Figure: The UNIX File System

28

Figure: Example of a UNIX files system Notice that the root directory ( / )containing several important system directories. / /bin /dev /etc /etc/group /etc/passwd /etc/shadow /home /mnt root /sbin /tmp /usr/include /usr/lib /stand 3.2 : : : : : : : : : : : : : : : Root directory Short for binaries, this is the directory where many commonly used like cat, date, who, cp, etc. Stores all the character and device files. Stores system administrative commands and files. Stores all group information. Stores all users information. Stores user passwords. contains user directories and files contains device files related to mounted devices the root users' home directory (note this is different than /) system binary files reside here. If there is no sbin directory on your system, these files most likely reside in etc. storage for temporary files which are periodically removed from the file system Stores standard header files. Contains standard libraries. Contains bootable programs and data files used in the booting process.

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

29

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 /a/b a /a/./b ./a ../a 3.4 The file named a, in the root directory. The file named b, in the directory named a in the root directory. The file named a, in the current working directory. This is the same as /a/b. The file named a, in the current working directory. The file named a, in the parent directory of the current working directory. 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.

30

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 ab.c $ ls : listing directory contents reva # In UNIX list files in a single column # this is same as $ls -x in Linux

Files are arranged in the ascending order numerals, uppercase finally lowercase. ls options: option -a -d dirname -F -i -l -R -t -u -x Meaning Shows all file names beginning with a dot ,including . Lists only dirname if dirname is a directory Marks executables with *, directories with / and symbolic links with @ Displays inode number Long listing Displays seven attributes of a file Recursive listing Sorts file name by last modification time Sorts file name by last access time 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

31

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 32

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

$mkdir ise ise/ise1 ise/ise3 $

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,

33

$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

34

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

35

$rm -i 3.13

* # removes all files with interactive

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 #renames directory name reva to CIT

$mv reva 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

36

$mv file(s)

directory_name #moves filea.c to CIT

$mv filea.c CIT

After mv now the contents of directory are: $ ls -R .: CIT ./CIT: cse filea.c ./CIT/cse: xy.c ./CIT/ise: $ cat >mn.c hello how are you? ^d $ ls -R .: CIT mn.c ./CIT: cse filea.c ./CIT/cse: xy.c ./CIT/ise: Moving and Renaming: $mv mn.c $ls -R .: CIT CIT/fileb.c #creating file under current directory

ise

# Now Contents of directory are:

ise

./CIT: cse filea.c ./CIT/cse:

fileb.c

ise

37

xy.c ./CIT/ise: 3.15 files wc Word Count , print the number of lines, words, and bytes(characters) in $wc option(s) file(s)

Syntax:

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 -l -w : : : Bytes : Print the number of characters in a file includes space, tab, and newline. Lines : Print the number of lines in a file 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. $

38

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 types files available in UNIX are: Regular files. Directory files. Device files. FIFO files. Link Files. 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. Example: All .exe files, C, C++, PDF Document files. Example: Folders in WINDOWS. Example: Floppy, CDROM, Printer. Example: PIPEs. Example: Shortcuts in WINDOWS.

39

^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) newdir

For example: $mkdir

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.

40

Syntax:

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

Example1: # mknod

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 2 Hard links Soft/symbolic links Hard Links

3.21.5.1

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

41

167 167

-rw-r--r--rw-r--r--

2 2

chandu chandu

chandu chandu

50 50

DEC 10 DEC 10

12:25 12:25

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

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 For example: $ln s xyz link_file_name

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 260 lrwxrwxrwx 1 chandu chandu chandu 50 DEC 10 1:25 abc chandu 3 DEC 10 1:25 abcl->abc

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. Limitations of Hard links xyz the link file references to this new version of the file. Users cant create hard links for directories unless they have super user privileges. Users cant 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

42

1.Do not create new inode. 2.Cant link directories unless super user privileges. 3.Cannot link file across file systems. 4.Increase 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. 3.23 find : Searching for files

1. Create a new inode. 2. Can link directories. 3. Can link files across file systems. 4. Does not change the hard link count. 5. Always reference to the latest version of the files to which they link.

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 $find / -type l -print # Locate All directories from the system # Locate All symbolic links from the system

-perm : Locating a file by permissions $find / -perm 766 -print 766 # Selects all files on the system are having the permissions

Finally, it takes some action on those selected files -print -ls -exec cmd : : : Prints selected file on standard output Executes ls -lids command on selected files Executes UNIX command cmd followed by {} \; Find all the files named aaa.txt in the current directory or Find all the files named 'vimrc' anywhere on the system.

$find . -name aaa.txt any subdirectory tree. $find / -name vimrc

43

$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. 4.1 The File Attributes

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.

$ls li * Total 45 Inode Permissions 125 rw-r-xr-150 d rw-r-xr-File Types 1 2

Owner Name chandu chandu

File Size In bytes chandu chandu Group Name 25 40 DEC 25 DEC 20

File Name 10:40 11:20 ab.c cse

Links

Last Modification Date & Time

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 120 140 200 File name . .. xyz.c

44

File type and permission: First character in the second column indicates file type b c d l p 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 1. Inode Number 2. File Type 3. Permissions 4. Hard Links 5. Owner Name 6. Group Name 7. File Size 8. Last Access Time 9.Last Modification Time 10.Last Change Time 11. File System Id Meaning The System inode number of the file. Type of File. The file access permission for Owner Group and Others. Number of hard links of a file. The file Owner or User Name. The group to which User or Owner Belongs to. The file size in bytes. The time, the file was last accessed. The time, the file was last modified. The time, the file Access Permission, Owner, Group or Hard link count was last changed. The file system id where the file is stored. : : : : : : Indicates Ordinary / hard link file Block device file Character device file Directory Symbolic Link file Fifo file ("named pipe") special file

In addition to the above attributes, the UNIX system also stores the Major and Minor numbers for each Device file.

45

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 chmod chown chgrp ln touch vi, emacs rm 4.2 Attributes Changed Changes access permission, last change time. Changes UID, Last Change time. Changes GID, Last change time. Increase hard link count. Changes last access time and modification time. Changes file size, last access time and last modification time. Decrease the hard link count by one. If the hard link count is zero the file will be permanently removed from the disk.

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 u User(owner) + g Group o Others = a All (ugo)

Operation Assigns Permissions r Removes Permissions w Assigns Absolute Permissions x

Permissions Read Permission Write Permission Execute Permission

46

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 6 5 4 3 2 1 (111) (110) (101) (100) (011) (010) (001) Read, Write and Execute Permission Read and Write Permission Read and execute Permission Read permission Write and Execute permission Write Permission Execute Permission

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

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

47

$ 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 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: #chown chandu #exit $ls -l f1 -r-xr----4.4 # Enter Password of Super user f1

4.3

1 chandu

kishan

49 Jul

7 21:07 f1

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

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-$chgrp 1 kishan f2 kishan 49 Jul 7 21:07 f2

chandu

$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. 5.1 The SHELL

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 Bourne Shell C-Shell TC-Shell Korn Shell Program Name sh csh tcsh ksh Description The original UNIX shell created by Steven Bourne, available on most UNIX systems A shell created by Bill Joy at Berkely, designed to provide some C language-like features A public domain available extension of csh which allows for command line editing and filename completion A shell created by David Korn from Bell Labs which provides features of csh and tcsh with the programming capability similar to sh 49

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 / shells 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 Metacharacters 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. & = ; \ \m Executes command in Background Assignment operator Command separator Continuation of command line Literal translation of meta-character m

50

' " # * ? $* $# $$ $? . []

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 * ? *.* *.com ?.com [xyz] [!xyz] name[xyz] name[a-z] name[a-z4] $ls ab* $ls ab? Meaning Matches z ero or any number of characters Matches single character All files that contain a period in the filename All files in the current directory that end in .com All files in the current directory that end in .com and have one character preceding the period A single character, either x,y or z A single character, that is not x,y or z All files in the current directory, namex, namey, or namez All files in the current directory namea through namez All files in the current directory, namea through namez and name4

list all files begins with ab, including 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.

51

5.4

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

Escaping: its Quoting: (like

Providing a \ (backslash) before the wild-card to remove (escape) special meaning. Enclosing the wild card, or even the entire pattern, within quotes ab*).

Example: $rm $cat $cat $rm ab\* #Removes a file ab* not ab followed by any characters even ab also. > ab\[a-z\]#creates a file with the name ab[a-z] ab\[a-z\] # Displays the contents of a file ab[a-z] ab\ xy #removes a file ab xy, a single file but containing space # hence escape space $echo e hello \\n #Escaping \ $echo \ $rm ab* $rm ab xy 5.5 #displays a \, use single or double quotes #removes file ab* #removes file ab xy 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

52

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 1 4 22 $wc file 1 4 22 file # 0 is a file handle of input device # takes input from file hence displays file name

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

53

^d 1 1 2

4 4 8

18 22 40

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

54

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.

55

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

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 command > file command >> file command < file command 2> file command 2>> file command > file 2>&1 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: Resulting Operation stdout written to file, overwriting if file exists stdout written to file, appending if file exists input read from file stderr written to file, overwriting if file exists stderr written to file, appending if file exists stdout written to file, stderr written to same file descriptor

57

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 kishan karun bindu pts/2 pts/12 pts/4 pts/6 Oct Oct Oct Oct 1 1 1 1 13:07 12:07 13:37 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.

58

Note: We can use any string as a terminator.

Chapter 6. 6.1

The Filters

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

59

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 -n -o N +page -l N Double space input Numbers lines Offset lines by N spaces, means increases left margin Starts Prints from a page Page length is set to N lines # Starts printing from page 5 # Page length is set to 50 lines

$pr +5 filename $pr -l 50 filename 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 filea # Prints using laser printer

$lp -d laser

-t title : Prints title on the first page. $lp -t First Page filea

60

-n N : $lp -n3

Prints N copies 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 $cancel laser 6.5 #Cancels job with request id prl-425 # Cancels current job on the laser printer

head : Displays the files contents from the beginning emp.dat #Displays first ten lines of a files

$head

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 # Displays 3 lines from the beginning

$head

-3 emp.dat

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

61

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 # Displays last 10 lines of a file by default

$tail emp.dat

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 #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 $tail +5 emp.dat 6.8 cut : Cutting a file vertically

62

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

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

63

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 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 emp.dat # Displays names and Date of joining

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

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

64

$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 "|" 1667|chethan 1767|vikky 1887|raghu 3167|yogish 4187|soumya 6.10 cutlist1 cutlist2 # Paste with delimiting character |

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

65

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 cse ece ise mech emp.dat|sort -u #Selects only dept names

-n : Used to sort numerically $cat numfile 12345 111 2258 453 7070 2500 1500 $sort numfile 111 12345 1500 2258 2500 453 7070 $sort -n numfile 111 453 1500 2258 2500

$sort -n numeric file #Contents of numfile

#Output of numfile without using -n option

#Output of numfile using -n option

66

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 -d -c 6.12 : : : Selects only non-repeated lines Selects only repeated lines Counting frequency of occurrence

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 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 # replaces | by @ and / by Y

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

67

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-rw-r--r-rw-r--r-rw-r--r-rw-r--r-rw-r--r-rwxr-xr-x 6.13

yogish a yogish a1.c root ab.doc yogish a.cpp yogish alarm1.cpp yogish alarm.cpp yogish a.out

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 68

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 -v -n -c -l -e -f -w Ignoring case while matching Doesnt display lines matching expression Displays lines containing the pattern along with line number Displays number of lines containing the pattern Displays list of files in the current directory containing the pattern Used to specifies multiple pattern in a single grep Takes pattern from file Matches a pattern string as a word -v cse emp.dat # Doesn't display lines matching expression

$grep

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

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 5 -c cse emp.dat #Displays number of lines containing the pattern # List all the filenames containing the pattern

$ grep -l cse emp.dat emp1.dat emp5.dat

*.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 mech ise >pat # File containing patterns to be search

$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

70

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. + ? exp1|exp2 (exp1|exp2)exp3 exp1(exp2|exp3) One or more occurrences of the previous character Zero or One occurrence of character Matches exp1 or exp2 Matches exp1exp3 or exp2exp3 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:

71

$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 $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. 5. $sed -n '$p' emp1.dat #Prints last line 4187|soumya|cse|5600|7/04/1985 $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

2.

72

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

9.

11.

73

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

14.

74

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

18.

19.

Chapter 7. 7.1

A PROCESS

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.

75

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.

76

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

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 30758 pts/0 30891 pts/0 TIME CMD 00:00:00 bash 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 30758 pts/0 31043 pts/0 STAT S R TIME 0:00 0:00 CMD -ksh 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)

78

R S T Z

runnable (on run queue) sleeping traced or stopped 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 root root root root root root root root root root root PID 1 2 3 4 5 6 7 8 9 13 88 PPID 0 1 1 0 0 0 0 0 1 1 1 C STIME TTY 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 Oct12 ? TIME 00:00:04 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:01 00:00:00 CMD init [keventd] [kapm-idled] [ksoftirqd_CPU0] [kswapd] [kreclaimd] [bdflush] [kupdated] [mdrecoveryd] [kjournald] [khubd]

root root ntp chandu chandu

193 1 649 1 749 1 30758 30757 30845 30758

0 Oct12 ? 0 Oct12 ? 0 Oct12 ? 0 15:21 pts/0 0 16:41 pts/0

00:00:00 00:00:00 00:00:00 00:00:00 00:00:00

[kjournald] syslogd -m 0 ntpd -U ntp -ksh 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 root 1 0 . . root 805 1 . . root 30757 805 chandu 30758 30757 chandu 30845 30758 C STIME TTY 0 Oct12 ? 0 Oct12 ? TIME CMD 00:00:04 init 00:00:01 /usr/sbin/sshd 00:00:00 /usr/sbin/sshd 00:00:00 -ksh 00:00:00 ps -eaf

0 15:21 ? 0 15:21 pts/0 0 15:29 pts/0

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

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 UID root root # -f option is used to display full Listing of process status PID PPID C STIME TTY TIME CMD 6508 6502 0 Jul07 pts/1 00:00:00 bash 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

80

1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 ? 10 ? 11 ? 31 ? 32 ? 43 ? 42 ? 44 ? 46 ? 45 ? 47 ? 630 ? 1692 ? 1693 ? 1695 ? and so on. 7.3

00:00:01 init 00:00:00 migration/0 00:00:00 ksoftirqd/0 00:00:00 migration/1 00:00:00 ksoftirqd/1 00:00:00 events/0 00:00:00 events/1 00:00:00 khelper 00:00:00 netlink/0 00:00:00 netlink/1 00:00:00 kacpid 00:00:00 kblockd/0 00:00:00 kblockd/1 00:00:00 pdflush 00:00:00 kirqd 00:00:00 pdflush 00:00:00 aio/0 00:00:00 kswapd0 00:00:00 aio/1 00:00:00 kseriod 00:00:00 ata/0 00:00:00 ata/1 00:00:00 scsi_eh_0

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 81

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

82

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 7.3.2.2 Internal Commands The shell has a number of built-in commands as well. Like cd echo dont generate a process, and are executed directly by the shell. Similarly variable assignment with the statement x=58, for instance doesnt 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

#ls is the command stored in directory /bin

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

83

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

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

84

$ls | wc [1] 1475 $

>out&

$ who | wc l > count & [2] 1490 $ [2] + Done [1] + Done just press RETURN or Just press ENTER KEY who | wc l > count & 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 & [1] 1719 $hello, world [1] $ stty + # Execute in background

# The output from the background job appears after the prompt, # when we press RETURN Done cat temp.c & #Disable ability of background jobs to output to controlling # Terminal try it again in the background

tostop

$ cat temp.c & [1] 1721 $ [1] + # When we press RETURN and find the job is stopped 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 85

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 chandu 5626 1 C STIME 99 10:34 TTY TIME tty05 00:01:03 COMMAND 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

86

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 SIGINT SIGQUIT SIGTRAP SIGKILL SIGTERM 1 2 3 5 9 15

Thus, you can terminate a process in the following way: kill pid kill -KILL pid kill -9 pid # Default signal of SIGTERM # use of SIGKILL signal # 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 2876 pts/0 2941 pts/0 2946 pts/0 STAT S T R TIME 0:00 0:00 0:00 COMMAND -bash vi foo 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

87

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 $fg %prg1.sh $bg %6 7.7.5 jobs: #Brings First job to foreground #Brings prg1.sh job to foreground #Sends sixth job to background 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 [2] - Running [3] - Running du / loop_program 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 Oclock $at 12:00 at>echo -e todays date is

88

at>date at>^d $ at is executing at 12 Oclock 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: -x: -j: Option shows the status of all the processes owned by others Shows process that does not have a controlling terminal Option displays the job related information:

89

The output from ps looks like: PPID 0 0 0 1 1 PID PGID 0 0 1 0 2 0 105 37 108 108 SID 0 0 0 37 108 TT ? ? ? ? ? TPGID -1 -1 -1 -1 -1 UID 0 0 0 0 0 COMMAND Swapper /sbin/init pagedaemon update 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.

90

Field Minute Hour Day Month Week day

Description Minute of the hour Hour of the day Day of the month Month of the year Day of the week

range 0 to 59 0 to 23 1 to 31 1 to 12 0 to 6(0 = Sunday)

Each of the time related fields may contain For Example: MIN (0-59) 0 30 HOUR (0-23) 18 2 DOM (1-31) * * MOY (1-12) * * DOW (0-6) * * COMMAND (note : 0-Sunday) date who 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

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

91

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 crontab l crontab -r : Checks out a copy of your crontab invokes u r editor on it, and then resubmit it to the crontab directory. : List the contents of your standard output. : 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. 9.1

Shell Programming

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.

92

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

93

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 root :0 root pts/0 root pts/1 this is the end of shell program $chmod +x spg1.sh $./spg1.sh /home/yogish

Jun Jun Jun Jun

6 6 6 6

22:35 21:57 (console) 21:57 22:32 (:0.0)

94

yogish root root root

tty1 :0 pts/0 pts/1

Jun Jun Jun Jun

6 6 6 6

22:35 21:57 (console) 21:57 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 readonly 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" 95

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

96

unset name readonly name

# name is now undefined # 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 : $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. 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. : This specifies what locale to use for string sorting. : This specifies what locale to use for character sets and character This is the name that the user used to log in.

LC_ALL :

LC_COLLATE LC_CTYPE

97

classification. LC_MONETAR : LC_NUMERIC LC_TIME : This specifies what locale to use for formatting monetary values. This specifies what locale to use for formatting numbers. : 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:

98

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

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|

100

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

101

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 102

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 25 $expr 10 - 8 2 $expr 10 '*' 4 40 $expr 137/ 5 27 # space is required in between operator

# Astrisk has to be escaped

#Decimal portion truncated

$expr 13%5 3 expr will also work with user defined variables as in the following Example: 103

$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 : '.' 8 # Space on either side of : is required

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

104

$let x=10 y=3 $let z=x+y $echo $z 13

#no space in between operator and $ sign not required

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

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 $echo $x chandu # x is now local to parent and assigned a value 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 chandu #value set inside the script doesn't affects value outside script

Because x is a local variable it is local to login shell, its value cant 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

106

chandu $sh export.sh the value of x is chandu Enter new value of x aish the New value of x is aish $echo $x chandu

# Value of x is now visible in sub shell

# value set inside the script doesn't affects value outside script

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

107

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 -ne -gt -lt -ge -le equal not equal greater than less than greater than or equal less than or equal

$x=2 ; y=3 $test $x -gt $echo $? 1

$y # 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 string1 != string2 -n string1 -z string1 $s1="aish" $test -n $s1 $echo $? 0 $test -z $s1 $echo $? 1 $s2="aish" $test $s1 = $s2 108 true if string1 and string 2 are equal. true if string1 and string2 are not equal. true if string1 is not a null string. true if string1 is a null string.

$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 -r file -w file -x file -d file -L file -s file
$ ls -l total 8 -rw-r--r-drwxr-xr-x -rwxr-xr-x

true if file exists and it is a plain file true if file exists and is readable true if file exists and is writable true if file exists and is executable true if file exists and it is a directory. true if file exists and it is symbolic link file true if file exits and it contains data(size greater than 0 bytes)
1 yogish users 264 2007-06-06 23:09 first2.sh 2 yogish users 48 2007-06-10 21:46 shelldir 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 $? 0 $test -w first2.sh $echo $? 0 $test -x first2.sh $echo $? 1 $test -f shelldir $echo $? 1

#first2.sh it is readable

#first2.sh it is writable

#first2.sh it is not executable

#it is exsting but it is a directory

Frequently used test operators are listed in the table below.

109

File Operators -d file -f file -r file -w file -x file -s file Integer Operators int1 -eq int2 int1 -ne int2 int1 -lt int2 int1 -le int2 int1 -gt int2 int1 -ge int2 String Operators string1 = string2 string1 != string2 -z string Logical Operators ! expr expr1 -o expr2 expr1 -a expr2 9.10.5 Logical Operators :

returns TRUE if file exists and is a directory file exists and is an ordinary file file exists and is readable file exists and is writable file exists and is executable file exists and its size is non-zero returns TRUE if int1 is equal to int2 int1 is not equal to int2 int1 is less than int2 int1 is less than or equal to int2 int1 is greater than int2 int1 is greater than or equal to int2 returns TRUE if string1 is equal to string2 string1 is not equal to string2 string is null (and must be seen) returns TRUE if expr is FALSE, otherwise returns TRUE expr1 is TRUE OR expr2 is TRUE expr1 is TRUE AND expr2 is TRUE && 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 110

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"

111

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

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

113

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

114

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

115

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 ]

116

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

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

118

$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

119

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

120

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.

121

#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 2 3 4 5 6 Sunday Monday Tuesday Wednesday Thursday Friday

122

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

123

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) 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 if [ $b -eq 0 ]

124

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

125

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

126

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 Var Var Var Var Var Var Var Var value value value value value value value value value is is is is is is is is is : : : : : : : : : karu jun 15 deepu aug 11 yogi jun 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

127

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

128

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 do 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` done $sh wdisp1to10.sh 1 2 expression command(s)

#command substitution

129

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

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 131

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"

132

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

133

# 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

134

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 135

$sh cal.sh July 2007 Su Mo Tu We Th 1 2 3 4 5 8 9 10 11 12 15 16 17 18 19 22 23 24 25 26 29 30 31

Fr 6 13 20 **

Sa 7 14 21 28

# 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

136

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 a b Function Delete one by one 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 a b Function Delete one by one Delete all

Enter Choice: a ab.sh xy.sh Enter file name to be deleted

137

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 a b Function Delete one by one 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 138

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 139

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

140

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

141

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"

142

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

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 Value Value Value of of of of i i i i is is is is 1 2 3 4

144

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

145

Value Value Value Value 9.11

of of of of

is is is is

: : : :

1 2 3 4

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 $trap rm $TMPFILE; exit 1 2 15


$trap 2 $trap 2 Signal number Signal name 0 EXIT 1 HUP 2 INT 3 QUIT 6 ABRT 9 KILL 14 ALRM 15 TERM

Log message and exit on hangup Removes $TMPFILE on signals 1, 2 or 15 Ignores interrupts Reset default processing of interrupts

Generated for Exit from the shell Hang-up Interrupt Example Delete key, ^c Quit Abort Kill Alarm timeout Software termination signal(sent by kill by default)

Table: Signal number and names for trap 9.12 Arrays

146

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 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 Or All the elements of an array is displayed by using : echo {arr_name[@]} Or echo {arr_name[*]} Fri Sat ${arr_name[15]}

147

$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 e \t Size of an array is 7 echo {#arr_name[*]} : {#days[@]}

The unset command is used to destroy complete array or individual array element. unset arr_name[subscript] Unset arr_name # Destroys the array element at index subscript. # 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 148

Kishan is 15 June 2007 Contets of an array are using loop a[ a[ a[ a[ a[ a[ a[ 0 1 2 3 4 5 6 ] ] ] ] ] ] ] = = = = = = = Dob of Kishan is 15 June 2007

Number of elements in an array is : 7 Contents of an array without loop 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" 149 : Dob of Kishan is

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

150

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

151

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

152

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 153

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 Sorted array is : 9 11 14 15 19 : 14 11 15 9 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[@]}

154

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

155

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

156

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 Sorted array is : 9 11 14 15 19 : 14 11 15 19 9

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 Sorted array is : 9 11 14 15 19 : 19 11 15 14 9

Enter element to be search :2258 2258 is Not Found

157

Chapter 10. 10.1 Introduction

Advanced filter The AWK

There are three variations of AWK: AWK NAWK GAWK The original from AT&T A newer, improved version from AT&T 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

158

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!" }

159

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

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 .

160

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 printf printf printf ( ( ( ( format); format, arguments...); format) >expression; 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 \f \n \r \t Backspace Formfeed Newline Carriage Return 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 %d %e %f %g %o %s %x %% ASCII Character Decimal integer Floating Point number (engineering format) Floating Point number (fixed point format) The shorter of e or f, with trailing zeros removed Octal String 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

161

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

162

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 kishan 6000 $awk -F"|" '/kishan/||/chandu/{printf "%3d %-20s %d \n", NR, $2 ,$4}' emp.dat 2 kishan 3 chandu 6000 5000

$awk -F"|" '$4 > 5000 {printf "\n%3d %-20s %d\n", NR, $2 ,$4}' emp.dat 2 kishan 4 sindu 6 yogish 6000 7000 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 }

163

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

164

}$4 > 4000 { sum+=$4 printf "\n\t%d } END { printf "\tSum of salaries greater than 4000 = %d \n\n", sum } $awk f empsal.awk emp.dat Emp data base ... %s = %d \n", $1, $3,$4

123 345

cse mech

= =

5000 6000

1345 ece = 6000 Sum of salaries greater than 4000 = 10.13.1 Parameter Passing

17000

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

{ 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

166

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) sqrt(x) length length(x) substr(s1,m,n) returns integer value of x returns square root of x returns length of complete record returns length of x 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 167

end-of-file was encountered, and 1 if some error occurred. Expression getline getline var getline <"file" getline var <"file" "cmd" |getline "cmd"|getline var 10.16 Regular Expression Awk uses the same regular expressions weve been using pattern matching metacharacters: ^ : $ : . : ? : * : + : [abcd] : [^abcd]: [a-z] : (exp) (exp1|exp2): 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 Character class Matches a or b or c or d. Negated character class - Matches other than a or b or c or d. Range of characters - Matches a or b or c or d -----or z. Matches exp1 or exp2 Sets $0, NF, NR, FNR var, NR, FNR $0, NF var $0, NF var

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

168

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' 123|kishan|cse|6000 345|chandu|mech|5000 567|yogish|ise|5670 emp.dat

The sole rule has a relational expression as its pattern and it has no actionso 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 169

$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

170

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; t=a;a=a+b;b=t}; exit}' while(++x<=10){print a;

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

171

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

172

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++) 173

print arr[x]; } $cat 5 am 3 am 1 am 4 am 2 am $awk 1 am 2 am 3 am 4 am 5 am inp.dat the fifth person the third person the first person the fourth person the second person -f arr1.awk inp.dat the first person the second person the third person the fourth person 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 } 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. The value of the function is returned via a statement of the

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

# 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

175

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

176

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: emp.dat: emp.dat: emp.dat: emp.dat: line line line line line # 1, Expected 4 fields, line: 123|kishan|cse|6000 #2, Expected 4 fields, line: 345|chandu|mech|5000 # 3, Expected 4 fields, line: 134|sindu|ece|7000 # 4, Expected 4 fields, line: 216|deepu|ece|2300 # 5, Expected 4 fields, line: 567|yogish|ise|5670

177

You might also like