1 / 26

Further Shell Scripting

Further Shell Scripting. Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk. Outline. Control Structures Conditional statements Looping statements Switch, case statements Do While loops Functions

grover
Download Presentation

Further Shell Scripting

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. Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk

  2. Outline • Control Structures • Conditional statements • Looping statements • Switch, case statements • Do While loops • Functions • A preview of Globus

  3. Conditional Statements – Bourne Shell if command executes successfully then execute command elif this command executes successfully then execute this command and execute this command else execute default command fi

  4. Bourne shell condition test example if date | grep “Fri” then echo “It’s Friday!” fi Testing strings or arithmetic expressions if test “$1” = “Monday” then echo “The typed argument is Monday.” fi

  5. Bourne shell string comparisons used with test • string1 = string2 Test identity • string1 !=string2 Test inequality • string Return 0 exit status is string is not null • -n string Return 0 exit status is string is not null • -z string Return 0 exit status is string is null

  6. Bourne shell arithmetic comparison operations used with test • int1 –eq int2 Test identity • int1 –ne int2 Test inequality • int1 –lt int2 Less than • int1 –gt int2 Greater than • int1 –le int2 Less than or equal • int1 –ge int2 Greater than or equal

  7. Bourne shell combining tests using logical operators || (or) and && (and) if date | grep “Fri” && test `date +’%H’` -gt 17 then echo “It’s Friday, it’s hometime!!!” fi

  8. Conditional statements c-shell if (condition(s)) then command group 1 else command group 2 endif Example: if( -e $ifile) then nedit $ifile else echo The file $ifile does not exist! endif

  9. Arithmetic comparison example for the c-shell if ( ( ($1 % 5) == 0) && ( ($1 % 3) == 0) ) then echo You entered a multiple of 3 and 5! else if( ( $1 % 5) == 0)then echo You entered a multiple of 5! else if ( ($1 % 3)== 0) then echo You entered a multiple of 3! else echo Not divisible by 3 or 5 endif endif endif

  10. File enquiry operations with the bourne shell and c-shell -d file Test if file is a directory -f file Test if file is not a directory -s file Test if the file has non zero length -r file Test if the file is readable -w file Test if the file is writable -x file Test if the file is executable -o file Test if the file is owned by the user -e file Test if the file exists -z file Test if the file has zero length

  11. Looping Statements • For Loops • Bourne shell for, in, do, done structure • C shell foreach, end structure • Conditional loops • Bourne shell while, do, done • C shell while(condition), end

  12. For loop – Bourne shell for name in name_1 name_2 name_3 ….. name_n do command(s) ... done Example: for i in 3 7 do echo " $i * 5 is `expr $i \* 5` " done

  13. For loop – C shell foreach name (wordlist) command(s) ... end Example: foreach dudfile(/home1/users/cs/*) if (-z $dudfile || $dudfile == "core") then rm $dudfile endif end

  14. while statement - Bourne shell while this command execute successfully do this command and this command done Example: while test "$i" -gt 0 do i=`expr $i - 1` done

  15. While – c shell while (condition) statements end while($i > 0 ) echo $i Sent to printer! #Might send the file to a printer set i=`expr $i - 1` end Example:

  16. Selecting From a List of Possibilities • Switch … case • Bourne shell • c-shell

  17. Switch.. Case – Bourne shell • case argument in • pattern 1) execute this command • and this • and this;; • pattern 2) execute this command • and this • and this;; • esac

  18. Switch …case – Bourne shell example case "$1" in *".txt") ls "$1" && cp "$1" txt && echo "$1 moved to txt directory";; *".tmp") ls "$1" && cp "$1" tmp && echo "$1 moved to tmp directory";; esac

  19. Switch … case – c-shell switch($1) case *".txt": ls "$1" && cp "$1" txt && echo "$1 moved to txt directory" breaksw case *".tmp": ls "$1" && cp "$1" tmp && echo "$1 moved to tmp directory" breaksw endsw

  20. Functions • Functions are declared at the beginning of a shell script and take the format shown below functionname() { function script commands go here }

  21. Functions • Functions called using the name of the function • e.g. help • Variables passed to a function using • calculate 3 5 • The variables 3 and 5 will be passed to the function calculate. These will be parameter $1 and $2 respectively for the function

  22. Functions • With the exception of command line variables all variables have global scope • Do not recognise the variables passed in from the command line • Use Functions to increase • reusability • Readability and ease of debugging

  23. A Preview The Globus Toolkit – Middleware for grids • Grid job submission • GRAM (Globus resource allocation manager) • Resource discovery and information • GIS (Grid information service a metacomputiing directory service using LDAP) • Secure Access • Grid security infrastructure • File Management • Grid ftp

  24. Globus Examples • grid-proxy-init • globus-job-run maxima.leeds.ac.uk /bin/echo Hello world • globus-url-copy gsiftp://maxima.leeds.ac.uk$LEEDSHOME/$1 file:$PWD/$2 • Use globus commands to create grid shells scripts • Later on

  25. Staging a Job • Useful for running a series of commands on a remote node • globus-job-run maxima.leeds.ac.uk –s /path/script.sh arguments • Script should not return an exit value

  26. Examples • Executing UNIX commands on remote nodes • Querying Sun Grid engine on remote nodes • Starting X-Applications • Staging your shell scripts

More Related