1 / 32

Introduction to Unix (CA263) Round and Round

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.

Download Presentation

Introduction to Unix (CA263) Round and Round

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix (CA263)Round and Round By Tariq Ibn Aziz Dammam Community College Compunet Corporation

  2. Objectives • In this lecture you will learn the following loops • the for; • the while; and • the until. Compunet Corporation

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related