200 likes | 216 Views
This lecture covers the if conditional in shell programming, along with the test statement to evaluate expressions. It also explains the case conditional and introduces the expr command for arithmetic and string manipulation. The lecture concludes with an overview of the while and until looping constructs.
E N D
CS 497C – Introduction to UNIXLecture 33: - Shell Programming Chin-Chih Changchang@cs.twsu.edu
The if Conditional • The if statement takes two-way decisions depending on the fulfillment of a certain condition. • There are three forms of the if statement: • if condition then commands else commands fi • if condition then commands fi • if condition then commands elif condition then commands else commands fi $ emp3.sh firewall
The if Conditional $ emp3.sh mail $ emp3a.sh firewall $ emp3a.sh mail • We saw two forms of the if conditional – if-then-fi and if-then-else-fi. There’s a third form – if-then-elif-then-else-fi. $ cronfind.sh “find */”
Test and []: Companions of if • When you utilize if to evaluate expressions, the test statement is often used as its control command. • test ueses certain operators to evaluate the condition on its right and returns either a true or false exit status, test works in three ways: • Compare two numbers. • Compare two strings or a single one for null value. • Checks a file’s attributes.
Test and []: Companions of if • Test doesn’t display any output but simply returns a value that sets the parameter $?. • The numerical comparison operators used by test are: • -eq: equal to • -ne: not equal to • -gt: greater than • -ge: greater than or equal to • -lt: less than • -le: less than or equal to
Test and []: Companions of if $ cat arg_number_check.sh #!/bin/sh if test $# -ne 3 ; then echo "You didn't enter three arguments" else echo "You entered the right number" fi • The test statement has a shorthand – a pair of rectangular brackets.
Test and []: Companions of if • The following two forms are equivalent: test $x –eq $y [$x –eq $y] • Test can be used to compare strings with the following operations: • s1 = s2: true if s1 = s2 • s1 != s2: true if s1 is not equal to s2 • stg: true if stg is assigned and not null • -n stg: true if stg is not a null string
Test and []: Companions of if • -z stg: true if stg is a null string • s1 == s2: true if s1 = s2 (korn and bash only) • The compile.sh script is used to compile the last modified c or java programs. • test can be used to test various file attributes (Page 554 Table 18.4): • -f fname: fname exists and is a regular file. • -f fname: finame exists and is readable. • -w fname: fname exists and is writable.
Test and []: Companions of if #!/bin/sh if [ $# -eq 1 ] ; then if [ $1 = "j" ] ; then file=`ls -t *.java | head -1` javac $file elif [ $1 = "c" ] ; then file=`ls -t *.c | head -1` cc $file && a.out else echo "Invalid file type" fi else echo "Usage: $0 file_type\nValid file types are c and j" fi
Test and []: Companions of if • -x fname: fname exists and is executable. • -d fname: finame exists and is a directory. • The ! negates a test, so [! –w file] negates [-w file]. • Check the file filetest.sh. $ filetest.sh emp3.lst $ filetest.sh emp.lst
The case Conditional • case is a compact string-matching construct and is closed with esac. • It uses the shell’s wild cards to match multiple patterns in egrep-style. • The * when used as the last option matches everything not matched by the previous options. The wild cards match strings and not files. • case is specially suitable for matching the filename $0.
The case Conditional • Here is its syntax: case expression in pattern1) commands1;; pattern2) commands2;; …. esac • case first matches expression with pattern1. If the match succeeds, then it executes commands1. Otherwise, then go to pattern2.
The case Conditional #!/bin/sh tput clear echo "\n 1. Find files modified in last 24 hours\n 2. The free disk space\n 3. Space consumed by this user\n 4. Exit\n\n SELECTION: \c" read choice case $choice in 1) find $HOME -mtime -1 -print ;; 2) df ;; 3) du -s $HOME ;; 4) exit ;; *) echo "Invalid option" esac
Expr: Computation and String Handling • expr is used for integer computation and string manipulation. It is used with the Bourne shell for incrementing the value of a variable. • This command combines two functions in one: • Performs arithmetic operations on integers. • Manipulates strings.
Expr: Computation and String Handling • The + (add), - (subtract), * (multiply), / (divide), and % (divide and truncate the decimal portion). $ x=3; y=5; expr $x + $y; • expr uses regular expressions to extract a substring, locate the position of a character, and evaluate the length of a string. • Korn and bash shells don’t need expr. • sleep specifies the number of seconds for which the shell will pause.
While and until: Looping • The while loop executes its body as long as the control command returns a true value. • It is used in scripts that repeatedly increment the value of a variable or provide multiple chances to a user. • You can set up an infinite loop using true as the control command. • The until loop complements while.
While and until: Looping • The syntax of the while command is: while condition is true do commands done while [! –r invoice.lst] do sleep 60 done
While and until: Looping until [–r invoice.lst] do sleep 60 done • for works with each element of a list at a time. The list can be generated by variables, wild cards, positional parameters and command substitution.
for: Looping with a List • The syntax of this construct is as follows: for variable in list; do commands done for file in chap*; do cp $file ${file}.bak echo $file copied to $file.bak done
for: Looping with a List for file in *.c; do cc -o ${file}.o $file done • All loops use the keywords do and done. • The break statement terminates a loop, while continue starts the next iteration.