Upload
clement-mckinney
View
221
Download
0
Embed Size (px)
DESCRIPTION
Compunet Corporation The for Command 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 do echo $i done Try directly on terminal $ for i in >do >echo $i >done $
Citation preview
Compunet Corporation
Introduction to Unix (CA263)
Round and Round
By
Tariq Ibn AzizDammam 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
• 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 3do
echo $idone
• Try directly on terminal$ for i in 1 2 3> do> echo $i> done123$
Compunet Corporation
Example for Command$ cat runtbl $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 $filedone
Compunet Corporation
Example for Command• If you want to run all of
the files in your current directory.
for file in *do
run $filedone$
• If the file filelist contains a list of the files that you want to run through run.
files=`cat filelist`for file in $filesdo
run $filedone
Compunet Corporation
The $* Variable• If you found that you were using the run program to
process several files at once.
$ cat runfor file in $*do tbl $file |nroff –mm Tlp | lpdone$ $ 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 argsecho Number of argument passed is $#for arg in $*do echo $argdone$ $ args a b cNumber of argument passed is 3abc$
Compunet Corporation
The $* Variable[2]
$ args 'a b' cNumber of argument passed is 2abc$
• 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 $argdone$ $ args a b cNumber of argument passed is 3abc$
Compunet Corporation
The $@ Variable[2]
$ args 'a b' cNumber of argument passed is 2a bc$
• The special variable “$@” will be replaced with "$1", "$2" …, the double quotes are necessary around $@, otherwise it will behave has $*.
Compunet Corporation
The while Command
• The second type of looping command is while.
$ cat twhilei=1While [ "$i" –le 5 ]doecho $ii=`expr $i + 1`
done
$ twhile12345$
Compunet Corporation
The while Command• The program print each of the
command-line argument one per line.
$ cat prargsWhile [ "$#" –ne 0 ]do
echo $ishift
done$ $ prargs$
$ prargs a b cabc$ prargs 'a b' ca bc$ prargs *addressesnunamephonebookstat
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 monif [ "$#" –ne 1 ]thenecho "Usage: mon user"exit 1
fiuntil who | grep "^$user ">/dev/nulldosleep 60
doneecho " $user has logged on“$
Compunet Corporation
Example-1 until Command[2]
$ mon sandysandy has logged on$ mon sandy &4392$ nroff newmemo do other work…sandy has logged on
Compunet Corporation
Example-2 until Command[1]
$ cat monif [ "$1" = -m ]then
mailopt=TRUEshift
elsemailopt=FALSE
fiif [ "$#" –eq 0 –o "$#" –gt 1
]then
echo "Usage: mon [-m] user"echo "-m means to be informed by mail"exit 1
fiuser="$1"
until who|grep "^$user ">/dev/nulldo
sleep 60doneif [ "$mailopt" =FALSE ]then
echo " $user has logged on"else
echo " $user has logged on"|mail steve
fi$$ man sandy –mUsage: mon [-m] user-m means to be informed by mail
Compunet Corporation
Example-2 until Command[2]
$ mon –m sandy &$ mon sandy &5435$ nroff newmemo do other work…you have mail$ mailFrom steve Mon Jul 22 11:05 EDT 1985sandy 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
Example break Command• Both the while and for
loop will be exited if error is nonnull
for filedo … while [ "$count" –lt 10 ] do …
if [ -n "$error" ]then
break 2fi…
done …done
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
Example continue Command
• Each value of file is checked to make sure that file exist.
for filedo if [ ! -f "$file" ]thenecho "$file not found"continuefi…
done
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
I/O Redirection on a Loop
• You can also perform I/O redirection on the entire loop.
$ for i in 1 2 3 4
>do> echo $i>done > loopout
$ cat loopout1234
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 filedoecho "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 –l4$
Compunet Corporation
Typing a Loop on One Line
for i in 1 2 3 4doecho $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; fiyes$ if [ 1 = 2 ]; then echo yes; else echo no; fino
• 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