320 likes | 484 Views
Introduction to Unix (CA263) Round and Round. By Tariq Ibn Aziz Dammam Community College. Objectives. In this lecture you will learn the following loops the for; the while; and the until. The for command is used to execute a set of commands a specified number of times.
E N D
Introduction to Unix (CA263)Round and Round By Tariq Ibn Aziz Dammam Community College Compunet Corporation
Objectives • In this lecture you will learn the following loops • the for; • the while; and • the until. Compunet Corporation
The for command is used to execute a set of commands a specified number of times. Here is a loop example that will execute 3 times for i in 1 2 3 do echo $i done Try directly on terminal $ for i in 1 2 3 > do > echo $i > done 1 2 3 $ The for Command Compunet Corporation
cat run tbl $1 |nroff –mm –Tlp |lp $ If you want to run the files memo1 through memo2 you can use the for command for file in memo1 memo2 >do > run $file >done $ The shell permits filename substitution in the list of words in the for command. for file in memo[1-2] do run $file done Example for Command Compunet Corporation
If you want to run all of the files in your current directory. for file in * do run $file done $ If the file filelist contains a list of the files that you want to run through run. files=`cat filelist` for file in $files do run $file done Example for Command Compunet Corporation
The $* Variable • If you found that you were using the run program to process several files at once. $ cat run for file in $* do tbl $file |nroff –mm Tlp | lp done $ $ run memo1 memo2 memo3 memo4 • The $* will be replaced by the four arguments memo1, memo2, memo3, and memo4. Compunet Corporation
The $* Variable[1] • The $* in for command will be replaced by shell with a b c $ cat args echo Number of argument passed is $# for arg in $* do echo $arg done $ $ args a b c Number of argument passed is 3 a b c $ Compunet Corporation
The $* Variable[2] $ args 'a b' c Number of argument passed is 2 a b c $ • Even though a b was passed as a single argument to args, the $* in the for command was replaced by shell with a b c, which is three words. Compunet Corporation
The $@ Variable[1] • The special variable “$@” will be replaced with "$1", "$2" …, the double quotes are necessary around $@, otherwise it will behave has $*. $ cat args echo Number of argument passed is $# for arg in "$@" do echo $arg done $ $ args a b c Number of argument passed is 3 a b c $ Compunet Corporation
The $@ Variable[2] $ args 'a b' c Number of argument passed is 2 a b c $ • The special variable “$@” will be replaced with "$1", "$2" …, the double quotes are necessary around $@, otherwise it will behave has $*. Compunet Corporation
The second type of looping command is while. cat twhile i=1 While [ "$i" –le 5 ] do echo $i i=`expr $i + 1` done twhile 1 2 3 4 5 $ The while Command Compunet Corporation
The program print each of the command-line argument one per line. $ cat prargs While [ "$#" –ne 0 ] do echo $i shift done $ $ prargs $ $ prargs a b c a b c $ prargs 'a b' c a b c $ prargs * addresses nu name phonebook stat The while Command Compunet Corporation
The until Command • The while command continues execution as long as the command listed after the while returns a zero exit status. • The until command is similar to the while, only it continues execution as long as the command follows the until returns a nonzero exit status. Compunet Corporation
Example-1 until Command[1] $ cat mon if [ "$#" –ne 1 ] then echo "Usage: mon user" exit 1 fi until who | grep "^$user ">/dev/null do sleep 60 done echo " $user has logged on“ $ Compunet Corporation
Example-1 until Command[2] $ mon sandy sandy has logged on $ mon sandy & 4392 $ nroff newmemo do other work … sandy has logged on Compunet Corporation
$ cat mon if [ "$1" = -m ] then mailopt=TRUE shift else mailopt=FALSE fi if [ "$#" –eq 0 –o "$#" –gt 1 ] then echo "Usage: mon [-m] user" echo "-m means to be informed by mail" exit 1 fi user="$1" until who|grep "^$user ">/dev/null do sleep 60 done if [ "$mailopt" =FALSE ] then echo " $user has logged on" else echo " $user has logged on"|mail steve fi $ $ man sandy –m Usage: mon [-m] user -m means to be informed by mail Example-2 until Command[1] Compunet Corporation
Example-2 until Command[2] $ mon –m sandy & $ mon sandy & 5435 $ nroff newmemo do other work … you have mail $ mail From steve Mon Jul 22 11:05 EDT 1985 sandy has logged on ?d $ Compunet Corporation
More on Loops • Breaking Out of a Loop • Sometime you want to make an immediate exit from a loop. You can use the break command. • If the break command is used in break n form, then the n innermost loops are immediately exited. Compunet Corporation
Both the while and for loop will be exited if error is nonnull for file do … while [ "$count" –lt 10 ] do … if [ -n "$error" ] then break 2 fi … done … done Example break Command Compunet Corporation
Skipping the Remaining Commands in Loop • The continue command is similar to break, only it doesn’t cause the loop to be exited, but the remaining commands in the loop to be skipped. • Like the break, an optional number can follow the continue, so continue n causes the commands in the innermost n loops to be skipped; but execution of loop then continues as normal. Compunet Corporation
Each value of file is checked to make sure that file exist. for file do if [ ! -f "$file" ] then echo "$file not found" continue fi … done Example continue Command Compunet Corporation
Executing a loop in the Background • An entire loop can be sent to the background simply by placing an ampersand after the done: $ for file in memo[1-4] >do > run $file >done & 9932 $ Compunet Corporation
You can also perform I/O redirection on the entire loop. $ for i in 1 2 3 4 >do > echo $i >done > loopout $ cat loopout 1 2 3 4 I/O Redirection on a Loop Compunet Corporation
I/O Redirection on a Loop • You can override redirection of the entire loop’s input or output by explicitly redirecting the input and/or output of commands inside the loop. for file do echo "Processing file $file" >/dev/tty … done > output • echo’s output is redirected to the terminal while the rest goes to the file output. Compunet Corporation
I/O Redirection on a Loop • You can also redirect the standard error output from a loop, simply by tacking on a 2> file after the done: while [ "$endofdata" –ne TRUE ] do … done 2> errors Compunet Corporation
Piping Data Into and Out of a Loop • A command output can be piped into a loop, and the entire output from a loop can be piped into another command in the executed manner. $ for i in 1 2 3 4 >do > echo $i >done | wc –l 4 $ Compunet Corporation
Typing a Loop on One Line for i in 1 2 3 4 do echo $i done becomes for i in 1 2 3 4; do echo $i; done Compunet Corporation
Typing condition on One Line • The same rules apply to while and until loops. if commands can also be typed on the same line using a similar format: $ if [ 1 = 1 ]; then echo yes; fi yes $ if [ 1 = 2 ]; then echo yes; else echo no; fi no • Note that no semicolons appear after the then and the else. Compunet Corporation
The getopts Command • The shell provide a built-in command getopts that exist for the express purpose of processing command line argument. • The general format of the command is: getopts options variable Compunet Corporation
The getopts Command • The getopts command is designed to be executed inside a loop. • Each time through the loop, getopts examines the next command line argument and determine if it is a valid option. • This check if the argument begins with a minus sign and is followed by any single letter contained inside option. If it does, getopts stores the matching option letter inside the specified variable and returns a zero exit status. Compunet Corporation
The getopts Command • If the letter that follows the minus sign is not listed in options, getopts stores a question mark inside variable before returning with a zero exit status. It also writes an error message to standard error. • If there are no more arguments left on the command line or if the next argument doesn’t begin with a minus sign, getopts return a nonzero exit status. Compunet Corporation
The getopts Command • Suppose you want getopts to recognize the options –a, -i, and –r for a command called foo. Your getopts call might look like this: • getopts air option Compunet Corporation