View
214
Download
0
Embed Size (px)
Citation preview
Further Shell Scripting
Michael GriffithsCorporate Information and Computing ServicesThe University of SheffieldEmail [email protected]
Outline
• Control Structures– Conditional statements– Looping statements– Switch, case statements– Do While loops
• Functions• A preview of Globus
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
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
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
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
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
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:
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
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
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
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
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
while statement - Bourne shell while this command execute successfully
do
this command
and this command
done
while test "$i" -gt 0
do
i=`expr $i - 1`
done
Example:
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
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
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
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
Functions
• Functions are declared at the beginning of a shell script and take the format shown below
functionname()
{
function script commands go here
}
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
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
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
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
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