1 / 18

Using UNIX Shell Scripts

Using UNIX Shell Scripts. Michael Griffiths Corporate Information and Computing Services The University of Sheffield Email m.griffiths@sheffield.ac.uk. Presentation Outline. Introduction Creating and Executing Shell Script Defining and accessing shell Variables

Télécharger la présentation

Using UNIX Shell Scripts

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

  2. Presentation Outline • 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

  3. Introduction • What is the shell? • Types of shell • Why write shell programs?

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

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

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

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

  8. Creating and Executing a Shell Script • Sample .cshrc script • Hello world script • Job submission script for SGE

  9. Sample .cshrc Shell Script #!/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 wrsmg@maxima.leeds.ac.uk' alias lsx509 'ls -l /tmp/x509* | grep `whoami`' echo "Welcome to Titania `whoami`"

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

  11. Sun Grid Engine Job Submission Script Submits the job benchtest to the sun gridengine queue #!/bin/sh #First simple job script for sun grid engine. # #$ -l h_cpu=01:00:00 #$ -m be #$ -M username@shef.ac.uk #$ -cwd benchtest inputfile > msgoutputfile

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

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

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

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

  16. Command line parameters • $0 Name of script • $1, $2, ….. $n 1st, 2nd 3rd command line parameter • $# Number of command line parameters

  17. 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 $< • Example echo "Please enter the name of the job:" set jobname=$< echo "Executing, $jobname"

  18. Arithmetical operations • c-shell set i1=10 set 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 shell i1=2 j1=6 k1=`expr $i1 \* $j1` #Using backslash to take * operator literally echo "The multiple of $i1 and $j1 is $k1"

More Related