Download presentation
Presentation is loading. Please wait.
Published byHolly Carter Modified over 8 years ago
1
Flow Control
2
The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based on a condition, you change the flow of the script. The commands are called flow control commands.
3
Types if statement case statement
4
if list1 then list2 elif list3 then list4 else list5 fi if list1 ; then list2 ; elif list3 ; then list4 ; else list5 ; fi ;
5
if commandt then command command... fi
6
Exit Status Whenever any program completes execution under the Unix system, it returns an exit status back to the system. This status is a number that usually indicates whether the program successfully ran. By convention, an exit status of zero indicates that a program succeeded, and nonzero indicates that it failed. In a pipeline, the exit status is that of the last command in the pipe. So in who | grep fred
7
The $? Variable $ cp phonebook phone2 $ echo $? 0 Copy "succeeded
8
Test command test expression String Operators test "$name" = julio The = operator is used to test whether two values are identical. –It's good programming practice to enclose shell variables that are arguments to test inside a pair of double quotes (to allow variable substitution). $ name= $ test $name = julio sh: test: argument expected $ $ test "$name" = julio
9
String operators string1 = string2 string1 is identical to string2. string1 != string2 string1 is not identical to string2. string string is not null. -n string string is not null (and string must be seen by test). -z string string is null (and string must be seen by test).
10
example $ day="monday " $ test $day = monday $ echo $?
11
You can test to see whether a shell variable has a null value with the third operator test "$day" This returns true if day is not null and false if it is.
12
-n and –z $ nullvar= $ nonnullvar=abc $ test -n "$nullvar“ Does nullvar have nonzero length? $ echo $? 1 No $ test -n "$nonnullvar“ And what about nonnullvar? $ echo $? 0 Yes $ test -z "$nullvar" Does nullvar have zero length? $ echo $? 0 Yes $ test -z "$nonnullvar" And nonnullvar? $ echo $? 1 No $
13
Special test X"$symbol" = X The X in front of symbol prevents test from interpreting the characters stored in symbol as an operator.
14
An Alternative Format for test [ expression ] $ [ -z "$nonnullvar" ] $ echo $? 1 $
15
Continue… if [ "$name" = julio ] then echo "Would you like to play a game?" fi
16
Integer Operators int1 -eq int2 int1 is equal to int2. int1 -ge int2 int1 is greater than or equal to int2. int1 -gt int2 int1 is greater than int2. int1 -le int2 int1 is less than or equal to int2. int1 -lt int2 int1 is less than int2. int1 -ne int2 int1 is not equal to int2.
17
Comparison between string and integer operators $ x1="005" $ x2=" 10" $ [ "$x1" = 5 ] String comparison $ echo $? 1 False $ [ "$x1" -eq 5 ] Integer comparison $ echo $? 0 True $ [ "$x2" = 10 ] String comparison $ echo $? 1 False $ [ "$x2" -eq 10 ] Integer comparison $ echo $? 0 True $
18
File operators -d file file is a directory. -e file file exists. -f file file is an ordinary file. -r file file is readable by the process. -s file file has nonzero length. -w file file is writable by the process. -x file file is executable. -L file file is a symbolic link.
19
[ -f /users/steve/phonebook ]
20
The Logical Negation Operator ! The Logical AND Operator –a Parentheses –[ \( "$count" -ge 0 \) -a \( "$count" -lt 10 \) ] The Logical OR Operator –o The -o operator has lower precedence than the - a operator, meaning that the expression "$a" -eq 0 -o ("$b" -eq 2 -a "$c" -eq 10)
21
If…else if commandt then command command... else command command... fi
22
Terminate program Exit 1
23
elif if commandt then command command... Elif then command command... else command command... fi
24
Case case word in pattern1) list1 ;; pattern2) list2 ;; esac
25
FRUIT=kiwi case "$FRUIT" in apple) echo "Apple pie is quite tasty." ;; banana) echo "I like banana nut bread." ;; kiwi) echo "New Zealand is famous for kiwi." ;; esac
26
if [ "$#" -ne 1 ] then echo "Usage: number digit" exit 1 fi case "$1" in 0) echo zero;; 1) echo one;; 2) echo two;; 3) echo three;; 4) echo four;; 5) echo five;; 6) echo six;; 7) echo seven;; 8) echo eight;; 9) echo nine;; *) echo Bad argument;; esac
27
case "$char" in [0-9] ) echo digit;; [a-z] ) echo lowercase letter;; [A-Z] ) echo uppercase letter;; ? ) echo special character;; * ) echo Please type a single character;; esac
28
Null command : 1.: 2.The && and || Constructs
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.