Flow Control
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.
Types if statement case statement
if list1 then list2 elif list3 then list4 else list5 fi if list1 ; then list2 ; elif list3 ; then list4 ; else list5 ; fi ;
if commandt then command command... fi
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
The $? Variable $ cp phonebook phone2 $ echo $? 0 Copy "succeeded
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
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).
example $ day="monday " $ test $day = monday $ echo $?
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.
-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 $
Special test X"$symbol" = X The X in front of symbol prevents test from interpreting the characters stored in symbol as an operator.
An Alternative Format for test [ expression ] $ [ -z "$nonnullvar" ] $ echo $? 1 $
Continue… if [ "$name" = julio ] then echo "Would you like to play a game?" fi
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.
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 $
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.
[ -f /users/steve/phonebook ]
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)
If…else if commandt then command command... else command command... fi
Terminate program Exit 1
elif if commandt then command command... Elif then command command... else command command... fi
Case case word in pattern1) list1 ;; pattern2) list2 ;; esac
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
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
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
Null command : 1.: 2.The && and || Constructs