using unix shell scripts

Post on 31-Dec-2015

32 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Using UNIX Shell Scripts

Michael GriffithsCorporate Information and Computing ServicesThe University of SheffieldEmail m.griffiths@sheffield.ac.uk

• 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

Introduction

• What is the shell?• Types of shell• Why write shell programs?

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

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

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

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

Creating and Executing a Shell Script

• Sample .cshrc script• Hello world script• Job submission script for SGE

#!/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`"

Sample .cshrc Shell Script

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"

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 username@shef.ac.uk#$ -cwd

benchtest inputfile > msgoutputfile

Submits the job benchtest to the sun gridengine queue

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

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

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

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

Command line parameters

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

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"

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"

top related