Lecture 5 More about Shell Script
Script Example Task: Write a script that accepts an optional command line parameter – a directory name and prints the file system structure under this directory in the form of a tree. Without parameters, it should start in the current directory. Call this script dirtree.csh
Example Result of dirtree.csh % dirtree % dirtree||------bin | | | | garbage | | | | | |------file1 | | | |------examples | | | | | |------arg | | |------average.csh | | |------fileinfo.csh | | | |------hw6.sh | |------lab3
Script: dirtree.csh #!/bin/csh -f if($#argv == 0) then set thisdir="." set thisdir="."else set thisdir=$argv[1] set thisdir=$argv[1]endif if($?TREEPREFIX) then set prefix="$TREEPREFIX" set prefix="$TREEPREFIX"else set prefix="" set prefix=""endif echo "$prefix|" set filelist=`ls -A $thisdir` foreach file ($filelist) echo "${prefix}|------${file}" echo "${prefix}|------${file}" if(-d "$thisdir/$file") then if(-d "$thisdir/$file") then if($file == $filelist[$#filelist]) then setenv TREEPREFIX setenv TREEPREFIX "${prefix} " else else setenv TREEPREFIX setenv TREEPREFIX "${prefix}| " endif endif $0 "$thisdir/$file" $0 "$thisdir/$file" endif endifend echo "$prefix"
More on Conditionals Recall the if command if ( expression ) then commandendif expression must be an actual boolean expression -d directory -d directory $x > $y $x > $y
More on Conditionals Problem: we want an expression like: “execute a command cmd and then do something based on the exit status of the command”? Solution: store status of cmd in variable set exitStatus = cmd if ($exitStatus == 0) then … else if ($exitStatus == 1) then ……endif
The Status Variable We don’t actually have to assign the exit status to a variable. The shell does this for us. $status is the exit status of the last command executed cmd if ($status == 0) then … else if ($status == 1) then ……endif
A Shortcut If we only care about whether cmd exited successfully or not, we don’t even need to use the $status variable if { ( cmd ) } then...else…endif
Evaluating Commands Surround the command with { ( ) } if { ( cmd ) } Evaluates to true iff exit status of cmd is 0
Script Exit Status How do we return an exit status for our scripts? Use the exit command exit 0, exit 1, etc. exit 0, exit 1, etc. Convention is the exit status of 0 means normal exit and any other exit status means abnormal exit
Conditional Structures We have already seen if. There is also switch. switch ( value ) case constant1: commands … breaksw case constant2: case constant2: breaksw breaksw ……………………. …………………….endsw
Notes on Switch If a variable is used as the value, surround the variable with double quotes switch ( “$status” ) Fall through is allowed case 1: case 2: echo “1 or 2” break Default case is allowed
More on Looping Structures Loops can be nested break stops execution of the current innermost loop continue begins the next iteration of the current innermost loop Another loop! repeat repeat
Repeat Command repeat number command repeat 3 echo hello repeat 3 echo hello repeat can be used to break out of more than one loop while ( $x > y ) … while ( $a > $b ) … if ( $c == 1 ) then repeat 2 break endifendend
Interrupt Handling When a script is executing and an interrupt (Ctrl-c) occurs, the script exits Use the onintr command to do something (like clean up temporary files) before exiting onintr label onintr label jump to label and execute commands therejump to label and execute commands there onintr – onintr – ignore interruptsignore interrupts
Onintr Example onintr cleanup script body goes here cleanup: onintr – clean up code goes here
Pros and Cons of Shell Scripting Quick programming solutions for simple problems Shell is an interpreter (no compilation compared with C) Shell is an interpreter (no compilation compared with C) Programming difficult things in shell is an abuse and ends up clumsy Compared with C, shell doesn’t allow subroutines and functions Compared with C, shell doesn’t allow subroutines and functions No complete data types No complete data types
Seeking the Right Solutions With increasing complexity of problems, solutions are: shell script Perl C Perl stands for Practical Extractions and Report Language Retain the immediateness of shell script while having the flexibility of C Retain the immediateness of shell script while having the flexibility of C Extremely good for text file handling Extremely good for text file handling C Full fledged programming language Full fledged programming language
Recommended Reading Chapter 10 Review Something Fun!! Something Fun!!