190 likes | 337 Views
Lecture 5. More about Shell Script. Script Example 1. Task: create a game that generates a random number each time, and let the user guess. Each time when the user guess, tell the user the guessed value is large or small. End if the user guess correctly, Call the script ‘guess.sh’.
E N D
Lecture 5 More about Shell Script
Script Example 1 • Task: create a game that generates a random number each time, and let the user guess. Each time when the user guess, tell the user the guessed value is large or small. End if the user guess correctly, • Call the script ‘guess.sh’
#!/bin/tcsh -f echo "guess the number" set answer = $< set goal = `head -c 10 </dev/urandom |uuencode -m - | tail -n+2 |sed 's/[^0-9]//g' |head -c 3` #ignore this now. set count = 1 while ($answer != $goal ) if ($answer < $goal ) then echo "too small" else echo "too large" endif @ count ++ set answer = $< end echo "correct!" echo "using $count rounds"
Script Example 2 • 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.sh
Example Result of dirtree.csh % 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="." else set thisdir=$argv[1] endif if($?TREEPREFIX) then set prefix="$TREEPREFIX" else set prefix="" endif echo "$prefix|" set filelist=`ls -A $thisdir` foreach file ($filelist) echo "${prefix}|------${file}" if(-d "$thisdir/$file") then if($file == $filelist[$#filelist]) then setenv TREEPREFIX "${prefix} " else setenv TREEPREFIX "${prefix}| " endif $0 "$thisdir/$file" endif end echo "$prefix"
More on Conditionals • Recall the if command if ( expression ) then command endif • expression must be an actual booleanexpression (0-false, 1,2…-true) • -d directory • $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 #success exit … else if ($exitStatus == 1) then #false exit … … 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
Script Exit Status • How do we return an exit status for our scripts? • Use the exit command • 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: 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” breaksw • 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 Command • repeat number command • 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 endif end end
goto command • Jump to a label. (another way of loop) label: echo “have a rest” sleep 1 goto label
#!/bin/tcsh -f set goal = `head -c 10 </dev/urandom |uuencode -m - | tail -n+2 |sed 's/[^0-9]//g' |head -c 3` set round = 1 startover: echo "\ninput your guess: " set guess = $< if ( $guess < $goal ) then echo too small @ round += 1 gotostartover else if ($guess > $goal ) then echo too large @ round += 1 gotostartover else echo correct ! echo total round is $round endif
Pros and Cons of Shell Scripting • Quick programming solutions for simple problems • 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 • No complete data types
Seeking the Right Solutions • With increasing complexity of problems, solutions are: Shell script PerlC • Perl stands for Practical Extractions and Report Language • Retain the immediateness of shell script while having the flexibility of C • Extremely good for text file handling • C • Full fledged programming language
Recommended Reading • Chapter 10