further shell scripting

26
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email [email protected]

Upload: grover

Post on 09-Feb-2016

48 views

Category:

Documents


3 download

DESCRIPTION

Further Shell Scripting. Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email [email protected]. Outline. Control Structures Conditional statements Looping statements Switch, case statements Do While loops Functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Further Shell Scripting

Further Shell Scripting

Michael GriffithsCorporate Information and Computing ServicesThe University of SheffieldEmail [email protected]

Page 2: Further Shell Scripting

Outline• Control Structures

– Conditional statements– Looping statements– Switch, case statements– Do While loops

• Functions• A preview of Globus

Page 3: Further Shell Scripting

Conditional Statements – Bourne Shellif command executes successfully then

execute command elif this command executes successfully then

execute this command and execute this command

else execute default command

fi

Page 4: Further Shell Scripting

Bourne shell condition test exampleif date | grep “Fri”then

echo “It’s Friday!” fi

if test “$1” = “Monday”then

echo “The typed argument is Monday.” fi

Testing strings or arithmetic expressions

Page 5: Further Shell Scripting

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

Page 6: Further Shell Scripting

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

Page 7: Further Shell Scripting

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

Page 8: Further Shell Scripting

Conditional statements c-shellif (condition(s)) then

command group 1else

command group 2endif

if( -e $ifile) then nedit $ifileelse echo The file $ifile does not exist!endif

Example:

Page 9: Further Shell Scripting

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 endifendif

Page 10: Further Shell Scripting

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

Page 11: Further Shell Scripting

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

Page 12: Further Shell Scripting

For loop – Bourne shellfor 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

Page 13: Further Shell Scripting

For loop – C shellforeach name (wordlist) command(s) ...end

Example: foreach dudfile(/home1/users/cs/*)

if (-z $dudfile || $dudfile == "core") then

rm $dudfile

endif

end

Page 14: Further Shell Scripting

while statement - Bourne shell while this command execute successfullydo

this commandand this command

done

while test "$i" -gt 0

do

i=`expr $i - 1`

done

Example:

Page 15: Further Shell Scripting

While – c shellwhile (condition)

statements

end

Example: while($i > 0 )

echo $i Sent to printer! #Might send the file to a printer

set i=`expr $i - 1`

end

Page 16: Further Shell Scripting

Selecting From a List of Possibilities• Switch … case

– Bourne shell– c-shell

Page 17: Further Shell Scripting

Switch.. Case – Bourne shellcase argument in

pattern 1) execute this command

and this

and this;;

pattern 2) execute this command

and this

and this;;

esac

Page 18: Further Shell Scripting

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

Page 19: Further Shell Scripting

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

Page 20: Further Shell Scripting

Functions• Functions are declared at the beginning of a shell

script and take the format shown belowfunctionname(){

function script commands go here}

Page 21: Further Shell Scripting

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

Page 22: Further Shell Scripting

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

Page 23: Further Shell Scripting

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

Page 24: Further Shell Scripting

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

Page 25: Further Shell Scripting

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

Page 26: Further Shell Scripting

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