if testcommand then command(s) fi if testcommand; then command(s) fi if testcommand; then command(s) else command(s) fi if testcommand; then command(s) elif testcommand; then command(s) else command(s) fi
#!/bin/sh # Demonstrate if echo "\n--If 1" if test $# -eq 0 ; then echo "I need an argument" ; exit 2 ; fi echo "\n--If 2" if test $# -ge 2 then echo "More than 1 argument is good" echo "Here they are" echo $* else echo "Only 1 argument" if test -z $1 ; then echo "But it's empty" ; ## 2nd ; not needed else echo "And it is $1" ; fi ## This ; needed fi echo "\n--If 3" if [ $# -lt 3 ] then echo "Less than 3 arguments\nI'll show them" echo $* elif [ $3 = "quit" ]; then echo "The third argument says \"quit\"" else echo "Argument 3 is $3" fi echo "\n--If 4" if [ -f $1 -o -d $1 ]; then echo "$1 is a regular file or directory\c" if [ -x $1 ]; then echo " and it is executable"; fi else echo "Argument 1 is just another name" fi
Integer tests | |
---|---|
n1 -ne n2 | True if integers are not equal. |
n1 -eq n2 | True if integers n1=n2 |
n1 -lt n2 | True if integers n1<n2 |
n1 -le n2 | True if integers n1<=n2 |
n1 -ge n2 | True if integers n1>=n2 |
n1 -gt n2 | True if integers n1>n2 |
String tests | |
-z string | True if string has length zero |
-n string | True if string has non-zero length |
s1 = s2 | True if the 2 strings are equal |
s1 != s2 | True if the 2 strings are not equal |
string | True if string is not the empty string |
File and Directory tests | |
-r file | True if file exists and is readable |
-w file | True if file exists and is writable |
-x file | True if file exists and is executable |
-f file | True if file exists and is a regular file |
-d file | True if file exists and is a directory |
-s file | True if file exists and has non-zero size |
Boolean combinations of tests | |
! t1 | True if t1 is not true |
t1 -a t2 | True if both t1 and t2 are true |
t1 -o t2 | True if t1 or t2 is true |
case string in pattern-list ) command(s) ;; pattern-list ) command(s) ;; * ) # fall through command(s) ;; esac
#!/bin/sh # Demonstrate case set $* NoArgument case $1 in [aAeEiIoOuU] ) echo "That's a vowel." ;; *.html | *.htm ) echo "$1 is an HTML file" ;; NoArgument ) echo "Did you forget to provided an argument?" echo "How peaceful it is that way." ;; * ) echo "I've no idea what $1 is." ;; esac
for var in list do command(s) done while command(s)2go do command(s) done until command(s)2stop do command(s) done
#!/bin/sh # Demonstrate for if [ ! -d "$1" ]; then echo "I need a directory" ; exit 1; fi echo "In directory $1" for f in `ls $1/*.html` do echo "$f is an html file" done ##### Sample Run: % ./for.1 I need a directory % ./for.1 doc/ch1 In directory doc/ch1 doc/ch1/1.html is an html file doc/ch1/exercises.html is an html file doc/ch1/intro.html is an html file ######################################################## #!/bin/sh # Demonstrate while while read x do set $x echo "$#: $x" done ##### Sample Run: % ./while.1 < while.1 1: #!/bin/sh 3: # Demonstrate while 3: while read x 1: do 2: set $x 3: echo "$#: $x" 1: done ######################################################## #!/bin/sh # Demonstrate until until [ $# -eq 0 ] do echo "$#: $1 | \c" shift echo "$*" done ##### Sample Run: % ./until.1 this will go away 4: this | will go away 3: will | go away 2: go | away 1: away |
#!/bin/sh # while.2 -- Demonstrate while and && ## If the first argument is a file, process it [ -f "$1" ] && while read x y do echo "$y $x" done < $1 ##### Sample Runs: % cat T This is a simple file on which we can experiment. % ./while.2 % ./while.2 . % ./while.2 T is a simple This on which we can file experiment. % ######################################################## % cat until.2 #!/bin/sh # until.2 -- Demonstrate until and || until [ $# -eq 0 ] || [ $1 = "quit" ] do echo "$#: $1 | \c" shift echo "$*" done ##### Sample Runs: % ./until.2 what is going on 4: what | is going on 3: is | going on 2: going | on 1: on | % ./until.2 what is "going on" 3: what | is going on 2: is | going on 1: going on | % ./until.2 will we quit early 4: will | we quit early 3: we | quit early %
$ echo $i $ i=1+1 $ echo $i 1+1 $ i=`expr 1+1` $ echo $i 1+1 $ i=`expr 1 + 1` $ echo $i 2 $ i=`expr 7 * 6` expr: syntax error $ i=`expr 7 \* 6` $ echo $i 42 $ ################ $ j=`expr i - 40` expr: non-numeric argument $ j=`expr $i - 40` $ echo $j 2 $ ################ $ i=`expr \( 3 + 4 \) \* \( 12 / 2 \)` $ echo $i 42 $ k=`expr \( 3 + 4 \) \* \( 1$j / $j \)` $ echo $k 42 $