Download - Using UNIX Shell Scripts
![Page 1: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/1.jpg)
Using UNIX Shell Scripts
Michael GriffithsCorporate Information and Computing ServicesThe University of SheffieldEmail [email protected]
![Page 2: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/2.jpg)
• Introduction• Creating and Executing Shell Script• Defining and accessing shell Variables• User Input during Shell Script Execution• Arithmetical operations on Shell variables• Control Structures• Defining ad Using Functions• Examples• References
Presentation Outline
![Page 3: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/3.jpg)
Introduction
• What is the shell?• Types of shell• Why write shell programs?
![Page 4: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/4.jpg)
What is the shell?
• Provides an Interface to the UNIX Operating System• It is a command interpreter
– Built on top of the kernel– Enables users to run services provided by the UNIX OS
• A series of commands
![Page 5: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/5.jpg)
Types of Shell
• Bourne Shell (/bin/sh)• C Shell (/bin/csh)• Korn Shell (/bin/ksh)• Bash (Bourne Again Shell) (/bin/ksh)• T shell by default (/bin/tcsh)
– Used on Titania– Enhanced C shell
![Page 6: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/6.jpg)
Shell Differences
• Bourne shell has fewer interactive features– E.g. no user input
• C shell – More C like in syntax and structure– Allows user input
![Page 7: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/7.jpg)
Why write shell programs?
• Run tasks customised for different systems• Write programs for controlling jobs run an a system• Write scripts submitted by a scheduler
![Page 8: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/8.jpg)
Creating and Executing a Shell Script
• Sample .cshrc script• Hello world script• Job submission script for SGE
![Page 9: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/9.jpg)
#!/bin/csh#First execute the titania cshrc script.
source /local/shef/cshrc
setenv PATH $PATH":/opt/globus/bin"setenv GLOBUS_INSTALL_PATH /opt/globus/bin
alias hi 'history'alias max 'ssh maxima.leeds.ac.uk -l wrsmg'alias sftl 'sftp [email protected]'alias lsx509 'ls -l /tmp/x509* | grep `whoami`'
echo "Welcome to Titania `whoami`"
Sample .cshrc Shell Script
![Page 10: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/10.jpg)
Hello World Shell Script
#!/bin/csh -f# Program 2a. Shell script illustrating the use of # labels and the goto statement.
echo "Hello World! " # characters taken literally except $, ` echo "My name is `whoami`" # `` backquotes enclose executable statements
goto label1
echo "Before label1" #This line is never reached and is never echoed
label1: echo "After label1"
![Page 11: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/11.jpg)
Sun Grid Engine Job Submission Script
#!/bin/sh#First simple job script for sun grid engine.
##$ -l h_cpu=01:00:00#$ -m be#$ -M [email protected]#$ -cwd
benchtest inputfile > msgoutputfile
Submits the job benchtest to the sun gridengine queue
![Page 12: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/12.jpg)
Shell Script Features
• Program starts with #!/bin/sh• Comment lines start with #
– Make code readable by including comments
• Tell UNIX that a script file is executable– chmod u+x scriptfilename
• Execute file and get runtime output, i.e. debugging mode– sh –x scriptfilename
![Page 13: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/13.jpg)
Commenting
• Identify who wrote a file and when• Identify input variables• Make code easy to read• Explain complex code sections• Version control tracking• Future modifications
![Page 14: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/14.jpg)
Use of quotation marks
• ‘ ‘ Single quotes enclosed characters taken literally– ls ‘>file>’ lists the file called >file>
• “ “ Double quotes enclosed characters taken literally except $ ` (backquote) and \– echo “$SHELL” the output may be /bin/tcsh
• ` ` Backquote encloses executable commands– echo “My name is `whoami` “ the output is My name is
cs1mkg
![Page 15: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/15.jpg)
Define and access shell variables
• c-shell– set a = hello– set maximum=20– set b=“hello world”
• bourne shell– a=hello– b=“hello world”– maximum=20
• Display a variable in the bourne and c-shell– echo $a
![Page 16: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/16.jpg)
Command line parameters
• $0 Name of script• $1, $2, ….. $n 1st, 2nd 3rd command line parameter• $# Number of command line parameters
![Page 17: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/17.jpg)
User Input During Shell Script Execution
• Main problem with bourne shell not very interactive there is no user input!
• User input with the c-shell uses the special variable $<
• Exampleecho "Please enter the name of the job:"set jobname=$<echo "Executing, $jobname"
![Page 18: Using UNIX Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022082411/568130ed550346895d970f06/html5/thumbnails/18.jpg)
Arithmetical operations
• c-shellset i1=10set j1=3@ k1 = $i1 + $j1 #Note:The space between @ and k1 is important!echo "The sum of $i1 and $j1 is $k1“
• Bourne shell# The @ operator does not work for the Bourne shelli1=2j1=6k1=`expr $i1 \* $j1` #Using backslash to take * operator literallyecho "The multiple of $i1 and $j1 is $k1"