experimentno.2
TRANSCRIPT
-
7/31/2019 ExperimentNo.2
1/7
Ope rating System Design Laborat ory
1 | P a g e
Exper im ent N o. 3
Exper im ent N um ber: 2
TITLE: Shel l Scr ipt ing- M enu Driven Program.
OBJECTIVE: To s tudy shel l sc r ip t and m enu dr iven program .
Theory: Shel l Scr ipt : No rm al ly shel ls are int eract ive. It m eans shel l accept com m and f rom you(v ia keyboard) and execute t hem . But i f you use com m and one b y one (sequence of 'n ' num berof commands) , the you can s tore th is sequence of command to tex t f i le and te l l the shel l toexecute this text f i le instead of enter ing the commands. This is known as shel l scr ipt . Shel lScr ipt is ser ies of com m and w ri t t en in plain te xt f i le.This m anual is m eant as a br ief int r odu ct ionto feat ures found in Bash.
Exit Statu sBy defaul t in Linux i f part icular command/shel l scr ipt is executed, i t return two type of valuesw hich is used to see wh eth er com m and o r shel l scr ipt executed is successful or n ot .
(1) I f retu rn valu e is zero (0), com m and is successful.(2) If ret urn value is nonzero, comm and is not successful or som e sort of er ror execut ingcomm and/ shel l sc r ip t .
This value is know n as ex i t stat us.But ho w to f ind o ut ex i t stat us of com m and o r shel l scr ipt ? Inorder to det erm ine th is ex i t Stat us you can use $? special var iable of shel l .
For e.g. (This examp le assum es th at unkn ow n1f i le doesn t n ot ex is t on your hard d r ive)$ rm unknow1file
I t w i ll show error as fo l low srm: cannot remove `unkowm1file': No such file or directory
and af ter that i f you g ive com m and
$ echo $?I t w i ll p r in t no nzero va lue to ind icate er ror .
User defined var iables (UD V)
To def ine UDV use fol low ing synt axSyntax:
variable name=value
' value' is ass igned t o given 'var iable nam e' and Value m ust be o n r ight s ide = sign.
Example:
To def ine var iable cal led n having value 10$ n=10
To pr int or access UDV use fol low ing syntaxSyntax:
$variablename
-
7/31/2019 ExperimentNo.2
2/7
Ope rating System Design Laborat ory
2 | P a g e
Exper im ent N o. 3
$ n=10
To p r in t conta ins o f var iab le 'n ' t ype com m and as fo l low s$ echo $n
About Q uote s
There are th ree types of quo tes :
Q uote s Na m e M e a n ing
"Double
Quotes
"Double Quot es" - Any th ing enclose in do uble quo tes removed m eaning
of th at characters (except \ and $).
'Singlequo tes
'Single quo tes ' - Enclosed in s ingle q uot es rem ains unchanged.
` Back quot e `Back quo te` - To execute com m and
Example:$ echo " Tod ay is date"Can' t pr int m essage wi t h to day 's date.
$ echo " Tod ay is date`" .
Rules for Na m ing var iable nam e (Both UD V and System Variable)
(1) Variable nam e m ust begin w i th Alphanum eric character or und erscore character (_),fol lowed by one or more Alphanumeric character. For e.g. Val id shel l var iable are asfo l lows :
H O M ESYSTEM _V ERSION
(2) Don' t put spaces on ei ther side of the equ al sign w hen ass igning value to var iable. Fore.g. In fol low ing var iable declarat ion t here w i l l be no er ror
$ no=10But there w i l l be prob lem for any of t he fo l low ing var iab le declarat ion:$ no =10$ no= 10
$ no = 10(3) Variables are case-sensit ive, just l ike f i lenam e in Linux.(4) You can def ine NULL variable as fol lo w s (NULL variable is variable wh ich has no value at
the t ime of def in i t ion) For e .g .$ vech=
$ vech=""Try to p r int i t 's value by issuing fol low ing com m and
-
7/31/2019 ExperimentNo.2
3/7
Ope rating System Design Laborat ory
3 | P a g e
Exper im ent N o. 3
$ echo $vechNot hing w i l l be show n b ecause var iable has no value i .e. NULL var iable.
(5) Do not use ?,* e tc , to name your var iab le names.
Shel l Ar i thm etic
Use to per fo rm ar i thm et ic operat ions .Syntax:
expr op1 m ath -opera to r op2
Examples:$ expr 1 + 3$ expr 2 - 1$ expr 10 / 2$ expr 20 % 3$ e xp r 1 0 \ * 3
$ echo `expr 6 + 3`No te :
expr 20 %3 - Rem ainder read as 20 mod 3 and rem ainder is 2.expr 10 \ * 3 - M ul t ip l i cat ion use \ * and not * since i t s w i ld card.
System Var iable M eaning
BASH=/ b in / bash Our shel l nam e
BASH_VERSION=1.14.7(1) Our shel l vers ion n am e
H O M E= / h o m e / r aja n Our hom e d i rec tory
LOGNAM E=students Stu dent sour logging nam e
OSTYPE=Linux Our OS type
PATH=/ usr / b in:/ sb in:/ b in:/ usr / sb in Our path set t ings
P W D = / h o m e / s t u d e n t s / C o m m o n Our current w ork ing d i rec tory
SHELL=/ bin/ bash Our shel l nam e
You can p r int an y of t he abo ve var iables cont ains as fol low s:
$ echo $HOM E
t est com ma nd or [ expr ]
-
7/31/2019 ExperimentNo.2
4/7
Ope rating System Design Laborat ory
4 | P a g e
Exper im ent N o. 3
test com m and or [ expr ] is used to see if an expression is t ru e, and i f i t is t rue i t ret urn zero(0),o therw ise re tu rns nonzero for fa lse.Syntax:
te st expre ssion O R [ expr ession ]
Example:Fol low ing sc r ip t determ ine w hether g iven argument num ber i s posi t i ve.
i f test $1 -gt 0t hen
echo "$1 num ber is pos i t ive"f i
tes t or [ expr ] w orks w i th
1. In teger ( Num ber w i thout dec im al po in t )2. Fi le ty pes3. Charact er strings
For M athem at ics, use fol low ing operat or in Shel l Scr ipt
M a the m a t i ca l
Ope rator in Shel lScript
M e a n in g N o rm a l
M a the m a t i ca lSta tements
But in Shell
For t ests ta tementw i t h if
command
For [ expr ]st a tement w i t hi f comm and
-eq is equal t o 5 == 6 i f test 5 -eq 6 i f [ 5 -eq 6 ]
-ne is not equal to 5 != 6 i f test 5 -ne 6 i f [ 5 -ne 6 ]
-lt is less th an 5 < 6 i f test 5 - l t 6 i f [ 5 - l t 6 ]
-le is less th an o r
equal to
5 6 i f test 5 -gt 6 i f [ 5 -gt 6 ]
-ge is greater t hanor equal to
5 >= 6 i f test 5 -ge 6 i f [ 5 -ge 6 ]
-
7/31/2019 ExperimentNo.2
5/7
Ope rating System Design Laborat ory
5 | P a g e
Exper im ent N o. 3
NOTE: == is eq ual, != is not eq ual.
For strin g Com parison s use:
O pe r a to r M e a n ing
str ing1 = string2 str ing1 is equal to s t r ing2
string1 != string2 str ing1 is NOT equal t o st r ing2
string1 str ing1 is NOT NULL or no t def ined
-n string1 string1 is NOT NULL and do es exist
-z strin g1 string1 is NULL and do es exist
Shel l a lso t est fo r f i le and di rector y type s
Test M eaning
-s file Non em pty f i l e
-f file Is Fi le ex ist o r no rm al f i le and n ot a di rectory
-d dir Is Directo ry ex ist an d n ot a f i le
-w f i le Is w ri teable f i le
-r file Is read -only f i le
-x file Is f i le is execut able
Logical Ope rat ors:Logica l operators are used to combine tw o or m ore condi t ion at a t im e
Opera torM e a n in g
! expre ssion Logical NOT
expression1 -a expression2 Logical AND
expression1 -o expression2 Logical OR
-
7/31/2019 ExperimentNo.2
6/7
Ope rating System Design Laborat ory
6 | P a g e
Exper im ent N o. 3
if con dition
i f condi t ion w hich is used for d ec ision m aking in shel l scr ipt , I f given condi t ion is t r ue t hencomm and1 is executed.Syntax:
if condition
thencommand1 if condition is true or if exit status
of condition is 0 (zero)
fi
Cond i t ion is def ined as: Condi t ion is noth ing but com par ison bet w een tw o va lues.
For com pression you can use test or [ e xpr ] s tatem ent s or even ex ist s tatu s can be also used.
Loo ps in Shell Script s
Bash supp ort s:
1) for loop
2) whi le loop
whi le :
The synt ax of th e w hi le is:
whi le tes t -commands docommands
doneExecut e com m ands as long as test -com m andshave an ex it s tatu s of zero.
for :
The synt ax of th e for is:for var iable in l is t
do
commands
done
Each w hi te space-separated w ord in l ist is ass igned t o var iable in tu rn and com m ands execut edunt i l l is t is exhausted .
The case Stat em ent
-
7/31/2019 ExperimentNo.2
7/7
Ope rating System Design Laborat ory
7 | P a g e
Exper im ent N o. 3
The case stat em ent is good al tern at ive to m ul t i level i f - then -else-f i stat em ent . It enables
you t o m atch several values against one var iable. I t s easier to read and w ri te.Syntax:case $variable-name in
pattern1) command
...
..command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
The $variable-name is compared against the pat terns unt i l a match is found. The shel l thenexecutes a ll the s ta tement s up t o t he tw o semico lons that are nex t to each ot her . The defau l t i s* ) and i t s executed i f n o m atch is found.
References:1. Unix Concept s and App l icat ion s By Sum itabha Das, Tata M cGraw Hi l l2. UNIX Shell Program m ing by Yashw ant Kanet kar, BPB Pub licat ion s.
Tushar B Kut e
(Sub ject Teacher)