Lab Instructions: B. 11 Bourne Shell Scripts
Lab Instructions: B. 11 Bourne Shell Scripts
SHELL SCRIPTING
1. UNIX shell and How to Change your Shell
A UNIX shell is a kernel working environment. There are many UNIX shells in common use
such as bash, ksh, csh, and tcsh. !any scienti"ic a##lications in A$S #ackage s#ace
such as GaussView, Cerius2, SYBYL, Cambridge Database etc. work only in the
C shell environment which is either csh or tcsh.
To know which shell you are currently use ty#e at the #rom#t on any o" the servers
echo $SHELL
The de"ault shell "rom the %N&'N we(site is ksh. To change to csh/tcsh go to the %N&'N
we(site select the )
rd
(utton *Change name or shell+ login and then "ollow the
instructions.
,. I" you have not done it yet create your own working directory on /netscr/YourOnyen
where *YourOnyen+ is your own %N&'N I-. %therwise go to the ne.t ste#.
mkdir /netscr/yourONYEN
-% N%T make other directories at the to# /netscr level.
). Co#y hands/on e.ercise shell scri#ts and codes "rom /netscr/training/shell to your
own scratch directory0
cp /netscr/training/shell/* /netscr/yourONYEN
B. 11 BOURNE SHELL SCRIPTS
1. The sim#lest Hello scri#t1
#!/bin/sh
echo "Hello, $LOGNAME!"
echo "Current date is `date`"
echo "ser is `!hoa"i`"
echo "Current director# `$!d`"
,. Summation o" two integers
213(in3sh
i% & $# 'ne ( )
then
echo "sa*e ' $+ , #"
echo "-here , and # are t!o inte*ers %or !hich . !ill $rint su""
1
e,it /
%i
su"0`e,$r $/ 1 $( `
echo "2u" o% $/ and $( is $su" "
). Summation o" two real num(ers
#!/bin/sh
a03455
b06457
c0`echo $a 1 $b 8 bc`
echo "$a 1 $b 0 $c"
4. Scri#t to "ind out the (iggest num(er in ) integers
#!/bin/sh
i% & $# 'ne 9 )
then
echo "$+: nu"ber/ nu"ber( nu"ber9 are not *i;en" <=(
e,it /
%i
n/0$/
n(0$(
n90$9
i% & $n/ '*t $n( ) == & $n/ '*t $n9 )
then
echo "$n/ is >i*est nu"ber"
eli% & $n( '*t $n/ ) == & $n( '*t $n9 )
then
echo "$n( is >i*est nu"ber"
eli% & $n9 '*t $n/ ) == & $n9 '*t $n( )
then
echo "$n9 is >i*est nu"ber"
eli% & $/ 'e? $( ) == & $/ 'e? $9 ) == & $( 'e? $9 )
then
echo "All the three nu"bers are e?ual"
else
echo ". can not %i*ure out !hich nu"ber is bi*er"
%i
5. %#eration 6summation su(traction multi#lication and division7 o" two integers
#!/bin/sh
i% test $# 0 9
then
case $( in
1@ let A0$/1$9BB
'@ let A0$/'$9BB
/@ let A0$//$9BB
,8C@ let A0$/D$9BB
,
D@ echo -arnin* ' $( in;alid o$erator, onl# 1,',,,/ o$erator allo!ed
e,itBB
esac
echo Ans!er is $A
else
echo "sa*e ' $+ ;alue/ o$erator ;alue("
echo " -here, ;alue/ and ;alue( are nu"eric ;alues"
echo " o$erator can be 1,',/,, EFor Multi$lication@"
%i
8. Scri#t to reverse a given #ositive integer
#!/bin/sh
i% & $# 'ne / )
then
echo "sa*e: $+ nu"ber"
echo " . !ill %ind re;erse o% *i;en $ositi;e inte*er"
echo " For e*4 $+ /(9, . !ill $rint 9(/"
e,it /
%i
n0$/
re;0+B sd0+
!hile & $n '*t + )
do
sd0`e,$r $n G /+`
re;0`e,$r $re; HD /+ 1 $sd`
n0`e,$r $n / /+`
done
echo "Ie;erse nu"ber is $re;"
)