Linux Shell Scripting Tutorial Ver 1 0 W
Linux Shell Scripting Tutorial Ver 1 0 W
I N D EX
● In t ro d u ct io n
❍ Ke rn e l
❍ S h e ll
❍ Ho w t o u s e S h e ll
❍ Co m m o n Lin u x Co m m a n d In t ro d u ct io n
● Pro ce s s
❍ Wh y Pro ce s s re q u ire d
❍ Lin u x co m m a n d s re la t e d wit h p ro ce s s
● Re d ire ct io n o f S t a n d a rd o u t p u t / in p u t
❍ Re d ire ct o rs
❍ Pip e s
❍ Filt e rs
● S h e ll Pro g ra m m in g
❍ Va ria b le s in Lin u x
❍ Ho w t o d e fin e Us e r d e fin e d va ria b le s
❍ Ru le s fo r Na m in g va ria b le n a m e
❍ Ho w t o p rin t o r a cce s s va lu e o f UDV ( Us e r d e fin e d va ria b le s )
❍ Ho w t o writ e s h e ll s crip t
❍ Ho w t o Ru n S h e ll S crip t s
❍ Qu o t e s in S h e ll S crip t s
❍ S h e ll Arit h m e t ic
❍ Co m m a n d Lin e Pro ce s s in g ( Co m m a n d Lin e Arg u m e n t s )
❍ Wh y Co m m a n d Lin e a rg u m e n t s re q u ire d
❍ Exit S t a t u s
❍ File n a m e S h o rt h a n d o r m e t a Ch a ra ct e rs ( i. e . wild ca rd s )
● Pro g ra m m in g Co m m a n d s
❍ e ch o co m m a n d
❍ De cis io n m a kin g in s h e ll s crip t ( i. e . if co m m a n d )
❍ t e s t co m m a n d o r [ e xp r ]
❍ Lo o p in s h e ll s crip t s
❍ Th e ca s e S t a t e m e n t
❍ Th e re a d S t a t e m e n t
● Mo re Ad va n ce d S h e ll S crip t Co m m a n d s
❍ / d e v/ n u ll - Us e t o s e n d u n wa n t e d o u t p u t o f p ro g ra m
❍ Lo ca l a n d Glo b a l S h e ll va ria b le ( e xp o rt co m m a n d )
❍ Co n d it io n a l e xe cu t io n i. e . && a n d | |
❍ I/ O Re d ire ct io n a n d file d e s crip t o rs
❍ Fu n ct io n s
❍ Us e r In t e rfa ce a n d d ia lo g u t ilit y
❍ t ra p co m m a n d
❍ g e t o p t s co m m a n d
❍ Mo re e xa m p le s o f S h e ll S crip t ( Exe rcis e fo r Yo u : - )
© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.
I n t ro d u c t io n
Th is t u t o ria l is d e s ig n e d fo r b e g in n e rs o n ly a n d Th is t u t o ria l e xp la in s t h e b a s ics o f
s h e ll p ro g ra m m in g b y s h o win g s o m e e xa m p le s o f s h e ll p ro g ra m s . It s n o t h e lp o r
m a n u a l fo r t h e s h e ll. Wh ile re a d in g t h is t u t o ria l yo u ca n fin d m a n u a l q u it e u s e fu l (
t yp e m a n b a s h a t $ p ro m p t t o s e e m a n u a l p a g e s ) . Ma n u a l co n t a in s a ll n e ce s s a ry
in fo rm a t io n yo u n e e d , b u t it wo n 't h a ve t h a t m u ch e xa m p le s , wh ich m a ke s id e a
m o re cle a r. Fo r t h a t re a s o n , t h is t u t o ria l co n t a in s e xa m p le s ra t h e r t h a n a ll t h e
fe a t u re s o f s h e ll. I a s s u m e s yo u h a ve a t le a s t wo rkin g kn o wle d g e o f Lin u x i. e . b a s ic
co m m a n d s like h o w t o cre a t e , co p y, re m o ve file s / d ire ct o rie s e t c o r h o w t o u s e
e d it o r like vi o r m ce d it a n d lo g in t o yo u r s ys t e m . Be fo re S t a rt in g Lin u x S h e ll S crip t
Pro g ra m m in g yo u m u s t kn o w
● Ke rn e l
● S h e ll
● Pro ce s s
W h a t 's Ke rn e l
Ke rn e l is h a rt o f Lin u x O/ S . It m a n a g e s re s o u rce o f Lin u x O/ S . Re s o u rce s m e a n s
fa cilit ie s a va ila b le in Lin u x. Fo r e g . Fa cilit y t o s t o re d a t a , p rin t d a t a o n p rin t e r,
m e m o ry, file m a n a g e m e n t e t c . Ke rn e l d e cid e s wh o will u s e t h is re s o u rce , fo r h o w
lo n g a n d wh e n . It ru n s yo u r p ro g ra m s ( o r s e t u p t o e xe cu t e b in a ry file s ) It 's
Me m o ry re s id e n t p o rt io n o f Lin u x. It p e rfo rm a n ce fo llo win g t a s k : -
● I/ O m a n a g e m e n t
● Pro ce s s m a n a g e m e n t
● De vice m a n a g e m e n t
● File m a n a g e m e n t
● Me m o ry m a n a g e m e n t
W h a t 's Lin u x S h e ll
Co m p u t e r u n d e rs t a n d t h e la n g u a g e o f 0 's a n d 1 's ca lle d b in a ry la n g u a g e , In e a rly
d a ys o f co m p u t in g , in s t ru ct io n a re p ro vid e d u s in g b in a ry la n g u a g e , wh ich is d ifficu lt
fo r a ll o f u s , t o re a d a n d writ e . S o in O/ s t h e re is s p e cia l p ro g ra m ca lle d S h e ll. S h e ll
a cce p t s yo u r in s t ru ct io n o r co m m a n d s in En g lis h a n d t ra n s la t e it in t o co m p u t e rs
n a t ive b in a ry la n g u a g e .
Th is is wh a t S h e ll Do e s fo r US
Yo u t yp e Yo u r co m m a n d a n d s h e ll co n ve rt it a s
Ho w t o u s e S h e ll
To u s e s h e ll ( Yo u s t a rt t o u s e yo u r s h e ll a s s o o n a s yo u lo g in t o yo u r s ys t e m ) yo u
h a ve t o s im p ly t yp e co m m a n d s . Fo llo win g is t h e lis t o f co m m o n co m m a n d s .
Lin u x Co m m o n Co m m a n d s
NOTE t h a t fo llo win g co m m a n d s a re fo r Ne w u s e rs o r fo r Be g in n e rs o n ly. Th e
p u rp o s e is if yo u u s e t h is co m m a n d yo u will b e m o re fa m ilia r wit h yo u r s h e ll a n d
s e co n d ly, yo u n e e d s o m e o f t h e s e co m m a n d in yo u r S h e ll s crip t . If yo u wa n t t o g e t
m o re in fo rm a t io n o r h e lp fo r t h is co m m a n d t ry fo llo win g co m m a n d s Fo r e . g . To s e e
h e lp o r o p t io n s re la t e d wit h d a t e co m m a n d t ry
$ d a t e - - h e lp
o r To s e e h e lp o r o p t io n s re la t e d wit h ls co m m a n d ( He re yo u will s cre e n b y s cre e n
h e lp , s in ce h e lp o f ls co m m a n d is q u it e b ig t h a t ca n 't fit o n s in g le s cre e n )
$ ls - - h e lp | m o re
S yn t a x: co m m a n d - n a m e - - h e lp
S yn t a x: m a n co m m a n d - n a m e
S yn t a x: in fo co m m a n d - n a m e
S e e wh a t h a p p e n e d wh e n yo u t yp e fo llo win g
$ m a n ls
$ in fo b a s h
NOTE: In MS - DOS , yo u g e t h e lp b y u s in g / ? clu e o r b y t yp in g h e lp co m m a n d a s
C:\ > d ir / ?
C:\ > d a t e / ?
C:\ > h e lp t im e
C:\ > h e lp d a t e
C:\ > h e lp
Lin u x Co m m a n d
Example ( In front of $
For this Purpose Use this Command Syntax
Prompt)
To see date da te $ da te
To see who's using
wh o $ w ho
system.
Print working directory p wd $ pw d
List name of files in
ls o r d irs $ ls
current directory
To create text file $ c a t > m y file
NOTE: Press and hold ty pe y our te x t
CTRL key and press D to ca t > { file n a m e } w h e n d o n e p re s s
stop or to end file ^D
(CTRL+D)
To text see files ca t { file n a m e } $ cat m y file
To display file one full
m o re { file n a m e } $ m o re m y file
screen at a time
To m o ve o r re n a m e $ m v s a le s
mv {file1} {file2}
file / d ire ct o ry s a le s . 9 9
To create multiple file
copies with various link.
After this both oldfile ln {oldfile} {newfile} $ ln Page1 Book1
newfile refers to same
name
To remove file rm file1 $ rm m y file
W h a t is P ro c e s s e s
Pro ce s s is a n y kin d o f p ro g ra m o r t a s k ca rrie d o u t b y yo u r PC. Fo r e . g . $ ls - lR , is
co m m a n d o r a re q u e s t t o lis t file s in a d ire ct o ry a n d a ll s u b d ire ct o ry in yo u r cu rre n t
d ire ct o ry. It is a p ro ce s s . A p ro ce s s is p ro g ra m ( co m m a n d g ive n b y u s e r) t o
p e rfo rm s o m e Jo b . In Lin u x wh e n yo u s t a rt p ro ce s s , it g ive s a n u m b e r ( ca lle d PID
o r p ro ce s s - id ) , PID s t a rt s fro m 0 t o 6 5 5 3 5 .
W h y P ro c e s s re q u ire d
Lin u x is m u lt i- u s e r, m u lt it a s kin g o / s . It m e a n s yo u ca n ru n m o re t h a n t wo p ro ce s s
s im u lt a n e o u s ly if yo u wis h . Fo r e . g . . To fin d h o w m a n y file s d o yo u h a ve o n yo u r
s ys t e m yo u m a y g ive co m m a n d like
$ ls / - R | w c - l
Th is co m m a n d will t a ke lo t o f t im e t o s e a rch a ll file s o n yo u r s ys t e m . S o yo u ca n
ru n s u ch co m m a n d in Ba ckg ro u n d o r s im u lt a n e o u s ly b y g ivin g co m m a n d like
$ ls / - R | w c - l &
Th e a m p e rs a n d ( &) a t t h e e n d o f co m m a n d t e lls s h e lls s t a rt co m m a n d ( ls / - R | wc
- l) a n d ru n it in b a ckg ro u n d t a ke s n e xt co m m a n d im m e d ia t e ly. An in s t a n ce o f
ru n n in g co m m a n d is ca lle d p ro ce s s a n d t h e n u m b e r p rin t e d b y s h e ll is ca lle d
p ro ce s s - id ( PID) , t h is PID ca n b e u s e t o re fe r s p e cific ru n n in g p ro ce s s .
Lin u x Co m m a n d Re la t e d w it h P ro c e s s
Fo r t h is p u rp o s e Us e t h is Co m m a n d Exa m p le
To s e e cu rre n t ly ru n n in g
ps $ ps
p ro ce s s
To s t o p a n y p ro ce s s i. e . t o
kill { PID} $ k ill 1 0 1 2
kill p ro ce s s
To g e t in fo rm a t io n a b o u t a ll
ps -ag $ ps -ag
ru n n in g p ro ce s s
To s t o p a ll p ro ce s s e xce p t
kill 0 $ k ill 0
yo u r s h e ll
Fo r b a ckg ro u n d p ro ce s s in g
( Wit h &, u s e t o p u t p a rt icu la r
lin u x- co m m a n d & $ ls / - R | w c - l &
co m m a n d a n d p ro g ra m in
b a ckg ro u n d )
NOTE t h a t yo u ca n o n ly kill p ro ce s s wh ich a re cre a t e d b y yo u rs e lf. A Ad m in is t ra t o r
ca n a lm o s t kill 9 5 - 9 8 % p ro ce s s . Bu t s o m e p ro ce s s ca n n o t b e kille d , s u ch a s VDU
Pro ce s s .
© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.
Re d ire c t io n o f S t a n d a rd o u t p u t / in p u t o r I n p u t - Ou t p u t
re d ire c t io n
Mo s t ly a ll co m m a n d g ive s o u t p u t o n s cre e n o r t a ke in p u t fro m ke yb o a rd , b u t in
Lin u x it 's p o s s ib le t o s e n d o u t p u t t o file o r t o re a d in p u t fro m file . Fo r e . g . $ ls
co m m a n d g ive s o u t p u t t o s cre e n ; t o s e n d o u t p u t t o file o f ls g ive co m m a n d , $ ls
> file n a m e . It m e a n s p u t o u t p u t o f ls co m m a n d t o file n a m e . Th e re a re t h re e m a in
re d ire ct io n s ym b o ls > , > > , <
( 1 ) > Re d ire c t o r S y m b o l
S yn t a x: Lin u x - co m m a n d > file n a m e
To o u t p u t Lin u x- co m m a n d s re s u lt t o file . No t e t h a t If file a lre a d y e xis t , it will b e
o ve rwrit t e n e ls e n e w file is cre a t e d . Fo r e . g . To s e n d o u t p u t o f ls co m m a n d g ive
$ ls > m y file s
No w if 'm yfile s ' file e xis t in yo u r cu rre n t d ire ct o ry it will b e o ve rwrit t e n wit h o u t a n y
t yp e o f wa rn in g . ( Wh a t if I wa n t t o s e n d o u t p u t t o file , wh ich is a lre a d y e xis t a n d
wa n t t o ke e p in fo rm a t io n o f t h a t file wit h o u t lo o s in g p re vio u s in fo rm a t io n / d a t a ? ,
Fo r t h is Re a d n e xt re d ire ct o r)
( 3 ) < Re d ire c t o r S y m b o l
S yn t a x: Lin u x - co m m a n d < file n a m e
To t a ke in p u t t o Lin u x- co m m a n d fro m file in s t e a d o f ke y- b o a rd . Fo r e . g . To t a ke
in p u t fo r ca t co m m a n d g ive
$ c a t < m y file s
P ip s
A p ip e is a wa y t o co n n e ct t h e o u t p u t o f o n e p ro g ra m t o t h e in p u t o f a n o t h e r
p ro g ra m wit h o u t a n y t e m p o ra ry file .
A p ip e is n o t h in g b u t a t e m p o ra ry s t o ra g e p la ce wh e re t h e o u t p u t o f o n e co m m a n d
is s t o re d a n d t h e n p a s s e d a s t h e in p u t fo r s e co n d co m m a n d . Pip e s a re u s e d t o ru n
m o re t h a n t wo co m m a n d s ( Mu lt ip le co m m a n d s ) fro m s a m e co m m a n d lin e .
S yn t a x: co m m a n d 1 | co m m a n d 2
Command using Pips Meaning or Use of Pipes
Here the output of ls command is given as input to
$ ls | more more command So that output is printed one screen
full page at a time
Here output of who command is given as input to
$ who | sort sort command So that it will print sorted list of
users
Here output of who command is given as input to
$ who | wc -l wc command So that it will number of user who
logon to system
Here output of ls command is given as input to wc
$ ls -l | wc -l command So that it will print number of files in
current directory.
Here output of who command is given as input to
grep command So that it will print if particular user
$ who | grep raju
name if he is logon or nothing is printed ( To see
for particular user logon)
Filt e r
If a Lin u x co m m a n d a cce p t s it s in p u t fro m t h e s t a n d a rd in p u t a n d p ro d u ce s it s
o u t p u t o n s t a n d a rd o u t p u t is kn o w a s a filt e r. A filt e r p e rfo rm s s o m e kin d o f
p ro ce s s o n t h e in p u t a n d g ive s o u t p u t . Fo r e . g . . S u p p o s e we h a ve file ca lle d
'h o t e l. t xt ' wit h 1 0 0 lin e s d a t a , An d fro m 'h o t e l. t xt ' we wo u ld like t o p rin t co n t a in s
fro m lin e n u m b e r 2 0 t o lin e n u m b e r 3 0 a n d s t o re t h is re s u lt t o file ca lle d 'h lis t ' t h e n
g ive co m m a n d
$ t a il + 2 0 < h o t e l. t x t | h e a d - n 3 0 > h lis t
He re h e a d is filt e r wh ich t a ke s it s in p u t fro m t a il co m m a n d ( t a il co m m a n d s t a rt
s e le ct in g fro m lin e n u m b e r 2 0 o f g ive n file i. e . h o t e l. t xt ) a n d p a s s e s t h is lin e s t o
in p u t t o h e a d , wh o s e o u t p u t is re d ire ct e d t o 'h lis t ' file .
© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.
I n t ro d u c t io n t o S h e ll P ro g ra m m in g
S h e ll p ro g ra m is s e rie s o f Lin u x co m m a n d s . S h e ll s crip t is ju s t like b a t ch file is MS - DOS b u t h a ve
m o re p o we r t h a n t h e MS - DOS b a t ch file . S h e ll s crip t ca n t a ke in p u t fro m u s e r, file a n d o u t p u t t h e m
o n s cre e n . Us e fu l t o cre a t e o u r o wn co m m a n d s t h a t ca n s a ve o u r lo t s o f t im e a n d t o a u t o m a t e s o m e
t a s k o f d a y t o d a y life .
Va ria b le s in Lin u x
S o m e t im e s t o p ro ce s s o u r d a t a / in fo rm a t io n , it m u s t b e ke p t in co m p u t e rs RAM m e m o ry. RAM
m e m o ry is d ivid e d in t o s m a ll lo ca t io n s , a n d e a ch lo ca t io n h a d u n iq u e n u m b e r ca lle d m e m o ry
lo ca t io n / a d d re s s , wh ich is u s e d t o h o ld o u r d a t a . Pro g ra m m e r ca n g ive a u n iq u e n a m e t o t h is
m e m o ry lo ca t io n / a d d re s s ca lle d m e m o ry va ria b le o r va ria b le ( It s a n a m e d s t o ra g e lo ca t io n t h a t m a y
t a ke d iffe re n t va lu e s , b u t o n ly o n e a t a t im e ) . In Lin u x, t h e re a re t wo t yp e s o f va ria b le
1 ) S y s t e m v a ria b le s - Cre a t e d a n d m a in t a in e d b y Lin u x it s e lf. Th is t yp e o f va ria b le d e fin e d in
CAPITAL LETTERS .
2 ) Us e r d e fin e d v a ria b le s ( UD V) - Cre a t e d a n d m a in t a in e d b y u s e r. Th is t yp e o f va ria b le d e fin e d
in lo we r LETTERS .
S o m e S y s t e m v a ria b le s
Yo u ca n s e e s ys t e m va ria b le s b y g ivin g co m m a n d like $ s e t , S o m e o f t h e im p o rt a n t S ys t e m
va ria b le s a re
System Variable Meaning
BASH=/bin/bash Our shell name
BASH_VERSION=1.14.7(1) Our shell version name
COLUMNS=80 No. of columns for our screen
HOME=/home/vivek Our home directory
LINES=25 No. of columns for our screen
LOGNAME=students Our logging name
OSTYPE=Linux Our o/s type : -)
PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings
PS1=[\u@\h \W]\$ Our prompt settings
PWD=/home/students/Common Our current working directory
SHELL=/bin/bash Our shell name
USERNAME=vivek User name who is currently login to this PC
$ v e c h = Bu s
To d e fin e va ria b le ca lle d n h a vin g va lu e 1 0
$ n= 1 0
Ru le s fo r N a m in g v a ria b le n a m e ( Bo t h UD V a n d S y s t e m Va ria b le )
( 1 ) Va ria b le n a m e m u s t b e g in wit h Alp h a n u m e ric ch a ra ct e r o r u n d e rs co re ch a ra ct e r ( _ ) , fo llo we d b y
o n e o r m o re Alp h a n u m e ric ch a ra ct e r. Fo r e . g . Va lid s h e ll va ria b le a re a s fo llo ws
HOME
S YS TEM_ VERS ION
ve ch
no
( 2 ) Do n 't p u t s p a ce s o n e it h e r s id e o f t h e e q u a l s ig n wh e n a s s ig n in g va lu e t o va ria b le . Fo r e . g . . In
fo llo win g va ria b le d e cla ra t io n t h e re will b e n o e rro r
$ no= 1 0
Bu t h e re t h e re will b e p ro b le m fo r fo llo win g
$ no = 10
$ no= 10
$ no = 10
( 5 ) Do n o t u s e ? , * e t c, t o n a m e yo u r va ria b le n a m e s .
Q. 3 . Ho w t o p rin t s u m o f t wo n u m b e rs , le t 's s a y 6 a n d 3
$ e cho 6 + 3
Th is will p rin t 6 + 3 , n o t t h e s u m 9 , To d o s u m o r m a t h o p e ra t io n s in s h e ll u s e e xp r, s yn t a x is a s
fo llo ws S yn t a x: e x p r o p 1 o p e ra t o r o p 2
Wh e re , o p 1 a n d o p 2 a re a n y In t e g e r Nu m b e r ( Nu m b e r wit h o u t d e cim a l p o in t ) a n d o p e ra t o r ca n b e
+ Ad d it io n
- S u b t ra ct io n
/ Divis io n
% Mo d u la r, t o fin d re m a in d e r Fo r e . g . 2 0 / 3 = 6 , t o fin d re m a in d e r 2 0 % 3 = 2 , ( Re m e m b e r it s
in t e g e r ca lcu la t io n )
\ * Mu lt ip lica t io n
$ e xpr 6 + 3
No w It will p rin t s u m a s 9 , Bu t
$ e xpr 6 + 3
will n o t wo rk b e ca u s e s p a ce is re q u ire d b e t we e n n u m b e r a n d o p e ra t o r ( S e e S h e ll Arit h m e t ic)
Ho w t o w rit e s h e ll s c rip t
No w we writ e o u r firs t s crip t t h a t will p rin t "Kn o wle d g e is Po we r" o n s cre e n . To writ e s h e ll s crip t yo u
ca n u s e in o f t h e Lin u x's t e xt e d it o r s u ch a s vi o r m ce d it o r e ve n yo u ca n u s e ca t co m m a n d . He re we
a re u s in g ca t co m m a n d yo u ca n u s e a n y o f t h e a b o ve t e xt e d it o r. Firs t t yp e fo llo win g ca t co m m a n d
a n d re s t o f t e xt a s it s
$ c a t > firs t
#
# My firs t s h e ll s c rip t
#
c le a r
e c h o "Kn o w le d g e is P o w e r"
Pre s s Ct rl + D t o s a ve . No w o u r s crip t is re a d y. To e xe cu t e it t yp e co m m a n d
$ . / firs t
Th is will g ive e rro r s in ce we h a ve n o t s e t Exe cu t e p e rm is s io n fo r o u r s crip t firs t ; t o d o t h is t yp e
co m m a n d
$ c h m o d + x firs t
$ . / firs t
Firs t s cre e n will b e cle a r, t h e n Kn o wle d g e is Po we r is p rin t e d o n s cre e n . To p rin t m e s s a g e o f
va ria b le s co n t a in s we u s e r e ch o co m m a n d , g e n e ra l fo rm o f e ch o co m m a n d is a s fo llo ws
e ch o "Me s s a g e "
e ch o "Me s s a g e v a ria b le 1 , v a ria b le 2 . . . . v a ria b le N"
Ho w t o Ru n S h e ll S c rip t s
Be ca u s e o f s e cu rit y o f file s , in Lin u x, t h e cre a t o r o f S h e ll S crip t d o e s n o t g e t e xe cu t io n p e rm is s io n b y
d e fa u lt . S o if we wis h t o ru n s h e ll s crip t we h a ve t o d o t wo t h in g s a s fo llo ws
( 1 ) Us e ch m o d co m m a n d a s fo llo ws t o g ive e xe cu t io n p e rm is s io n t o o u r s crip t
S yn t a x: ch m o d +x s h e ll- s crip t - n a m e
OR S yn t a x: ch m o d 777 s h e ll- s crip t - n a m e
( 2 ) Ru n o u r s crip t a s
S yn t a x: . / y o u r- s h e ll- p ro g ra m - n a m e
Fo r e . g .
$ . / firs t
He re '. '( d o t ) is co m m a n d , a n d u s e d in co n ju n ct io n wit h s h e ll s crip t . Th e d o t ( . ) in d ica t e s t o cu rre n t
s h e ll t h a t t h e co m m a n d fo llo win g t h e d o t ( . ) h a s t o b e e xe cu t e d in t h e s a m e s h e ll i. e . wit h o u t t h e
lo a d in g o f a n o t h e r s h e ll in m e m o ry. Or yo u ca n a ls o t ry fo llo win g s yn t a x t o ru n S h e ll S crip t
S yn t a x: b a s h &n b s h ; &n b s h ; y o u r- s h e ll- p ro g ra m - n a m e
OR / b in / s h &n b s h ; &n b s h ; y o u r- s h e ll- p ro g ra m - n a m e
Fo r e . g .
$ b a s h firs t
$ / b in / s h firs t
No t e t h a t t o ru n s crip t , yo u n e e d t o h a ve in s a m e d ire ct o ry wh e re yo u cre a t e d yo u r s crip t , if yo u a re
in d iffe re n t d ire ct o ry yo u r s crip t will n o t ru n ( b e ca u s e o f p a t h s e t t in g s ) , Fo r e g . Yo u r h o m e d ire ct o ry
is ( u s e $ p wd t o s e e cu rre n t wo rkin g d ire ct o ry) / h o m e / vive k. Th e n yo u cre a t e d o n e s crip t ca lle d
'firs t ', a ft e r cre a t io n o f t h is s crip t yo u m o ve d t o s o m e o t h e r d ire ct o ry le t s s a y
/ h o m e / vive k/ Le t t e rs / Pe rs o n a l, No w if yo u t ry t o e xe cu t e yo u r s crip t it will n o t ru n , s in ce s crip t 'firs t '
is in / h o m e / vive k d ire ct o ry, t o Ove rco m e t h is p ro b le m t h e re a re t wo wa ys Firs t , s p e cify co m p le t e
p a t h o f yo u r s crip t wh e n e ve r yo u wa n t t o ru n it fro m o t h e r d ire ct o rie s like g ivin g fo llo win g
co m m a n d
$ / b in / s h / h o m e / v iv e k / firs t
Co m m a n d s Re la t e d w it h S h e ll P ro g ra m m in g
( 1 ) e ch o [o p t io n s ] [s t rin g , v a ria b le s . . . ]
Dis p la ys t e xt o r va ria b le s va lu e o n s cre e n .
Op t io n s
- n Do n o t o u t p u t t h e t ra ilin g n e w lin e .
- e En a b le in t e rp re t a t io n o f t h e fo llo win g b a cks la s h e s ca p e d ch a ra ct e rs in t h e s t rin g s :
\ a a le rt ( b e ll)
\ b b a cks p a ce
\ c s u p p re s s t ra ilin g n e w lin e
\ n n e w lin e
\ r ca rria g e re t u rn
\ t h o rizo n t a l t a b
\ \ b a cks la s h
Fo r e g . $ e c h o - e "An a p p le a d a y k e e p s a w a y \ a \ t \ t d o c t o r\ n "
( 2 ) Mo re a b o u t Qu o t e s
Th e re a re t h re e t yp e s o f q u o t e s
" i. e . Do u b le Qu o t e s
' i. e . S in g le q u o t e s
` i. e . Ba ck q u o t e
1 . "Do u b le Qu o t e s " - An yt h in g e n clo s e in d o u b le q u o t e s re m o ve d m e a n in g o f t h a t ch a ra ct e rs ( e xce p t
\ and $).
2 . 'S in g le q u o t e s ' - En clo s e d in s in g le q u o t e s re m a in s u n ch a n g e d .
3 . ` Ba ck q u o t e ` - To e xe cu t e co m m a n d .
Fo r e g .
$ e c h o "To d a y is d a t e "
Ca n 't p rin t m e s s a g e wit h t o d a y's d a t e .
$ e c h o "To d a y is ` d a t e ` ".
No w it will p rin t t o d a y's d a t e a s , To d a y is Tu e Ja n . . . . , S e e t h e ` d a t e ` s t a t e m e n t u s e s b a ck q u o t e ,
( S e e a ls o S h e ll Arit h m e t ic NOTE) .
( 3 ) S h e ll Arit h m e t ic
Us e t o p e rfo rm a rit h m e t ic o p e ra t io n s Fo r e . g .
$ e xpr 1 + 3
$ e xpr 2 - 1
$ e xpr 1 0 / 2
$ e x p r 2 0 % 3 # re m a in d e r re a d a s 2 0 m o d 3 a n d re m a in d e r is 2 )
$ e x p r 1 0 \ * 3 # Mu lt ip lic a t io n u s e \ * n o t * s in c e it s w ild c a rd )
$ e cho ` e xpr 6 + 3 `
Fo r t h e la s t s t a t e m e n t n o t t h e fo llo win g p o in t s
1 ) Firs t , b e fo re e xp r ke ywo rd we u s e d ` ( b a ck q u o t e ) s ig n n o t t h e ( s in g le q u o t e i. e . ') s ig n . Ba ck
q u o t e is g e n e ra lly fo u n d o n t h e ke y u n d e r t ild e ( ~ ) o n PC ke yb o a rd s OR To t h e a b o ve o f TAB ke y.
2 ) S e co n d , e xp r is a ls o e n d wit h ` i. e . b a ck q u o t e .
3 ) He re e xp r 6 + 3 is e va lu a t e d t o 9 , t h e n e ch o co m m a n d p rin t s 9 a s s u m
4 ) He re if yo u u s e d o u b le q u o t e o r s in g le q u o t e , it will NOT wo rk, Fo r e g .
$ e c h o "e x p r 6 + 3 " # It will p rin t e xp r 6 + 3
$ e c h o 'e x p r 6 + 3 '
Co m m a n d Lin e P ro c e s s in g
No w t ry fo llo win g co m m a n d ( a s s u m e s t h a t t h e file "g ra t e _ s t o rie s _ o f" is n o t e xis t o n yo u r d is k)
$ ls g ra t e _ s t o rie s _ o f
It will p rin t m e s s a g e s o m e t h in g like -
g ra t e _ s t o rie s _ o f: No s u ch file o r d ire ct o ry
We ll a s it t u rn s o u t ls wa s t h e n a m e o f a n a ct u a l co m m a n d a n d s h e ll e xe cu t e d t h is co m m a n d wh e n
g ive n t h e co m m a n d . No w it cre a t e s o n e q u e s t io n Wh a t a re co m m a n d s ? Wh a t h a p p e n e d wh e n yo u
t yp e $ ls g ra t e _ s t o rie s _ o f? Th e firs t wo rd o n co m m a n d lin e , ls , is n a m e o f t h e co m m a n d t o b e
e xe cu t e d . Eve ryt h in g e ls e o n co m m a n d lin e is t a ke n a s a rg u m e n t s t o t h is co m m a n d . Fo r e g .
$ t a il + 1 0 m y f
He re t h e n a m e o f co m m a n d is t a il, a n d t h e a rg u m e n t s a re + 1 0 a n d m yf.
No w t ry t o d e t e rm in e co m m a n d a n d a rg u m e n t s fro m fo llo win g co m m a n d s :
$ ls fo o
$ cp y y .ba k
$ mv y .ba k y .o ka y
$ t a il - 1 0 m yf
$ m a il ra j
$ s o rt -r -n m yf
$ da te
$ c le a r
Command No. of argument to this command Actual Argument
ls 1 foo
cp 2 y and y.bak
mv 2 y.bak and y.okay
tail 2 -10 and myf
mail 1 raj
sort 3 -r, -n, and myf
date 0
clear 0
W h y Co m m a n d Lin e a rg u m e n t s re q u ire d
S h e ll S crip t n a m e i. e . m ys h e ll
Firs t co m m a n d lin e a rg u m e n t p a s s e d t o m ys h e ll i. e . fo o
S e co n d co m m a n d lin e a rg u m e n t p a s s e d t o m ys h e ll i. e . b a r
m ys h e ll it is $ 0
fo o it is $ 1
b a r it is $ 2
bp 3 -5 myf +20
ls 1 *
cal 0
findBS 4 4 8 24 BIG
Fo r e . g . n o w will writ e s crip t t o p rin t co m m a n d lin g a rg u m e n t a n d we will s e e h o w t o a cce s s t h e m
$ cat > de m o
# !/ b in / s h
#
# S c rip t t h a t d e m o s , c o m m a n d lin e a rg s
#
e c h o "To t a l n u m b e r o f c o m m a n d lin e a rg u m e n t a re $ # "
e c h o "$ 0 is s c rip t n a m e "
e c h o "$ 1 is firs t a rg u m e n t "
e c h o $ 2 is s e c o n d a rg u m e n t "
e c h o "All o f t h e m a re :- $ * "
S a ve t h e a b o ve s crip t b y p re s s in g ct rl+ d , n o w m a ke it e xe cu t a b le
$ chm o d + x de m o
$ . / d e m o He llo W o rld
$ c p d e m o ~ / b in
$ de m o
No t e : Aft e r t h is , Fo r a n y s crip t yo u h a ve t o u s e d a b o ve co m m a n d , in s e q u e n ce , I a m n o t g o in g t o
s h o w yo u a ll o f t h e a b o ve .
( 5 ) Ex it S t a t u s
By d e fa u lt in Lin u x if p a rt icu la r co m m a n d is e xe cu t e d , it re t u rn t wo t yp e o f va lu e s , ( Va lu e s a re u s e d
t o s e e wh e t h e r co m m a n d is s u cce s s fu l o r n o t ) if re t u rn va lu e is ze ro ( 0 ) , co m m a n d is s u cce s s fu l, if
re t u rn va lu e is n o n ze ro ( > 0 ) , co m m a n d is n o t s u cce s s fu l o r s o m e s o rt o f e rro r e xe cu t in g
co m m a n d / s h e ll s crip t . Th is va lu e is kn o w a s Exit S t a t u s o f t h a t co m m a n d . To d e t e rm in e t h is e xit
S t a t u s we u s e $ ? va ria b le o f s h e ll. Fo r e g .
$ rm u n k n o w 1 file
It will s h o w e rro r a s fo llo ws
rm : ca n n o t re m o ve ` u n ko wm 1 file ': No s u ch file o r d ire ct o ry
a n d a ft e r t h a t if yo u g ive co m m a n d $ e c h o $ ?
it will p rin t n o n ze ro va lu e ( > 0 ) t o in d ica t e e rro r. No w g ive co m m a n d
$ ls
$ e cho $ ?
It will p rin t 0 t o in d ica t e co m m a n d is s u cce s s fu l. Try t h e fo llo win g co m m a n d s a n d n o t d o wn t h e re
e xit s t a t u s
$ e xpr 1 + 3
$ e cho $ ?
$ e c h o W e lc o m e
$ e cho $ ?
$ w ild w e s t c a n w o rk ?
$ e cho $ ?
$ da te
$ e cho $ ?
$ e chon $ ?
$ e cho $ ?
fi
He re co n d it io n is n o t h in g b u t co m p a ris o n b e t we e n t wo va lu e s , fo r co m p re s s io n we ca n u s e t e s t o r [
e xp r ] s t a t e m e n t s o r e ve n e xis t s t a t u s ca n b e a ls o u s e d . An e x p re s s io n is n o t h in g b u t co m b in a t io n o f
va lu e s , re la t io n a l o p e ra t o r ( s u ch a s > , < , < > e t c) a n d m a t h e m a t ica l o p e ra t o rs ( s u ch a s + , - , / e t c ) .
Fo llo win g a re a ll e xa m p le s o f e xp re s s io n :
5 > 2
3 + 6
3 * 65
a < b
c > 5
c > 5 + 30 -1
Typ e fo llo win g co m m a n d ( a s s u m e s yo u h a ve file ca lle d fo o )
$ c a t fo o
$ e cho $ ?
Th e ca t co m m a n d re t u rn ze ro ( 0 ) o n s u cce s s fu l, t h is ca n b e u s e d in if co n d it io n a s fo llo ws , Writ e s h e ll
s crip t a s
$ c a t > s h o w file
# !/ b in / s h
#
# S c rip t t o p rin t file
#
if c a t $ 1
the n
e c h o - e "\ n \ n File $ 1 , fo u n d a n d s u c c e s s fu lly e c h o e d "
fi
No w ru n it .
$ c h m o d + x s h o w file
$ . / s h o w file fo o
He re
$ . / s h o w file fo o
Ou r s h e ll s crip t n a m e is s h o wfile ( $ 0 ) a n d fo o is a rg u m e n t ( wh ich is $ 1 ) . No w we co m p a re a s fo llo ws
if ca t $ 1 ( i. e . if ca t fo o )
No w if ca t co m m a n d fin d s fo o file a n d if it s s u cce s s fu lly s h o wn o n s cre e n , it m e a n s o u r ca t co m m a n d
is s u cce s s fu l a n d it s e xis t s t a t u s is 0 ( in d ica t e s s u cce s s ) S o o u r if co n d it io n is a ls o t ru e a n d h e n ce
s t a t e m e n t e ch o - e "\ n \ n File $ 1 , fo u n d a n d s u cce s s fu lly e ch o e d " is p ro ce e d b y s h e ll. No w if ca t
co m m a n d is n o t s u cce s s fu l t h e n it re t u rn s n o n - ze ro va lu e ( in d ica t e s s o m e s o rt o f fa ilu re ) a n d t h is
s t a t e m e n t e ch o - e "\ n \ n File $ 1 , fo u n d a n d s u cce s s fu lly e ch o e d " is s kip p e d b y o u r s h e ll.
No w t ry t o writ e a n s we r fo r fo llo win g
1 ) Cre a t e fo llo win g s crip t
c a t > t rm if
#
# S c rip t t o t e s t rm c o m m a n d a n d e x is t s t a t u s
#
if rm $ 1
the n
e c h o "$ 1 file d e le t e d "
fi
( Pre s s Ct rl + d t o s a ve )
$ c h m o d + x t rm if
No w a n s we r t h e fo llo win g
A) Th e re is file ca lle d fo o , o n yo u r d is k a n d yo u g ive co m m a n d , $ . / t rm fi fo o wh a t will b e o u t p u t .
( 7 ) t e s t co m m a n d o r [ e x p r ]
t e s t co m m a n d o r [ e xp r ] is u s e d t o s e e if a n e xp re s s io n is t ru e , a n d if it is t ru e it re t u rn ze ro ( 0 ) ,
o t h e rwis e re t u rn s n o n ze ro ( > 0 ) fo r fa ls e . S yn t a x: t e s t e x p re s s io n OR [ e x p re s s io n ]
No w will writ e s crip t t h a t d e t e rm in e wh e t h e r g ive n a rg u m e n t n u m b e r is p o s it ive . Writ e s crip t a s
fo llo ws
$ c a t > is p o s t iv e
# !/ b in / s h
#
# S c rip t t o s e e w h e t h e r a rg u m e n t is p o s it iv e
#
if t e s t $ 1 - g t 0
the n
e c h o "$ 1 n u m b e r is p o s it iv e "
fi
Ru n it a s fo llo ws
$ c h m o d + x is p o s t iv e
$ is p o s t iv e 5
He re o / p : 5 n u m b e r is p o s it ive
$ is p o s t iv e - 4 5
He re o / p : No t h in g is p rin t e d
$ is p o s t iv e
He re o / p : . / is p o s t ive : t e s t : - g t : u n a ry o p e ra t o r e xp e ct e d
Th e lin e , if t e s t $ 1 - g t 0 , t e s t t o s e e if firs t co m m a n d lin e a rg u m e n t ( $ 1 ) is g re a t e r t h a n 0 . If it is
t ru e ( 0 ) t h e n t e s t will re t u rn 0 a n d o u t p u t will p rin t e d a s 5 n u m b e r is p o s it ive b u t fo r - 4 5 a rg u m e n t
t h e re is n o o u t p u t b e ca u s e o u r co n d it io n is n o t t ru e ( 0 ) ( n o - 4 5 is n o t g re a t e r t h a n 0 ) h e n ce e ch o
s t a t e m e n t is s kip p e d . An d fo r la s t s t a t e m e n t we h a ve n o t s u p p lie d a n y a rg u m e n t h e n ce e rro r
. / is p o s t ive : t e s t : - g t : u n a ry o p e ra t o r e xp e ct e d is g e n e ra t e d b y s h e ll , t o a vo id s u ch e rro r we ca n t e s t
wh e t h e r co m m a n d lin e a rg u m e n t is s u p p lie d o r n o t . ( S e e co m m a n d 8 S crip t e xa m p le ) . t e s t o r [ e xp r
] wo rks wit h
1 . In t e g e r ( Nu m b e r wit h o u t d e cim a l p o in t )
2 . File t yp e s
3 . Ch a ra ct e r s t rin g s
Fo r Ma t h e m a t ics u s e fo llo win g o p e ra t o r in S h e ll S crip t
Math- ematical
Normal Arithmetical/
Operator in Meaning But in Shell
Mathematical Statements
Shell Script
For [ expr ]
For test statement
statement with if
with if command
command
-eq is equal to 5 == 6 if test 5 -eq 6 if expr [ 5 -eq 6 ]
-ne is not equal to 5 != 6 if test 5 -ne 6 if expr [ 5 -ne 6 ]
-lt is less than 5<6 if test 5 -lt 6 if expr [ 5 -lt 6 ]
is less than or
-le 5 <= 6 if test 5 -le 6 if expr [ 5 -le 6 ]
equal to
-gt is greater than 5>6 if test 5 -gt 6 if expr [ 5 -gt 6 ]
is greater than or
-ge 5 >= 6 if test 5 -ge 6 if expr [ 5 -ge 6 ]
equal to
NOTE: = = is e q u a l, != is n o t e q u a l.
Fo r s t rin g Co m p a ris o n s u s e
Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist
S h e ll a ls o t e s t fo r file a n d d ire ct o ry t yp e s
Test Meaning
-s file Non empty file
-f file Is File exist or normal file and not a directory
-d dir Is Directory exist and not a file
-w file Is writeable file
-r file Is read-only file
-x file Is file is executable
Lo g ica l Op e ra t o rs
Lo g ica l o p e ra t o rs a re u s e d t o co m b in e t wo o r m o re co n d it io n a t a t im e
Operator Meaning
! expression Logical NOT
expression1 -a expression2 Logical AND
expression1 -o expression2 Logical OR
( 8 ) if. . . e ls e . . . fi
If g ive n co n d it io n is t ru e t h e n co m m a n d 1 is e xe cu t e d o t h e rwis e co m m a n d 2 is e xe cu t e d .
S yn t a x:
if condition
then
command1 if condition is true or if exit status
of condition is 0(zero)
...
...
else
command2 if condition is false or if exit status
of condition is >0 (nonzero)
...
...
fi
# S c rip t t o s e e w h e t h e r a rg u m e n t is p o s it iv e o r n e g a t iv e
#
if [ $ # - e q 0 ]
the n
e c h o "$ 0 : Yo u m u s t g iv e / s u p p ly o n e in t e g e rs "
e x it 1
fi
if t e s t $ 1 - g t 0
the n
e c h o "$ 1 n u m b e r is p o s it iv e "
e ls e
e c h o "$ 1 n u m b e r is n e g a t iv e "
fi
Try it a s fo llo ws
$ c h m o d + x is n u m p _ n
$ is n u m p _ n 5
He re o / p : 5 n u m b e r is p o s it ive
$ is n u m p _ n - 4 5
He re o / p : - 4 5 n u m b e r is n e g a t ive
$ is n u m p _ n
He re o / p : . / is p o s _ n : Yo u m u s t g ive / s u p p ly o n e in t e g e rs
$ is n u m p _ n 0
He re o / p : 0 n u m b e r is n e g a t ive
He re firs t we s e e if n o co m m a n d lin e a rg u m e n t is g ive n t h e n it p rin t e rro r m e s s a g e a s ". / is p o s _ n :
Yo u m u s t g ive / s u p p ly o n e in t e g e rs ". if s t a t e m e n t ch e cks wh e t h e r n u m b e r o f a rg u m e n t ( $ # ) p a s s e d
t o s crip t is n o t e q u a l ( - e q ) t o 0 , if we p a s s e d a n y a rg u m e n t t o s crip t t h e n t h is if s t a t e m e n t is fa ls e
a n d if n o co m m a n d lin e a rg u m e n t is g ive n t h e n t h is if s t a t e m e n t is t ru e . Th e e ch o co m m a n d i. e .
e ch o "$ 0 : Yo u m u s t g ive / s u p p ly o n e in t e g e rs "
| |
| |
1 2
1 will p rin t Na m e o f s crip t
2 will p rin t t h is e rro r m e s s a g e
An d fin a lly s t a t e m e n t e xit 1 ca u s e s n o rm a l p ro g ra m t e rm in a t io n wit h e xit s t a t u s 1 ( n o n ze ro m e a n s
s crip t is n o t s u cce s s fu lly ru n ) , Th e la s t s a m p le ru n $ is n u m p _ n 0 , g ive s o u t p u t a s "0 n u m b e r is
n e g a t ive ", b e ca u s e g ive n a rg u m e n t is n o t > 0 , h e n ce co n d it io n is fa ls e a n d it 's t a ke n a s n e g a t ive
n u m b e r. To a vo id t h is re p la ce s e co n d if s t a t e m e n t wit h if t e s t $ 1 - g e 0 .
( 9 ) Mu lt ile v e l if- t h e n - e ls e
S yn t a x:
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are true (i.e.
all of the above nonzero or false)
execute all commands up to fi
fi
Suppose ,
$ c a t > t e s t fo r
fo r i in 1 2 3 4 5
do
e c h o "W e lc o m e $ i t im e s "
do ne
Ru n it a s ,
$ c h m o d + x t e s t fo r
$ . / t e s t fo r
Th e fo r lo o p firs t cre a t e s i va ria b le a n d a s s ig n e d a n u m b e r t o i fro m t h e lis t o f n u m b e r fro m 1 t o 5 ,
Th e s h e ll e xe cu t e e ch o s t a t e m e n t fo r e a ch a s s ig n m e n t o f i. ( Th is is u s u a lly kn o w a s it e ra t io n ) Th is
p ro ce s s will co n t in u e u n t il a ll t h e it e m s in t h e lis t we re n o t fin is h e d , b e ca u s e o f t h is it will re p e a t 5
e ch o s t a t e m e n t s . fo r e . g . No w t ry s crip t a s fo llo ws
$ c a t > m t a b le
# !/ b in / s h
#
# S c rip t t o t e s t fo r lo o p
#
#
if [ $ # - e q 0 ]
the n
e c h o "Erro r - N u m b e r m is s in g fo rm c o m m a n d lin e a rg u m e n t "
e c h o "S y n t a x : $ 0 n u m b e r"
e c h o " Us e t o p rin t m u lt ip lic a t io n t a b le fo r g iv e n n u m b e r"
e x it 1
fi
n= $ 1
fo r i in 1 2 3 4 5 6 7 8 9 1 0
do
e c h o "$ n * $ i = ` e x p r $ i \ * $ n ` "
do ne
S a ve a n d Ru n it a s
$ c h m o d + x m t a b le
$ . / m t a b le 7
$ . / m t a b le
Fo r firs t ru n , Ab o ve p ro g ra m p rin t m u lt ip lica t io n t a b le o f g ive n n u m b e r wh e re i = 1 , 2 . . . 1 0 is
m u lt ip ly b y g ive n n ( h e re co m m a n d lin e a rg u m e n t 7 ) in o rd e r t o p ro d u ce m u lt ip lica t io n t a b le a s
7 * 1 = 7
7 * 2 = 14
...
..
7 * 1 0 = 7 0 An d fo r S e co n d ru n , it will p rin t m e s s a g e -
Erro r - Nu m b e r m is s in g fo rm co m m a n d lin e a rg u m e n t
S yn t a x : . / m t a b le n u m b e r
Us e t o p rin t m u lt ip lica t io n t a b le fo r g ive n n u m b e r
Th is h a p p e n e d b e ca u s e we h a ve n o t s u p p lie d g ive n n u m b e r fo r wh ich we wa n t m u lt ip lica t io n t a b le ,
He n ce we a re s h o win g Erro r m e s s a g e , S yn t a x a n d u s a g e o f o u r s crip t . Th is is g o o d id e a if o u r
p ro g ra m t a ke s s o m e a rg u m e n t , le t t h e u s e r kn o w wh a t is u s e o f t h is s crip t a n d h o w t o u s e d it . No t e
t h a t t o t e rm in a t e o u r s crip t we u s e d 'e xit 1 ' co m m a n d wh ich t a ke s 1 a s a rg u m e n t ( 1 In d ica t e s e rro r
a n d t h e re fo re s crip t is t e rm in a t e d )
( b ) w h ile lo o p
S yn t a x:
while [ condition ]
do
command1
command2
command3
..
....
done
Lo o p is e xe cu t e d a s lo n g a s g ive n co n d it io n is t ru e . Fo r e g . Ab o ve fo r lo o p p ro g ra m ca n b e writ t e n
u s in g wh ile lo o p a s
$ ca t > nt1
# !/ b in / s h
#
# S c rip t t o t e s t w h ile s t a t e m e n t
#
#
if [ $ # - e q 0 ]
the n
e c h o "Erro r - N u m b e r m is s in g fo rm c o m m a n d lin e a rg u m e n t "
e c h o "S y n t a x : $ 0 n u m b e r"
e c h o " Us e t o p rin t m u lt ip lic a t io n t a b le fo r g iv e n n u m b e r"
e x it 1
fi
n= $ 1
i= 1
w h ile [ $ i - le 1 0 ]
do
e c h o "$ n * $ i = ` e x p r $ i \ * $ n ` "
i= ` e x p r $ i + 1 `
do ne
S a ve it a n d t ry a s
$ chm o d + x nt1
$ ./ n t1 7
Ab o ve lo o p ca n b e e xp la in e d a s fo llo ws
Set the value of command line argument to
n=$1
variable n. (Here it's set to 7 )
i=1 Set variable i to 1
This is our loop condition, here if value of i is less
while [ $i -le 10 ] than 10 then, shell execute all statements between
do and done
do Start loop
Print multiplication table as
7*1=7
7 * 2 = 14
echo "$n * $i = `expr $i \* $n`"
....
7 * 10 = 70, Here each time value of variable n is
multiply be i.
Increment i by 1 and store result to i. ( i.e. i=i+1)
Caution: If we ignore (remove) this statement than
our loop become infinite loop because value of
variable i always remain less than 10 and program
i=`expr $i + 1` will only output
7*1=7
...
...
E (infinite times)
Loop stops here if i is not less than 10 i.e.
done condition of loop is not true. Hence
loop is terminated.
( 1 1 ) Th e ca s e S t a t e m e n t
Th e ca s e s t a t e m e n t is g o o d a lt e rn a t ive t o Mu lt ile ve l if- t h e n - e ls e - fi s t a t e m e n t . It e n a b le yo u t o m a t ch
s e ve ra l va lu e s a g a in s t o n e va ria b le . It s e a s ie r t o re a d a n d writ e .
S yn t a x:
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
Th e $ va ria b le - n a m e is co m p a re d a g a in s t t h e p a t t e rn s u n t il a m a t ch is fo u n d . Th e s h e ll t h e n
e xe cu t e s a ll t h e s t a t e m e n t s u p t o t h e t wo s e m ico lo n s t h a t a re n e xt t o e a ch o t h e r. Th e d e fa u lt is * )
a n d it s e xe cu t e d if n o m a t ch is fo u n d . Fo r e g . Cre a t e s crip t a s fo llo ws
$ cat > car
#
# if n o v e h ic le n a m e is g iv e n
# i. e . - z $ 1 is d e fin e d a n d it is N ULL
#
# if n o c o m m a n d lin e a rg
if [ - z $ 1 ]
the n
re n t a l= "* * * Un k n o w n v e h ic le * * * "
e lif [ - n $ 1 ]
the n
# o t h e rw is e m a k e firs t a rg a s re n t a l
re n t a l= $ 1
fi
c a s e $ re n t a l in
"c a r") e c h o "Fo r $ re n t a l Rs . 2 0 p e r k / m ";;
"v a n ") e c h o "Fo r $ re n t a l Rs . 1 0 p e r k / m ";;
"je e p ") e c h o "Fo r $ re n t a l Rs . 5 p e r k / m ";;
"b ic y c le ") e c h o "Fo r $ re n t a l 2 0 p a is a p e r k / m ";;
* ) e c h o "S o rry , I c a n n o t g a t a $ re n t a l fo r y o u ";;
e sac
S a ve it b y p re s s in g CTRL+ D
$ chm od + x car
$ car van
$ car car
$ c a r Ma ru t i- 8 0 0
He re firs t we will ch e ck, t h a t if $ 1 ( firs t co m m a n d lin e a rg u m e n t ) is n o t g ive n s e t va lu e o f re n t a l
va ria b le t o "* * * Un kn o wn ve h icle * * * ", if va lu e g ive n t h e n s e t it t o g ive n va lu e . Th e $ re n t a l is
co m p a re d a g a in s t t h e p a t t e rn s u n t il a m a t ch is fo u n d . He re fo r firs t ru n it s m a t ch wit h va n a n d it will
s h o w o u t p u t Fo r va n Rs . 1 0 p e r k/ m . Fo r s e co n d ru n it p rin t , "Fo r ca r Rs . 2 0 p e r k/ m ". An d fo r la s t
ru n , t h e re is n o m a t ch fo r Ma ru t i- 8 0 0 , h e n ce d e fa u lt i. e . * ) is e xe cu t e d a n d it p rin t s , "S o rry, I ca n
n o t g a t a Ma ru t i- 8 0 0 fo r yo u ". No t e t h a t e s a c is a lwa ys re q u ire d t o in d ica t e e n d o f ca s e s t a t e m e n t .
( 1 2 ) Th e re a d S t a t e m e n t
Us e t o g e t in p u t fro m ke yb o a rd a n d s t o re t h e m t o va ria b le .
Ru n it a s fo llo ws
$ chm od + x s ayH
$ ./ s a y H
Th is s crip t firs t a s k yo u yo u r n a m e a n d t h e n wa it s t o e n t e r n a m e fro m t h e u s e r, Th e n u s e r e n t e rs
n a m e fro m ke yb o a rd ( Aft e r g ivin g n a m e yo u h a ve t o p re s s ENTER ke y) a n d t h is e n t e re d n a m e
t h ro u g h ke yb o a rd is s t o re d ( a s s ig n e d ) t o va ria b le fn a m e .
( 1 3 ) File n a m e S h o rt h a n d o r m e t a Ch a ra ct e rs ( i. e . w ild ca rd s )
* o r ? o r [ . . . ] is o n e o f s u ch s h o rt h a n d ch a ra ct e r.
* Ma t ch e s a n y s t rin g o r g ro u p o f ch a ra ct e rs .
Fo r e . g . $ ls * , will s h o w a ll file s , $ ls a * - will s h o w a ll file s wh o s e firs t n a m e is s t a rt in g wit h le t t e r
'a ', $ ls * . c , will s h o w a ll file s h a vin g e xt e n s io n . c $ ls u t * . c , will s h o w a ll file s h a vin g e xt e n s io n . c
b u t firs t t wo le t t e rs o f file n a m e m u s t b e 'u t '.
? Ma t ch e s a n y s in g le ch a ra ct e r.
Fo r e . g . $ ls ? , will s h o w o n e s in g le le t t e r file n a m e , $ ls fo ? , will s h o w a ll file s wh o s e n a m e s a re 3
ch a ra ct e r lo n g a n d file n a m e b e g in wit h fo
[ . . . ] Ma t ch e s a n y o n e o f t h e e n clo s e d ch a ra ct e rs .
Fo r e . g . $ ls [ a b c ] * - will s h o w a ll file s b e g in n in g wit h le t t e rs a , b , c
[ . . - . . ] A p a ir o f ch a ra ct e rs s e p a ra t e d b y a m in u s s ig n d e n o t e s a ra n g e ;
Fo r e g . $ ls / b in / [ a - c] * - will s h o w a ll file s n a m e b e g in n in g wit h le t t e r a , b o r c like
Bu t
$ ls / b in / [ !a - o ]
$ ls / b in / [ ^ a - o ]
If t h e firs t ch a ra ct e r fo llo win g t h e [ is a ! o r a ^ t h e n a n y ch a ra ct e r n o t e n clo s e d is m a t ch e d i. e . d o
n o t s h o w u s file n a m e t h a t b e g in n in g wit h a , b , c, e . . . o , like
b y u s e rs wh o a re cu rre n t ly lo g in . No t e t h a t Yo u ca n 't u s e $ d a t e wh o fo r s a m e p u rp o s e , yo u m u s t
p u t s e m ico lo n in b e t we e n d a t e a n d wh o co m m a n d .
© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.
Mo re Ad v a n c e d S h e ll S c rip t Co m m a n d s
/ d e v / n u ll - Us e t o s e n d u n w a n t e d o u t p u t o f p ro g ra m
Th is is s p e cia l Lin u x file wh ich is u s e d t o s e n d a n y u n wa n t e d o u t p u t fro m p ro g ra m / co m m a n d .
S yn t a x: co m m a n d > / d e v / n u ll
Fo r e . g . $ ls > / d e v / n u ll , o u t p u t o f t h is co m m a n d is n o t s h o wn o n s cre e n it s s e n d t o t h is s p e cia l file .
Th e / d e v d ire ct o ry co n t a in s o t h e r d e vice file s . Th e file s in t h is d ire ct o ry m o s t ly re p re s e n t p e rip h e ra l
d e vice s s u ch d is ks liks flo p p y d is k, s o u n d ca rd , lin e p rin t e rs e t c.
Lo c a l a n d Glo b a l S h e ll v a ria b le ( e x p o rt c o m m a n d )
No rm a lly a ll o u r va ria b le s a re lo ca l. Lo ca l va ria b le ca n b e u s e d in s a m e s h e ll, if yo u lo a d a n o t h e r co p y o f
s h e ll ( b y t yp in g t h e / b in / b a s h a t t h e $ p ro m p t ) t h e n n e w s h e ll ig n o re d a ll o ld s h e ll's va ria b le . Fo r e . g .
Co n s id e r fo llo win g e xa m p le
$ v e c h = Bu s
$ e cho $ ve ch
Bu s
$ / b in / b a s h
$ e cho $ ve ch
Bu s
Command Meaning
Create new local variable 'vech' with Bus as value
$ vech=Bus
in first shell
$ echo $vech Print the contains of variable vech
$ export vech Export first shells variable to second shell
Now load second shell in memory (Old shell's
$ /bin/bash variable is accessed from second shell, if they are
exported )
$ echo $vech Print the contains of variable vech
$ exit Exit from second shell return to first shell
$ echo $vech Print the contains of variable vech
Co n d it io n a l e x e c u t io n i. e . && a n d | |
Th e co n t ro l o p e ra t o rs a re && ( re a d a s AND) a n d | | ( re a d a s OR) . An AND lis t h a s t h e
S yn t a x: co m m a n d 1 && co m m a n d 2
He re co m m a n d 2 is e xe cu t e d if, a n d o n ly if, co m m a n d 1 re t u rn s a n e xit s t a t u s o f ze ro . An OR lis t h a s t h e
S yn t a x: co m m a n d 1 | | co m m a n d 2
He re co m m a n d 2 is e xe cu t e d if a n d o n ly if co m m a n d 1 re t u rn s a n o n - ze ro e xit s t a t u s . Yo u ca n u s e b o t h a s
fo llo ws
co m m a n d 1 && co m a m n d 2 if e xis t s t a t u s is ze ro | | co m m a n d 3 if e xit s t a t u s is n o n - ze ro
He re if co m m a n d 1 is e xe cu t e d s u cce s s fu lly t h e n s h e ll will ru n co m m a n d 2 a n d if co m m a n d 1 is n o t
s u cce s s fu l t h e n co m m a n d 3 is e xe cu t e d . Fo r e . g .
$ rm m y f && e c h o File is re m o v e d s u c c e s s fu lly | | e c h o File is n o t re m o v e d
If file ( m yf) is re m o ve d s u cce s s fu l ( e xis t s t a t u s is ze ro ) t h e n "e ch o File is re m o ve d s u cce s s fu lly" s t a t e m e n t
is e xe cu t e d , o t h e rwis e "e ch o File is n o t re m o ve d " s t a t e m e n t is e xe cu t e d ( s in ce e xis t s t a t u s is n o n - ze ro )
-2 0
11
2
^D
$ s o rt < n o s
-2 0
2
10
11
Firs t we h a ve cre a t e d t h e file n o s , t h e n we h a ve t a ke n in p u t fro m t h is file a n d s o rt co m m a n d p rin t s s o rt e d
n u m b e rs . Th is is ca lle d in p u t re d ire ct io n . In Lin u x ( An d in C p ro g ra m m in g La n g u a g e ) yo u r ke yb o a rd ,
s cre e n e t c a re t re a t e d a s file s . Fo llo win g a re n a m e o f s u ch file s
File Descriptors
Standard File Use Example
number
stdin 0 as Standard input Keyboard
stdout 1 as Standard output Screen
stderr 2 as Standard error Screen
Try it a s fo llo w s
$ chm o d + x de m o s cr
$ ./ de m o s cr
Erro r : Nu m b e r a re n o t s u p p lie d
Us a g e : . / d e m o s cr n u m b e r1 n u m b e r2
$ . / d e m o s c r > e r1
$ ./ de m o s cr 5 7
S u m is 1 2
Fu n c t io n s
Fu n ct io n is s e rie s o f in s t ru ct io n / co m m a n d s . Fu n ct io n p e rfo rm s p a rt icu la r a ct ivit y in s h e ll. To d e fin e
fu n ct io n u s e fo llo win g
S yn t a x:
function-name ( )
{
command1
command2
.....
...
commandN
return
}
Wh e re fu n ct io n - n a m e is n a m e o f yo u fu n ct io n , t h a t e xe cu t e s t h e s e co m m a n d s . A re t u rn s t a t e m e n t will
t e rm in a t e t h e fu n ct io n . Fo r e . g . Typ e S a yHe llo ( ) a t $ p ro m p t a s fo llo ws
$ S a y He llo ( )
{
e c h o "He llo $ LOGN AME, Ha v e n ic e c o m p u t in g "
re t u rn
}
No w t o e xe cu t e t h is S a yHe llo ( ) fu n ct io n ju s t t yp e it n a m e a s fo llo ws
$ S a y He llo
He llo xxxxx, Ha ve n ice co m p u t in g
Th is wa y yo u ca n ca ll yo u r fu n ct io n . No t e t h a t a ft e r re s t a rt in g yo u r co m p u t e r yo u will lo s s t h is S a yHe llo ( )
fu n ct io n , s in ce it s cre a t e d fo r t h a t s e s s io n o n ly. To o ve rco m e t h is p ro b le m a n d t o a d d yo u o wn fu n ct io n t o
a u t o m a t s o m e o f t h e d a y t o d a y life t a s k, yo u r fu n ct io n t o / e t c/ b a s h rc file . No t e t h a t t o a d d fu n ct io n t o t h is
file yo u m u s t lo g o n a s ro o t . Fo llo win g is t h e s a m p le / e t c/ b a s h rc file wit h t o d a y( ) fu n ct io n , wh ich is u s e d
t o p rin t fo rm a t t e d d a t e . Firs t lo g o n a s ro o t o r if yo u a lre a d y lo g o n wit h yo u r n a m e ( yo u r lo g in is n o t ro o t ) ,
a n d wa n t t o m o ve t o ro o t a cco u n t , t h e n yo u ca n t yp e fo llo win g co m m a n d , wh e n a s ke d fo r p a s s wo rd t yp e
ro o t ( a d m in is t ra t o rs ) p a s s wo rd
$ su
p a s s w o rd :
No w o p e n file a s ( No t e yo u r p ro m p t is ch a n g e d t o # fro m $ t o in d ica t e yo u a re ro o t )
# v i / e t c / b a s h rc
OR
# m c e d it / e t c / b a s h rc
At t h e e n d o f file a d d fo llo win g in / e t c/ b a s h rc file
#
# t o d a y ( ) t o p rin t fo rm a t t e d d a t e
#
# To ru n t h is fu n c t io n t y p e t o d a y a t t h e $ p ro m p t
# Ad d e d b y Viv e k t o s h o w fu n c t io n in Lin u x
#
to da y( )
{
e c h o Th is is a ` d a t e + "% A % d in % B o f % Y ( % r) "`
re t u rn
}
# / e t c / b a s h rc
# S y s t e m w id e fu n c t io n s a n d a lia s e s
# En v iro n m e n t s t u ff g o e s in / e t c / p ro file
# Fo r s o m e u n k n o w n re a s o n b a s h re fu s e s t o in h e rit
# P S 1 in s o m e c irc u m s t a n c e s t h a t I c a n 't fig u re o u t .
# P u t t in g P S 1 h e re e n s u re s t h a t it g e t s lo a d e d e v e ry t im e .
P S 1 = "[ \ u @\ h \ W ] \ \ $ "
#
# t o d a y ( ) t o p rin t fo rm a t t e d d a t e
#
# To ru n t h is fu n c t io n t y p e t o d a y a t t h e $ p ro m p t
# Ad d e d b y Viv e k t o s h o w fu n c t io n in Lin u x
to da y( )
{
e c h o Th is is a ` d a t e + "% A % d in % B o f % Y ( % r) "`
re t u rn
}
# . b a s h rc
#
# Us e r s p e c ific a lia s e s a n d fu n c t io n s
# S o u rc e g lo b a l d e fin it io n s
if [ - f / e t c / b a s h rc ] ; t h e n
. / e t c / b a s h rc
fi
S a y Bu y ( )
{
e c h o "Bu y $ LOGN AME ! Life n e v e r b e t h e s a m e , u n t il y o u lo g a g a in !"
e c h o "P re s s a k e y t o lo g o u t . . . "
re a d
re t u rn
}
Us e r I n t e rfa c e a n d d ia lo g u t ilit y
Go o d p ro g ra m / s h e ll s crip t m u s t in t e ra ct wit h u s e rs . Th e re a re t wo wa ys t o t h is o n e is u s e co m m a n d lin e
t o s crip t wh e n yo u wa n t in p u t , s e co n d u s e s t a t e m e n t like e ch o a n d re a d t o re a d in p u t in t o va ria b le fro m
t h e p ro m p t . Fo r e . g . Writ e s crip t a s
$ c a t > u s e rin t e
#
# S c rip t t o d e m o e c h o a n d re a d c o m m a n d fo r u s e r in t e ra c t io n
#
e c h o "Yo u r g o o d n a m e p le a s e :"
re a d n a
e c h o "Yo u r a g e p le a s e :"
re a d a g e
n e y r= ` e x p r $ a g e + 1 `
e c h o "He llo $ n a , n e x t y e a r y o u w ill b e $ n e y r y rs o ld . "
S a ve it a n d ru n a s
$ c h m o d + x u s e rin t e
$ . / u s e rin t e
Yo u r g o o d n a m e p le a s e :
Vive k
Yo u r a g e p le a s e :
25
He llo Vive k, n e xt ye a r yo u will b e 2 6 yrs o ld .
Eve n yo u ca n cre a t e m e n u s t o in t e ra ct wit h u s e r, firs t s h o w m e n u o p t io n , t h e n a s k u s e r t o ch o o s e m e n u
it e m , a n d t a ke a p p ro p ria t e a ct io n a cco rd in g t o s e le ct e d m e n u it e m , t h is t e ch n iq u e is s h o w in fo llo win g
s crip t
$ cat > m e nuui
#
# S c rip t t o c re a t e s im p le m e n u s a n d t a k e a c t io n a c c o rd in g t o t h a t s e le c t e d
# m e n u it e m
#
w h ile :
do
c le a r
e c h o "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
e c h o " Ma in Me n u "
e c h o "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
e c h o "[ 1 ] S h o w To d a y s d a t e / t im e "
e c h o "[ 2 ] S h o w file s in c u rre n t d ire c t o ry "
e c h o "[ 3 ] S h o w c a le n d a r"
e c h o "[ 4 ] S t a rt e d it o r t o w rit e le t t e rs "
e c h o "[ 5 ] Ex it / S t o p "
e c h o "= = = = = = = = = = = = = = = = = = = = = = = "
e c h o - n "En t e r y o u r m e n u c h o ic e [ 1 - 5 ] : "
re a d y o u rc h
c a s e $ y o u rc h in
1 ) e c h o "To d a y is ` d a t e ` , p re s s a k e y . . . " ; re a d ;;
2 ) e c h o "File s in ` p w d ` " ; ls - l ; e c h o "P re s s a k e y . . . " ; re a d ;;
3 ) c a l ; e c h o "P re s s a k e y . . . " ; re a d ;;
4 ) v i ;;
5 ) e x it 0 ;;
* ) e c h o "Op p s !!! P le a s e s e le c t c h o ic e 1 , 2 , 3 , 4 , o r 5 ";
e c h o "P re s s a k e y . . . " ; re a d ;;
e s ca
do ne
Ab o ve a ll s t a t e m e n t e xp la in e d in fo llo win g t a b le
Statement Explanation
Start infinite loop, this loop will
only break if you select 5 ( i.e.
while :
Exit/Stop menu item) as your menu
choice
do Start loop
Clear the screen, each and every
clear
time
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
Show menu on screen with menu
echo "[2] Show files in current directory"
items
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
Ask user to enter menu item
echo -n "Enter your menu choice [1-5]: "
number
read yourch Read menu item number from user
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ;
Take appropriate action according
echo "Press a key. . ." ; read ;;
to selected menu item, If menu item
3) cal ; echo "Press a key. . ." ; read ;;
is not between 1 - 5, then show
4) vi ;;
error and ask user to input number
5) exit 0 ;;
between 1-5 again
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esca
Stop loop , if menu item number is
done
5 ( i.e. Exit/Stop)
Us e r in t e rfa ce u s u a lly in clu d e s , m e n u s , d iffe re n t t yp e o f b o xe s like in fo b o x, m e s s a g e b o x, In p u t b o x e t c.
In Lin u x s h e ll t h e re is n o b u ilt - in fa cilit y a va ila b le t o cre a t e s u ch u s e r in t e rfa ce , Bu t t h e re is o n e u t ilit y
s u p p lie d wit h Re d Ha t Lin u x ve rs io n 6 . 0 ca lle d d ia lo g , wh ich is u s e d t o cre a t e d iffe re n t t yp e o f b o xe s like
in fo b o x, m e s s a g e b o x, m e n u b o x, In p u t b o x e t c. No w t ry d ia lo g u t ilit y a s fo llo ws :
$ c a t > d ia 1
d ia lo g - - t it le "Lin u x D ia lo g Ut ilit y I n fo b o x " - - b a c k t it le "Lin u x S h e ll S c rip t \
Tu t o ria l" - - in fo b o x "Th is is d ia lo g b o x c a lle d in fo b o x , w h ic h is u s e d \
t o s h o w s o m e in fo rm a t io n o n s c re e n , Th a n k s t o S a v io La m a n d \
S t u a rt He rb e rt t o g iv e u s t h is u t ilit y . P re s s a n y k e y . . . " 7 5 0 ; re a d
S a ve t h e s h e ll s crip t a n d ru n a s
$ c h m o d + x d ia 1
$ . / d ia 1
Aft e r e xe cu t in g t h is d ia lo g s t a t e m e n t yo u will s e e b o x o n s cre e n wit h t it le d a s "We lco m e t o Lin u x Dia lo g
Ut ilit y" a n d m e s s a g e "Th is is d ia lo g . . . . Pre s s a n y ke y. . . " in s id e t h is b o x. Th e t it le o f b o x is s p e cifie d b y
- - t it le o p t io n a n d in fo b o x wit h - - in fo b o x "Me s s a g e " wit h t h is o p t io n . He re 7 a n d 5 0 a re h e ig h t - o f- b o x a n d
wid t h - o f- b o x re s p e ct ive ly. "Lin u x S h e ll S crip t Tu t o ria l" is t h e b a ckt it le o f d ia lo g s h o w o n u p p e r le ft s id e o f
s cre e n a n d b e lo w t h a t lin e is d ra wn . Us e d ia lo g u t ilit y t o Dis p la y d ia lo g b o xe s fro m s h e ll s crip t s .
S yn t a x:
m s g b o x u s in g d ia lo g u t ilit y
$ c a t > d ia 2
d ia lo g - - t it le "Lin u x D ia lo g Ut ilit y Ms g b o x " - - b a c k t it le "Lin u x S h e ll S c rip t \
Tu t o ria l" - - m s g b o x "Th is is d ia lo g b o x c a lle d m s g b o x , w h ic h is u s e d \
t o s h o w s o m e in fo rm a t io n o n s c re e n w h ic h h a s a ls o Ok b u t t o n , Th a n k s t o S a v io La m \
a n d S t u a rt He rb e rt t o g iv e u s t h is u t ilit y . P re s s a n y k e y . . . " 9 5 0
S a ve it a n d ru n a s
$ c h m o d + x d ia 2
$ . / d ia 2
ye s n o b o x u s in g d ia lo g u t ilit y
$ c a t > d ia 3
d ia lo g - - t it le "Ale rt : D e le t e File " - - b a c k t it le "Lin u x S h e ll S c rip t \
Tu t o ria l" - - y e s n o "\ n D o y o u w a n t t o d e le t e '/ u s r/ le t t e rs / jo b a p p lic a t io n '\
file " 7 6 0
s e l= $ ?
c a s e $ s e l in
0 ) e c h o "Yo u s e le c t t o d e le t e file ";;
1 ) e c h o "Yo u s e le c t n o t t o d e le t e file ";;
2 5 5 ) e c h o "Ca n c e le d b y y o u b y p re s s in g [ ES C] k e y ";;
e sac
S a ve it a n d ru n a s
$ c h m o d + x d ia 3
$ . / d ia 3
Statement Meaning
Get exit status of dialog
sel=$?
utility
Now take action according
case $sel in to exit status of dialog
0) echo "You select to delete file";; utility, if exit status is 0 ,
1) echo "You select not to delete file";; delete file, if exit status is
255) echo "Canceled by you by pressing [Escape] key";; 1 do not delete file and if
esac exit status is 255, means
Escape key is pressed.
in p u t b o x u s in g d ia lo g u t ilit y
$ c a t > d ia 4
d ia lo g - - t it le "I n p u t b o x - To t a k e in p u t fro m y o u " - - b a c k t it le "Lin u x S h e ll\
S c rip t Tu t o ria l" - - in p u t b o x "En t e r y o u r n a m e p le a s e " 8 6 0 2 > / t m p / in p u t . $ $
s e l= $ ?
n a = ` c a t / t m p / in p u t . $ $ `
c a s e $ s e l in
0 ) e c h o "He llo $ n a " ;;
1 ) e c h o "Ca n c e l is P re s s " ;;
2 5 5 ) e c h o "[ ES CAP E] k e y p re s s e d " ;;
e sac
rm - f / t m p / in p u t . $ $
In p u t b o x is u s e d t o t a ke in p u t fro m u s e r, He re we a re t a kin g Na m e o f u s e r a s in p u t . Bu t wh e re we a re
g o in g t o s t o re in p u t t e d n a m e , t h e a n s we r is t o re d ire ct in p u t t e d n a m e t o file via s t a t e m e n t
2 > / t m p / in p u t . $ $ a t t h e e n d o f d ia lo g co m m a n d , wh ich m e a n s s e n d s cre e n o u t p u t t o file ca lle d
/ t m p / in p u t . $ $ , le t t e r we ca n re t rie ve t h is in p u t t e d n a m e a n d s t o re t o va ria b le a s fo llo ws
n a = ` c a t / t m p / in p u t . $ $ ` . Fo r in p u t b o x e xit s t a t u s is a s fo llo ws
Exit Status for Inputbox Meaning
0 Command is successful
1 Cancel button is pressed by user
255 Escape key is pressed by user
No w we will writ e s crip t t o cre a t e m e n u s u s in g d ia lo g u t ilit y, fo llo win g a re m e n u it e m s
Da t e / t im e
Ca le n d a r
Ed it o r
a n d a ct io n fo r e a ch m e n u - it e m is fo llo ws
-----------------------------------------------
MENU- ITEM ACTION
-----------------------------------------------
Da t e / t im e S h o w cu rre n t d a t e / t im e
Ca le n d a r S h o w ca le n d a r
Ed it o r S t a rt vi Ed it o r
-----------------------------------------------
Cre a t e s crip t a s fo llo ws
$ cat > s m e nu
#
# Ho w t o c re a t e s m a ll m e n u u s in g d ia lo g
#
d ia lo g - - b a c k t it le "Lin u x S h e ll S c rip t Tu t o ria l " - - t it le "Ma in \
Me n u " - - m e n u "Mo v e u s in g [ UP ] [ D OW N ] , [ En t e r] t o \
S e le c t " 1 5 5 0 3\
D a t e / t im e "S h o w s D a t e a n d Tim e "\
Ca le n d a r "To s e e c a le n d a r "\
Ed it o r "To s t a rt v i e d it o r " 2 > / t m p / m e n u it e m . $ $
m e n u it e m = ` c a t / t m p / m e n u it e m . $ $ `
o pt= $ ?
c a s e $ m e n u it e m in
D a t e / t im e ) d a t e ;;
Ca le n d a r) c a l;;
Ed it o r) v i;;
e sac
rm - f / t m p / m e n u it e m . $ $
S a ve it a n d ru n a s
$ chm od + x s m e nu
$ ./ s m e n u
He re - - m e n u o p t io n is u s e d o f d ia lo g u t ilit y t o cre a t e m e n u s , m e n u o p t io n t a ke
--menu options Meaning
"Move using [UP] [DOWN],[Enter] to Select" This is text show before menu
15 Height of box
50 Width of box
3 Height of menu
First menu item called as tag1 (i.e. Date/time)
Date/time "Shows Date and Time" and description for menu item called as item1
(i.e. "Shows Date and Time")
First menu item called as tag2 (i.e. Calendar)
and description for menu item called as item2
Calendar "To see calendar "
(i.e. "To see calendar")
First menu item called as tag3 (i.e. Editor) and
Editor "To start vi editor " description for menu item called as item3
(i.e."To start vi editor")
Send sleeted menu item (tag) to this temporary
2>/tmp/menuitem.$$
file
t ra p c o m m a n d
No w co n s id e r fo llo win g s crip t
$ c a t > t e s t s ig n
ls - R /
S a ve a n d ru n it a s
$ c h m o d + x t e s t s ig n
$ . / t e s t s ig n
No w if yo u p re s s ct rl + c , wh ile ru n n in g t h is s crip t , s crip t g e t t e rm in a t e d . Th e ct rl + c h e re wo rk a s
s ig n a l, Wh e n s u ch s ig n a l o ccu rs it s s e n d t o a ll p ro ce s s cu rre n t ly ru n n in g in yo u r s ys t e m . No w co n s id e r
fo llo win g s h e ll s crip t
$ c a t > t e s t s ig n 1
#
# W h y t o t ra p s ig n a l, v e rs io n 1
#
Ta k e _ in p u t 1 ( )
{
re c n o = 0
c le a r
e c h o "Ap p o in t m e n t N o t e k e e p e r Ap p lic a t io n fo r Lin u x "
e c h o - n "En t e r y o u r d a t a b a s e file n a m e : "
re a d file n a m e
if [ ! - f $ file n a m e ] ; t h e n
e c h o "S o rry , $ file n a m e d o e s n o t e x it , Cre a t in g $ file n a m e d a t a b a s e "
e c h o "Ap p o in t m e n t N o t e k e e p e r Ap p lic a t io n d a t a b a s e file " > $ file n a m e
fi
if [ $ a n s = y - o $ a n s = Y ] ; t h e n
re c n o = ` e x p r $ re c n o + 1 `
e c h o "$ re c n o . $ n a $ t i $ re m a rk " > > / t m p / in p u t 0 . $ $
fi
e c h o - n "Ad d n e x t a p p o in t m e n t ( y / n ) ? "
re a d is n e x t
if [ $ is n e x t = n - o $ is n e x t = N ] ; t h e n
c a t / t m p / in p u t 0 . $ $ > > $ file n a m e
rm - f / t m p / in p u t 0 . $ $
re t u rn # t e rm in a t e lo o p
fi
do ne
}
#
#
# Ca ll o u r u s e r d e fin e fu n c t io n : Ta k e _ in p u t 1
#
Ta k e _ in p u t 1
S a ve it a n d ru n a s
$ c h m o d + x t e s t s ig n 1
$ . / t e s t s ig n 1
It firs t a s k yo u m a in d a t a b a s e file wh e re a ll a p p o in t m e n t o f t h a t d a y is s t o re d , if n o s u ch d a t a b a s e file
fo u n d , file is cre a t e d , a ft e r t h a t it o p e n o n e t e m p o ra ry file in / t m p d ire ct o ry, a n d p u t s t o d a y's d a t e in t h a t
To ca t ch t h is s ig n a l in a b o ve s crip t , p u t t ra p s t a t e m e n t b e fo re ca llin g Ta ke _ in p u t 1 fu n ct io n a s t ra p
d e l_ file 2 . , He re t ra p co m m a n d ca lle d d e l_ file ( ) wh e n 2 n u m b e r in t e rru p t ( i. e . CTRL+ C ) o ccu rs . Op e n
a b o ve s crip t in e d it o r a n d m o d ify it s o t h a t a t t h e e n d it will lo o k like a s fo llo ws
$ v i t e s t s ig n 1
or
$ m c e d it t e s t s ig n 1
#
# s ig n a l is t ra p p e d t o d e le t e t e m p o ra ry file , v e rs io n 2
#
d e l_ file ( )
{
e c h o "* * * CTRL + C Tra p Oc c u rs ( re m o v in g t e m p o ra ry file ) * * * "
rm - f / t m p / in p u t 0 . $ $
e x it 1
}
Ta k e _ in p u t 1 ( )
{
re c n o = 0
c le a r
e c h o "Ap p o in t m e n t N o t e k e e p e r Ap p lic a t io n fo r Lin u x "
e c h o - n "En t e r y o u r d a t a b a s e file n a m e : "
re a d file n a m e
if [ ! - f $ file n a m e ] ; t h e n
e c h o "S o rry , $ file n a m e d o e s n o t e x it , Cre a t in g $ file n a m e d a t a b a s e "
e c h o "Ap p o in t m e n t N o t e k e e p e r Ap p lic a t io n d a t a b a s e file " > $ file n a m e
fi
if [ $ a n s = y - o $ a n s = Y ] ; t h e n
re c n o = ` e x p r $ re c n o + 1 `
e c h o "$ re c n o . $ n a $ t i $ re m a rk " > > / t m p / in p u t 0 . $ $
fi
e c h o - n "Ad d n e x t a p p o in t m e n t ( y / n ) ? "
re a d is n e x t
if [ $ is n e x t = n - o $ is n e x t = N ] ; t h e n
c a t / t m p / in p u t 0 . $ $ > > $ file n a m e
rm - f / t m p / in p u t 0 . $ $
re t u rn # t e rm in a t e lo o p
fi
do ne
}
#
# S e t t ra p t o fo r CTRL+ C in t e rru p t ,
# W h e n o c c u rs it firs t it c a lls d e l_ file ( ) a n d t h e n e x it
#
t ra p d e l_ file 2
#
# Ca ll o u r u s e r d e fin e fu n c t io n : Ta k e _ in p u t 1
#
Ta k e _ in p u t 1
No w s a ve it ru n t h e p ro g ra m a s
$ . / t e s t s ig n 1
Aft e r g ive n d a t a b a s e file n a m e a n d a ft e r g ivin g a p p o in t m e n t t it le p re s s CTRL+ C, He re we h a ve a lre a d y
ca p t u re d t h is CTRL + C s ig n a l ( in t e rru p t ) , s o firs t o u r fu n ct io n d e l_ file ( ) is ca lle d , in wh ich it g ive s
m e s s a g e a s "* * * CTRL + C Tra p Occu rs ( re m o v in g t e m p o ra ry file ) * * * " a n d t h e n it re m o ve o u r
t e m p o ra ry file a n d t h e n e xit wit h e xit s t a t u s 1 . No w ch e ck / t m p d ire ct o ry a s fo llo ws
$ ls / t m p / in p u t *
No w S h e ll will re p o rt n o s u ch t e m p o ra ry file e xit .
g e to pts co m m a nd
Th is co m m a n d is u s e d t o ch e ck va lid co m m a n d lin e a rg u m e n t p a s s e d t o s crip t . Us u a lly u s e d in wh ile lo o p .
S yn t a x: g e t o p t s { o p t s rin g } { v a ria b le 1 }
g e t o p t s is u s e d b y s h e ll t o p a rs e co m m a n d lin e a rg u m e n t . o p t s t rin g co n t a in s t h e o p t io n le t t e rs t o b e
re co g n ize d ; if a le t t e r is fo llo we d b y a co lo n , t h e o p t io n is e xp e ct e d t o h a ve a n a rg u m e n t , wh ich s h o u ld
b e s e p a ra t e d fro m it b y wh it e s p a ce . Ea ch t im e it is in vo ke d , g e t o p t s p la ce s t h e n e xt o p t io n in t h e s h e ll
va ria b le va ria b le 1 , Wh e n a n o p t io n re q u ire s a n a rg u m e n t , g e t o p t s p la ce s t h a t a rg u m e n t in t o t h e va ria b le
OPTARG. On e rro rs g e t o p t s d ia g n o s t ic m e s s a g e s a re p rin t e d wh e n ille g a l o p t io n s o r m is s in g o p t io n
a rg u m e n t s a re e n co u n t e re d . If a n ille g a l o p t io n is s e e n , g e t o p t s p la ce s ? in t o va ria b le 1 . Fo r e . g . We h a ve
s crip t ca lle d a n i wh ich h a s s yn t a x a s
an i -n -a -s -w -d
Op t io n s : Th e s e a re o p t io n a l a rg u m e n t
- n n a m e o f a n im a l
- a a g e o f a n im a l
- s s e x o f a n im a l
- w w e ig h t o f a n im a l
- d d e m o v a lu e s ( if a n y o f t h e a b o v e o p t io n s a re u s e d
t h e ir v a lu e s a re n o t t a k e n )
$ cat > ani
#
# Us a g e : a n i - n - a - s - w - d
#
#
# h e lp _ a n i( ) To p rin t h e lp
#
h e lp _ a n i( )
{
e c h o "Us a g e : $ 0 - n - a - s - w - d "
e c h o "Op t io n s : Th e s e a re o p t io n a l a rg u m e n t "
e cho " - n n a m e o f a n im a l"
e cho " - a a g e o f a n im a l"
e cho " - s s e x o f a n im a l "
e cho " - w w e ig h t o f a n im a l"
e cho " - d d e m o v a lu e s ( if a n y o f t h e a b o v e o p t io n s a re u s e d "
e cho " t h e ir v a lu e s a re n o t t a k e n ) "
e x it 1
}
#
# S t a rt m a in p ro c e d u re
#
#
# S e t d e fa u lt v a lu e fo r v a ria b le
#
is d e f= 0
n a = Mo t i
a g e = "2 Mo n t h s "
s e x = Ma le
w e ig h t = 3 Kg
#
# if n o a rg u m e n t
#
if [ $ # - lt 1 ] ; t h e n
h e lp _ a n i
fi
w h ile g e t o p t s n :a :s :w :d o p t
do
c a s e "$ o p t " in
n ) n a = "$ OP TARG";;
a ) a g e = "$ OP TARG";;
s ) s e x = "$ OP TARG";;
w ) w e ig h t = "$ OP TARG";;
d ) is d e f= 1 ;;
\ ? ) h e lp _ a n i;;
e sac
do ne
if [ $ is d e f - e q 0 ]
the n
e c h o "An im a l N a m e : $ n a , Ag e : $ a g e , S e x : $ s e x , W e ig h t : $ w e ig h t ( u s e r d e fin e m o d e ) "
e ls e
n a = "P lu t o D o g "
age = 3
s e x = Ma le
w e ig h t = 2 0 k g
e c h o "An im a l N a m e : $ n a , Ag e : $ a g e , S e x : $ s e x , W e ig h t : $ w e ig h t ( d e m o m o d e ) "
fi
S a ve it a n d ru n a s fo llo ws
$ chm od + x ani
$ a n i - n La s s ie - a 4 - s Fe m a le - w 2 0 Kg
$ a n i - a 4 - s Fe m a le - n La s s ie - w 2 0 Kg
$ a n i - n La s s ie - s Fe m a le - w 2 0 Kg - a 4
$ a n i - w 2 0 Kg - s Fe m a le - n La s s ie - a 4
$ a n i - w 2 0 Kg - s Fe m a le
$ a n i - n La s s ie - a 4
$ a n i - n La s s ie
$ ani -a 2
Mo re e x a m p le s o f S h e ll S c rip t ( Ex e rc is e fo r Yo u :- )
Firs t t ry t o writ e t h is s h e ll s crip t , a s e xe rcis e , if a n y p ro b le m o r fo r s a m p le a n s we r t o t h is S h e ll s crip t o p e n
t h e s h e ll s crip t file s u p p lie d wit h t h is t u t o ria l.
Q. 1 . Ho w t o writ e s h e ll s crip t t h a t will a d d t wo n o s , wh ich a re s u p p lie d a s co m m a n d lin e a rg u m e n t , a n d if
t h is t wo n o s a re n o t g ive n s h o w e rro r a n d it s u s a g e
An s we r: S e e Q1 s h e ll S crip t .
Q. 2 . Writ e S crip t t o fin d o u t b ig g e s t n u m b e r fro m g ive n t h re e n o s . No s a re s u p p lie s a s co m m a n d lin e
a rg u m e n t . Prin t e rro r if s u fficie n t a rg u m e n t s a re n o t s u p p lie d .
An s we r: S e e Q2 s h e ll S crip t .
Q. 3 . Writ e s crip t t o p rin t n o s a s 5 , 4 , 3 , 2 , 1 u s in g wh ile lo o p .
An s we r: S e e Q3 s h e ll S crip t .
Q. 4 . Writ e S crip t , u s in g ca s e s t a t e m e n t t o p e rfo rm b a s ic m a t h o p e ra t io n a s
fo llo ws
+ a d d it io n
- s u b t ra ct io n
x m u lt ip lica t io n
/ d ivis io n
Th e n a m e o f s crip t m u s t b e 'q 4 ' wh ich wo rks a s fo llo ws
$ . / q 4 2 0 / 3 , Als o ch e ck fo r s u fficie n t co m m a n d lin e a rg u m e n t s
An s we r: S e e Q4 s h e ll S crip t .
Q. 5 . Writ e S crip t t o s e e cu rre n t d a t e , t im e , u s e rn a m e , a n d cu rre n t d ire ct o ry
An s we r: S e e Q5 s h e ll S crip t .
Q. 6 . Writ e s crip t t o p rin t g ive n n u m b e r in re ve rs e o rd e r, fo r e g . If n o is 1 2 3 it m u s t p rin t a s 3 2 1 .
An s we r: S e e Q6 s h e ll S crip t .
Q. 7 . Writ e s crip t t o p rin t g ive n n u m b e rs s u m o f a ll d ig it , Fo r e g . If n o is 1 2 3 it 's s u m o f a ll d ig it will b e
1+ 2+ 3 = 6.
An s we r: S e e Q7 s h e ll S crip t .
Q. 8 . Ho w t o p e rfo rm re a l n u m b e r ( n u m b e r wit h d e cim a l p o in t ) ca lcu la t io n in Lin u x
An s we r: Us e Lin u x's b c co m m a n d
Q. 9 . Ho w t o ca lcu la t e 5 . 1 2 + 2 . 5 re a l n u m b e r ca lcu la t io n a t $ p ro m p t in S h e ll ?
An s we r: Us e co m m a n d a s , $ e c h o 5 . 1 2 + 2 . 5 | b c , h e re we a re g ivin g e ch o co m m a n d s o u t p u t t o b c t o
ca lcu la t e t h e 5 . 1 2 + 2 . 5
Q. 1 0 . Ho w t o p e rfo rm re a l n u m b e r ca lcu la t io n in s h e ll s crip t a n d s t o re re s u lt t o
t h ird va ria b le , le t s s a y a = 5 . 6 6 , b = 8 . 6 7 , c= a + b ?
An s we r: S e e Q1 0 s h e ll S crip t .
Q. 1 5 . Writ e s crip t ca lle d s a yHe llo , p u t t h is s crip t in t o yo u r s t a rt u p file ca lle d . b a s h _ p ro file , t h e s crip t
s h o u ld ru n a s s o o n a s yo u lo g o n t o s ys t e m , a n d it p rin t a n y o n e o f t h e fo llo win g m e s s a g e in in fo b o x u s in g
d ia lo g u t ilit y, if in s t a lle d in yo u r s ys t e m , If d ia lo g u t ilit y is n o t in s t a lle d t h e n u s e e ch o s t a t e m e n t t o p rin t
m essage : -
Go o d Mo rn in g
Go o d Aft e rn o o n
Go o d Eve n in g , a cco rd in g t o s ys t e m t im e .
An s we r: S e e Q1 5 s h e ll S crip t
Q. 1 6 . Ho w t o writ e s crip t , t h a t will p rin t , Me s s a g e "He llo Wo rld " , in Bo ld a n d Blin k e ffe ct , a n d in d iffe re n t
co lo rs like re d , b ro wn e t c u s in g e ch o co m m a n d .
An s we r: S e e Q1 6 s h e ll S crip t
Q. 1 7 . Writ e s crip t t o im p le m e n t b a ckg ro u n d p ro ce s s t h a t will co n t in u a lly p rin t cu rre n t t im e in u p p e r rig h t
co rn e r o f t h e s cre e n , wh ile u s e r ca n d o h is / h e r n o rm a l jo b a t $ p ro m p t .
An s we r: S e e Q1 7 s h e ll S crip t .
Q. 1 8 . Writ e s h e ll s crip t t o im p le m e n t m e n u s u s in g d ia lo g u t ilit y. Me n u - it e m s a n d a ct io n a cco rd in g t o s e le ct
m e n u - it e m is a s fo llo ws
Me n u - I t e m P u rp o s e Ac t io n fo r Me n u - I t e m
Da t e a n d t im e m u s t b e s h o wn u s in g in fo b o x o f d ia lo g
Da t e / t im e To s e e cu rre n t d a t e t im e
u t ilit y
Ca le n d a r To s e e cu rre n t ca le n d a r Ca le n d a r m u s t b e s h o wn u s in g in fo b o x o f d ia lo g u t ilit y
Firs t a s k u s e r n a m e o f d ire ct o ry wh e re a ll file s a re
p re s e n t , if n o n a m e o f d ire ct o ry g ive n a s s u m e s cu rre n t
d ire ct o ry, t h e n s h o w a ll file s o n ly o f t h a t d ire ct o ry, File s
m u s t b e s h o wn o n s cre e n u s in g m e n u s o f d ia lo g u t ilit y,
De le t e To d e le t e s e le ct e d file
le t t h e u s e r s e le ct t h e file , t h e n a s k t h e co n firm a t io n t o
u s e r wh e t h e r h e / s h e wa n t s t o d e le t e s e le ct e d file , if
a n s we r is ye s t h e n d e le t e t h e file , re p o rt e rro rs if a n y
wh ile d e le t in g file t o u s e r.
Exit To Exit t h is s h e ll s crip t Exit / S t o p s t h e m e n u d rive n p ro g ra m i. e . t h is s crip t