Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 240 Introduction to UNIX Instructor: Sue Sampson.

Similar presentations


Presentation on theme: "CIS 240 Introduction to UNIX Instructor: Sue Sampson."— Presentation transcript:

1 CIS 240 Introduction to UNIX Instructor: Sue Sampson

2 CIS240 – UNIX Shell Programming Variables Control Structures Functions Commands Arithmetic expressions Getting back to a known state

3 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)

4 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

5 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

6 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

7 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

8 CIS240 – UNIX Shell Programming Decision (if) Format: if then else fi

9 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

10 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

11 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)

12 CIS240 – UNIX Shell Programming Selection (Case) Format: case in pattern1) statement-1;; pattern2) statement-2;; esac

13 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

14 CIS240 – UNIX Shell Programming Looping (while) Format: while do done

15 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…

16 CIS240 – UNIX Shell Programming Looping (for) Format: For in do done

17 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

18 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

19 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)


Download ppt "CIS 240 Introduction to UNIX Instructor: Sue Sampson."

Similar presentations


Ads by Google