1 / 19

Lecture 5

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’.

moke
Download Presentation

Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5 More about Shell Script

  2. 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’

  3. #!/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"

  4. 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

  5. Example Result of dirtree.csh % dirtree | |------bin | | | |------.garbage | | | | | |------file1 | | | |------examples | | | | | |------arg | | |------average.csh | | |------fileinfo.csh | | | |------hw6.sh | |------lab3

  6. 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"

  7. 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

  8. 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

  9. 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

  10. 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

  11. Conditional Structures • We have already seen if. There is also switch. switch ( value ) case constant1: commands … breaksw case constant2: breaksw ……………………. endsw

  12. 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

  13. 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

  14. 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

  15. goto command • Jump to a label. (another way of loop) label: echo “have a rest” sleep 1 goto label

  16. #!/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

  17. 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

  18. 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 • Extremely good for text file handling • C • Full fledged programming language

  19. Recommended Reading • Chapter 10

More Related