CIS 240 Introduction to UNIX Instructor: Sue Sampson
CIS240 – UNIX Shell Programming Variables Control Structures Functions Commands Arithmetic expressions Getting back to a known state
CIS240 – UNIX Shell Programming Variables Places in memory used to hold values Can be initialized and altered Are represented by variable names Do not need to be declared before they are used (declaring means identifying the name and variable type)
CIS240 – UNIX Shell Programming Variable Names Follow the same convention as C names First character is upper or lower case a- z Next characters are upper or lower case letters, numbers, or underscores The maximum length depends on your version of UNIX
CIS240 – UNIX Shell Programming Variable Types Default is string May be declared as follows -u Upper Case -lLower Case -iInteger -rRead only -LLeft justified -RRight justified
CIS240 – UNIX Shell Programming Variable Example Execute these two commands. typeset –i x=5 echo $x echo x == returns x echo $x == returns the contents of variable x
CIS240 – UNIX Shell Programming Environment Variables Special variables that pertain to your environment Upper case letters HOMEContains the user’s home directory IFSInternal Field Separator PATHContains the full path used for commands PS1Primary prompt PS2Secondary prompt SHELLContains the pathname of the current shell
CIS240 – UNIX Shell Programming Decision (if) Format: if then else fi
CIS240 – UNIX Shell Programming File myscript if test -f a_test_file then echo “file exists” else echo “file does not exist” fi # sh myscript file does not exist
CIS240 – UNIX Shell Programming Results of TEST If true, the “then” code is executed If false, the “else” code is executed A zero value is true (reverse of C/C++) False is any value other than zero
CIS240 – UNIX Shell Programming Operators Used to formulate a test File Operators (tests characteristics of files) Arithmetic Operators (allows comparisons) Group Operators (groups operators together)
CIS240 – UNIX Shell Programming Selection (Case) Format: case in pattern1) statement-1;; pattern2) statement-2;; esac
CIS240 – UNIX Shell Programming File secondscript echo –n “Enter a word: “ read guessword case “$guessword” in fred) echo “guessword is fred” ;; bill | bert) echo “guessword is either bill or bert” ;; [S,s]*) echo “guessword starts with S or s” ;; xyzzy) echo “You have been playing adventure!” ;; *) echo “guessword was not matched” ;; Esac Sh secondscript Enter a word: Sue guess word starts with S or s
CIS240 – UNIX Shell Programming Looping (while) Format: while do done
CIS240 – UNIX Shell Programming File thirdscript while test –s test_file do echo “File test_file not of zero length” sleep 10 done Sh thirdscript File test_file not of zero length File test_file not of zero length…
CIS240 – UNIX Shell Programming Looping (for) Format: For in do done
CIS240 – UNIX Shell Programming File fifthscript for item in one two three do echo “Argument value is: $item” done sh fifthscript Argument value is: one Argument value is: two Argument value is: three
CIS240 – UNIX Shell Programming Two Methods for Saving a Shell Script Using cat (cat > script1.sh) Stores command line scripts as they are entered Does not allow for editing of scripts Using vi Allows scripts to be edited Scripts cannot be run from vi
CIS240 – UNIX Shell Programming Getting Back to a Known State Use d to exit out of a shell script d or exit will get you out of most problems Type bash to get you back into the bourne-again shell Log out and log back in (use logout if exit will not log you out)