1 / 75

Decisons in Shell Programming

Decisons in Shell Programming. The if construct. The general format of the if command is: Every command has exit status If the exit status is zero , then the commands that follow between the then and if are executed, otherwise they are skipped. if command then command

nirav
Télécharger la présentation

Decisons in Shell Programming

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. Decisons in Shell Programming

  2. The if construct • The general format of the if command is: • Every command has exit status • If the exit status is zero, then the commands that follow between the then and if are executed, otherwise they are skipped if command then command command … fi

  3. exit status • Whenever any program completes execution, it returns an exit status back to the system • status is a number • status 0: program executed successfully • status of nonzero: program executed unsuccessfully

  4. The $? Variable • The shell variable $? Is automatically set by the shell to the exit status of the last command executed

  5. The $? Variable (continue.) $ touch file1 $ cp file1 file2 $ echo $? 0 $ cp file3 file1 cp: cannot access file3 $ who hyunku console Jan 24 00:16 (:0) hyunku pts/4 Jan 24 00:16 (:0.0) sbenayed pts/5 Jan 251 12:01 (216.87.102.199) $ who | grep abuzneid $ echo $? 1 $ who | grep sbenayed sbenayed pts/5 Jan 25 21:01 (216.87.102.199) $ echo $? 0

  6. The test command • Format: test expression • test evaluates expression, and if the result is true, it returns an exit status of zero, otherwise the result is false • test "$name" = Alice • Operator (=) separates arguments which means it must be surrounded with white space characters • It's a good programming practice to enclose shell variables that are arguments to test inside a pair of doubles quotes

  7. The test command (continue.) $ name= $ test $name = Alice test: argument expected $ test "$name" = Alice $ = Alice test arguments test $name = Alice without name null

  8. The test command (continue.) = null Alice arguments test test $name = Alice with name null

  9. File operators in test command

  10. File operators intest command (continue.)

  11. String operators in test command (continue.)

  12. Integer Comparison Operators in testcommand (continue.)

  13. The test command (continue.)

  14. Example: testString Operator • test can be quite picky about its arguments • The = operator has its highest precedence than the –z operator, so test expects an argument to follow • To avoid this sort of problem test X "$symbol'=X $ blanks=" " $ test $blanks $ echo $? 1 $ test "$blanks" $ echo $? 0 $

  15. An Abstract format to test • test expression  [ expression ] • Spaces must appear after [ and before ] $ if [ "$name = Alice ] > then > echo "Hello Alice" > fi $ echo $? 0 $

  16. Example: testInteger Operator $ X1="005" $ X2=" 10" $ [ "$X1" = 5 ] $ echo $? 1 $ [ "$X1" -eq 5 ] $ echo $? 0 $

  17. Example: testFile Operator • To test if the file exits [ -f /home/abuzneid/UNIX/aaa ] • To test is the file exists and is also readable by the user [ -r /home/abuzneid/UNIX/aaa ]

  18. The Logical Negation Operator ! • The unary logical operator ! can be placed in front of any other test expression to negate the result of the evaluation of that expression [ ! -r /home/abuzneid/UNIX/aaa ] returns true if /home/abuzneid/UNIX/aaa is not readable [ ! "$X1" = "$X2" ] returns true if $X1 is not identical to $X2and is equivalent to [ "$X1" != "$X2" ]

  19. The Logical ANDOperator -a • Returns true only, if the two joined expressions are both true [ ! -f "$myfile" –a –r "myfile" ] Returns true if $myfile is an ordinary file and is readable by the user • Parentheses can be used in test expression: [ \( "$count" –ge o \) -a \( "$count" –1t 10 \) ]

  20. The Logical OROperator -o • Returns true if either the first expression is true or the second expression is true • The –o operator has lower precedence than the –a operator • "$a" –eq 0 –o "$b" –eq 2 –a "$c" –eq 10 gets evaluated by test as "$a" –eq 0 –o ("$b" –eq 2 –a "$c" –eq 10) • Parentheses can change the order if necessary

  21. The else Construct if commandt then command1 command2 … else command3 command4 … fi

  22. The exitCommand • Exit immediately terminates execution of a shell program • exit n • n: the exit status that you want to be returned • If n is not specified, then the exit status used is that of the last command executed before the exit

  23. The elseif Construct if command1 then command command … else if command2 then command command … else … if commandn then command command … else command command … fi … fi fi

  24. The elif Construct (continue.) if command1 then command command … elif command2 then command command … … elif commandn then command command … else command command … Fi

  25. The elif Construct (continue.) • command1, command2…commandn are executed in turn and their exit status tested • As soon as one returns an exit status of zero, the commands listed after the then that follows are executed up to another elif, else, or fi • If none of the command returns a zero status, then the commands listed after the optional else are executed

  26. The case Command case value in pat1) command;; command;; … command;; pat2) command command;; … command;; patn) command;; command;; … command;; esac

  27. The case Command • The word value is compared against the values pat1, pat2, … until a match is found • When a match is found, the commands listed after the matching value, up to the double semicolons, are executed • The shell lets you use *,?,[] special characters with shell • The symbol | has the effect of a logical OR when used between two patterns • Pat1 | Pat2

  28. The null Command • The format of null command is : • Example: suppose you want to check to make sure that the value stored in the variable var exists in the file /home/abuzneid/.profile, and if it doesn't, you want to issue an error message and exit from the program

  29. The null Command (continue.) if grep "^var" /home/abuzneid/.profile > then > hello > else > echo "$var does not exit. > exit > fi

  30. The && Construct • command1 && command2 • Example: $ sort file1 > /tmp/file2 && mv /tmp/file2 file1 The mv command will be executed only if the sort is successful If $?!=0 If $?=0

  31. The || Construct • command1 || command2 • Example: $ grep "$name" phonebook || echo "couldn’t find $name" • Example: $ who | grep "^name" > /tmp/null || echo "$name is not logged on" If $?=0 If $?!=0

  32. The || Construct (continue.) • command3 will be executed if command1 or command2 returns zero • if command1 || command2 • then • Command3 • fi

  33. Shell Script: on • Checks if a user is logged in or not • To view on click here $ on Incorrect number of arguments Usage: on user $ on abuzneid abuzneid is logged on $ on sobh sobh is not logged on

  34. Shell Script: greetings • Prints greeting wherever you logged in the system • To view greetings click here $ greetings Good morning

  35. Shell Script: rem • Removes someone from the phonebook • To view rem click here $ rem Incorrect number of arguments. Usage: rem name $ rem Abdelshakour $ rem 'Abdelshakour Abuzneid' I coudn't find Abdelshakour Abuzneid in the phone book $ rem Susan More than one match; please qualify further $ rem 'Susan Clinton'

  36. Shell Script: number • Translates a digit to english • To view number click here $ number 9 nine $ number 88 Bad argument; please specify a single digit $ number Usage: number digit $

  37. Shell Script: charis • Classify character given as argument • To view charis click here $ charis q Please type a single character $ charis 9 Please type a single character $ charis 7 Please type a single character $ sh -x charis 9 + [ 1 -ne 1 ] char=9 + echo 9 + wc -c numchars= 2 + [ 2 -ne 1 ] + echo Please type a single character Please type a single character + exit 1

  38. Shell Script: charis2 • Classify character given as argument--version 2 • To view charis2 click here $ charis2 t lowercase letter $ charis2 '>' special character $ charis zzz Please type a single character

  39. Shell Script: greetings2 • Program to print a greeting case version • To view greetings2 click here $ date Thu Jan 25 09:14:08 EST 2001 $ greetings2 Good Morning $

  40. Loops in Shell Programming

  41. The for Command for var in word1 word2…wordn do command command … done

  42. The for Command (continue.) • There is many ways to pass files to for 1. for file in f1 f2 f3 f2 do run $file done 2. for file in f[1-4] do run $file done

  43. The for Command (continue.) 3. for file in * do run $file done 4. for file in 'cat filelist' do run $file done

  44. The $@ Command • Replaces $* when argument are passed to for • To view args click here $ args a b c number of arguments passed is 3 a b c $ args 'a b' c number of arguments passed is 2 a b c

  45. The $@ Command (continue.) • shell replaces the value of $* with $1, $2… • shell replaces the value of $@ with "$1", "#2"… • The double quotes are necessary around $@, as without them this variable behaves just like $*

  46. The $@ Command (continue.) • args -- version 2 • To view args2 click here $ args2 a b c number of arguments passed is 3 a b c $ args2 'a b' c number of arguments passed is 2 a b c $ args number of arguments passed is 0 $

  47. The for without the list • Shell will automatically sequence through all of the arguments typed on the command line, just as if you had written in "$@" • To view args3 click here $ args3 a b c number of arguments passed is 3 a b c $ args3 'a b' c number of arguments passed is 2 a b c $

  48. The while Command • command1 is executed and its exit tested • If it's zero, then the commands enclosed between do and done are executed • Then process continues until command1 returns nonzero status • while command1 • do • command • command • … • done

  49. Shell Program: printargs • Print command line arguments are per line • To view printargs click here $ printargs a b c a b c $ printargs 'a b' c a b c

  50. Shell Program: printargs(continue.) $ printargs * Documents args args2 args3 charis greetings mail memos number on personal phonebook printargs Rem $ printargs $ printargs * Documents args args2 args3 charis greetings mail memos number on personal phonebook printargs Rem $ printargs $

More Related