Chapter 5: Unix Shell Programming
Chapter 5: Unix Shell Programming
for loop: A loop is a programming device that lets you cycle through the same steps several times. The general form of the procure shell for loop is: for variable-name in value1 value2 ......... do ----statements---done Ex: for i in ACE ERA ACS do echo $i is in Hyderabad echo In Hyderabad $i \ ? echo " done Assume it is in an executable file comp then try running it. The i in the first line is the shell variable. The list of companies are the values that the variables assume in succession as the shell executes the for loop. The $i represents the current value of the variables i. A for loop can get values from the command line as in this case. for i in $1 $2 $3 do echo $i is in Hyderabad done Arguments for this shell script are passed from the command line during the execution as: $ sh comp ACE ACS ERA The same comp script can be changed by using $* which stands for all command line arguments following the script name: for i in $* do echo $i is in Hyderabad. echo In hyderabad $i echo in hyderabad $i done Generating values for a for loop: for loop variables get values in many ways some of them are discussed here under: " $i !
Unix Shell Programming 1. List the values explicitly after the "in" statement. for name in ACE ERA ACS 2. Take values from the command line for i in $* 3. Take filename from a directory as values for file in * 4. Take values from a shell variable using read read name for n in $name 5. Take values from the output of a command for i in `cat test` What output would the following program give? n=0 for i in $* do n=`expr $n + 1` echo Total number of arguments are $# echo All the arguments are $* echo Argument no. $n is $i done
Unix Shell Programming Case Statement is used to set up menus. Eg: echo please enter the choice echo ' 1 date echo ' 3 ls read choice. case $choice in 1) date;; 2) who;; 3) ls;; 4) pwd;; *) echo that wasn't the required choice ! Bye esac We used the single quotes (') along with the echo to preserve the spacing. will print just one space between each argument. The double semicolon the following labels. The right parenthesis Otherwise echo (;;) separates one Put this 2 who ' 4 pwd '
choice from the next. The case statement tries to match the value of choice against one of ()) is used to identify label names. program in a script "ask" and make it an executable file. The words case and esac must come at the beginning of a command that is at the beginning of the line, or after a semicolon (;) or ampersand (&). The '*' choice matches any value.
Unix Shell Programming Here each command line argument is matched against the list of choices.
Unix Shell Programming Here is an example that watches for someone (say Raman) to login: While sleep 60 do who | grep Raman done. The sleep which pauses for 60 seconds will always execute normally (unless interrupted) and therefore return "success". Hence the loop will check once a minute to see if Raman has logged in. In this program if Raman has logged in you must wait 60 seconds. If raman stays logged in, you will be told about him once a minute, The loop can be turned inside out and written with an until, to provide the information once, without delay, if Raman is logged in currently, until who | grep raman do sleep 60 done In this case, the condition is first checked and if found to be true, the loop terminates, else the program waits for 60 secs. And the checking process is done. This cycle repeats until raman logs into the system. The until loop is similar to a while loop, with the difference that, it is executed until the test condition succeeds. In other words, a while loop runs until a control command fails, and the until loop runs until a control command succeeds. General form of Until Statement. until control command do Commands done Here the control command is executed, If it fails the commands between do and done are executed and the control command is attempted again. control command reports success. The process continues until the
Unix Shell Programming If the given condition, "if cmp -s $1 $2", executes successfully, then the instructions between the then and else are executed. If they are different then the instructions between else and fi are executed. The if then ------- elif statement: A further extension to the if form, this lets you string several alternatives one after the other. The usage of elif keyword is similar to the else if statement. general form: if control command 1 then ----commands---elif control command 2 then ----commands---else ----commands---fi Let us take an example. Type the following text in a file called test.sh read salary if test $salary -ge 3000 then echo the grade is A elif test $salary -ge 2000 then echo the grade is B elif test $salary -ge 1500 then echo the grade is C else echo the grade is D fi In this case if the condition in the first "if succeeds, then all statements between then and elif are executed. If the condition fails the next if condition is evaluated and if this succeeds, the statements between then and elif are executed. If the entire if conditions fail then the statements between else and fi are executed.
Unix Shell Programming For example: $ echo sometext >rama $ if test -r rama > then > > > > fi The file called rama is a readable file $ The first instruction echoes a text into the file called Rama. The second instruction has checked whether the file called rama is a readable file or not and has reported with an affirmative. echo The file called rama is a readable file else echo The file called rama is not a readable file
Unix Shell Programming the operators that can be used are - eg - ne - gt - lt - ge - le the values of N and M are equal The values of N and M are not equal N is greater than M N is less than M N is greter than or equal to M N is less than or equal to M
Let's take an example. $ users=`who | wc -l` $ if test $users -qt 8 > then echo "more than 8 people logged on" > fi The variable called users will store the value that is returned by the pipeline command, which is nothing but a number, indicating the number of users currently logged in. This is compared with 8 and if it is greater than this value, the message "more than 8 people logged on" is printed.
10
Unix Shell Programming outcome of both these possibilities is positive.test !-r space - True if the file called space is not a readable file.
11
Unix Shell Programming test -r space -a -w space - True if file called space is readable and writeable. Note: Spaces should be allowed between the various options. Also note that the 'and' and 'or' operators are used between the tests they link, not before or after them. eg: This can be used to check two files and append the contents. Open a file "append" and write the program. if test -w $2 -a -r $1 then cat $1 >> $2 else echo cannot append fi now give $ append programme1 Programme2
12
Unix Shell Programming For example, type the following in a file called beep. sleep $1 echo ^G HI wake up ! ^G $ beep 60 & This will introduce a 60-second pause, and then the message "HI wake up!" will displayed, preceded and followed by a beep.
Note:
1. The arguments including the arithmetic operator must be separated by blank space from one another. 2. We also need to mask the * operator with a back slash or quotes otherwise it is interpreted by the shell as a request for filename expansion. 3. Expression division is integer division and is truncated to the nearest integer. 4. The % is the module operator provided by the shell.
13