Review The Unix Shells Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003.
Reading from Standard Input Command read Reads 1 line Examples $ read v1 v2 one two $ echo $v1 one $ echo $v2 two $ read v1 v2 one two three four $ echo $v1 one $ echo $v2 two three four
Arithmetic Bourne shell does not directly do math Command expr evaluates expressions Supports Multiplication (\*), Division (/), Remainder (%) Add (+), Subtract (-) Equal (=), Not Equal (!=) Less (<), Less/Eq (<=), Greater (>), Greater/Eq (>=) And (&), Or (|) Index (locate substring) Match
Examples: Integer Operands
Environment Variables $HOME : full pathname of home directory $PATH : list of directories to search for commands $MAIL : the full pathname of mailbox $USER : your username $SHELL : the full pathname of login shell $TERM : the type of your terminal
Built-In Variables $$ the process ID of the shell $0 the name of the shell script $1..$9 $n refers to the nth command line argument $* a list of all command-line arguments
Example
Test Command test expression [ expression ] (equivalent form on some UNIX: if it’s built-in) Returns a zero if expression evaluates to true Otherwise, returns a nonzero status. Examples: $ test 2 -eq 3; $ test -e for.sh; $ test abc = abc;
test Command Expression: Files
test Command Expression: Strings, Integers, Complex Expressions
Case Structure case expression in pattern) list ;; pattern2) list2 ... *) # default list_n esac
Control Structures: for .. do .. done for name [in {word}* ] do list done Loops the value of name through each word in the word list, evaluates commands in the list at each iteration. If no word list is supplied, $@ is used. break : terminate the loop immediately. continue : jump to the next iteration.
Control Structures: if .. then .. fi if list1 then list2 elif list3 #may be repeated several times list4 else #may occur zero or one time list5 fi Commands in list1 are executed. If last command succeeds, the list2 is executed. Otherwise, a successful command list in elif causes the associated then to be executed.
Control Structures: until .. do .. done until list1 do list2 done Executes list1 If the last command in list1 succeeds, it ends. Otherwise, list2 is executed and the process is repeated. If list2 is empty, the do keyword should be omitted. break continue
Control Structures: while .. done while list1 do list2 done Executes list1. If the last command in list1 fails, it ends Otherwise, list2 is executed and the process is repeated. If list2 is empty, the do keyword should be omitted. break continue