05.08.2013 Views

Module 13: Shell Scripts in UNIX - E-Courses

Module 13: Shell Scripts in UNIX - E-Courses

Module 13: Shell Scripts in UNIX - E-Courses

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Operat<strong>in</strong>g Systems Prof. P. C. P. Bhat<br />

Indian Institute of Science Bangalore<br />

Operat<strong>in</strong>g Systems/<strong>Shell</strong> <strong>Scripts</strong> <strong>in</strong> <strong>UNIX</strong> Lecture Notes<br />

echo $0 /* $0 yields this script file name */<br />

Table <strong>13</strong>.3: A partial list of special variables.<br />

echo we now get the number of parameters that were <strong>in</strong>put<br />

echo $# /* yields the number of arguments */<br />

echo now we now get the first three parameters that were <strong>in</strong>put<br />

echo $1 $2 $3 /* The first three arguments */<br />

shift /* shift cmd could have arguments */<br />

echo now we now shift and see three parameters that were <strong>in</strong>put<br />

echo $1 $2 $3 /* Three arguments after one shift */<br />

A partial list of symbols with their respective mean<strong>in</strong>gs is shown <strong>in</strong> Table <strong>13</strong>.3.<br />

# file sh_1a.file<br />

# this is a file with only one echo command<br />

echo this l<strong>in</strong>e is actually a very long command as its arguments run \<br />

on and on and on Note that both this and the l<strong>in</strong>e above and below \<br />

are part of the command Also note how back slash folds commands<br />

In most shells, multiple commands may be separated on the same l<strong>in</strong>e by a semi-colon<br />

(;). In case the command needs to be folded this may be done by simply putt<strong>in</strong>g a back<br />

slash and carry<strong>in</strong>g on as shown.<br />

file sh_2.file: If a variable is not def<strong>in</strong>ed, no value is returned for it. However, one can<br />

choose to return an error message and check out if a certa<strong>in</strong> variable has <strong>in</strong>deed been<br />

def<strong>in</strong>ed. A user may even generate a suitable message as shown <strong>in</strong> scripts sh 2a and sh<br />

2b.<br />

# file sh_2.file<br />

# This is to f<strong>in</strong>d out if a certa<strong>in</strong> parameter has been def<strong>in</strong>ed.<br />

echo param is not def<strong>in</strong>ed so we should get a null value for param<br />

echo ${param}<br />

echo param was not def<strong>in</strong>ed earlier so we got no message.<br />

PCP Bhatt/IISc, Bangalore M<strong>13</strong>/V1/June 04/6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!