Linux Shell Scripting Tutorial [version 1.0 ed.]

This tutorial is designed for beginners only and explains the basics of shell programming by showing some examples of sh

356 63 213KB

English Pages 46 Year 2000

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
freeos.com......Page 0
Linux Shell Scripting Tutorial......Page 1
Linux Shell Script Tutorial......Page 3
Linux Shell Script Tutorial......Page 8
Linux Shell Script Tutorial......Page 9
Linux Shell Script Tutorial......Page 11
Linux Shell Script Tutorial......Page 30
Recommend Papers

Linux Shell Scripting Tutorial [version 1.0 ed.]

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Linux Shell Scripting Tutorial

Linux Shell Scripting Tutorial Ver. 1.0 Written by Vivek G Gite

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

http://www.freeos.com/guides/lsst/index.htm (1 of 2) [17/08/2001 17.41.52]

Linux Shell Scripting Tutorial







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.

http://www.freeos.com/guides/lsst/index.htm (2 of 2) [17/08/2001 17.41.52]

Linux Shell Script Tutorial

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 ● Re d ire ct o rs , Pip e s , Filt e rs e t c.

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

http://www.freeos.com/guides/lsst/kershell.htm (1 of 5) [17/08/2001 17.42.02]

Linux Shell Script Tutorial

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

It 's e n viro n m e n t p ro vid e d fo r u s e r in t e ra ct io n . S h e ll is a n co m m a n d la n g u a g e in t e rp re t e r t h a t e xe cu t e s co m m a n d s re a d fro m t h e s t a n d a rd in p u t d e vice ( ke yb o a rd ) o r fro m a file . Lin u x m a y u s e o n e o f t h e fo llo win g m o s t p o p u la r s h e lls ( In MS - DOS , S h e ll n a m e is COMMAND. COM wh ich is a ls o u s e d fo r s a m e p u rp o s e , b u t it 's n o t a s p o we rfu l a s o u r Lin u x S h e lls a re !) Shell Name

Developed by

BASH ( Bourne-Again SHell )

Brian Fox and Chet Ramey

CSH (C SHell)

Bill Joy

KSH (Korn SHell)

David Korn

Where

Remark Most common shell in Free Software Linux. It's Freeware Foundation shell. The C shell's syntax and University of California usage are very similar to (For BSD) the C programming language. AT & T Bell Labs

An y o f t h e a b o ve s h e ll re a d s co m m a n d fro m u s e r ( via Ke yb o a rd o r Mo u s e ) a n d t e lls Lin u x O/ s wh a t u s e rs wa n t . If we a re g ivin g co m m a n d s fro m ke yb o a rd it is ca lle d co m m a n d lin e in t e rfa ce ( Us u a lly in - fro n t o f $ p ro m p t , Th is p ro m p t is d e p e n d u p o n yo u r s h e ll a n d En viro n m e n t t h a t yo u s e t o r b y yo u r S ys t e m Ad m in is t ra t o r, t h e re fo re yo u m a y g e t d iffe re n t p ro m p t ) . NOTE: To fin d yo u r s h e ll t yp e fo llo win g co m m a n d $ e c h o $ S HELL

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 ) http://www.freeos.com/guides/lsst/kershell.htm (2 of 5) [17/08/2001 17.42.02]

Linux Shell Script Tutorial

$ 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 For this Purpose

Use this Command Syntax

Example ( In front of $ Prompt)

To see date To see who's using system. Print working directory List name of files in current directory

da te

$ da te

wh o

$ w ho

p wd

$ pw d

ls

$ ls

o r d irs

To create text file NOTE: Press and hold CTRL key and press D to ca t stop or to end file (CTRL+D) To text see files To display file one full screen at a time To m o ve o r re n a m e file / d ire ct o ry To create multiple file copies with various link. After this both oldfile newfile refers to same name To remove file

> { file n a m e }

$ c a t > m y file ty pe y our te x t w h e n d o n e p re s s ^D

ca t

{ file n a m e }

$ cat

m y file

m o re

{ file n a m e }

$ m o re

m y file

mv

{file1}

{file2}

$ m v s a le s s a le s . 9 9

ln

{oldfile}

{newfile}

$ ln

rm file1

http://www.freeos.com/guides/lsst/kershell.htm (3 of 5) [17/08/2001 17.42.02]

$ rm

Page1

Book1

m y file

Linux Shell Script Tutorial

Remove all files in given directory/subdirectory. rm Use it very carefully. To change file access permissions

-rf

{dirname}

$ rm -rf

$ chmod u+x,g+wx,o+x

u - User who owns the file g - Group file owner o - User classified as other a - All other system user chmod {u|g|o|a} {+|-} {r|w|x}

{filename}

+ Set permission - Remove permission r - Read permission w - Write permission x - Execute permission Read your mail. To See more about currently login person (i..e. yourself)

oldfiles

myscript

NOTE: This command set permission for file called 'myscript' as User (Person who creates that file or directory) has execute permission (u+x) Group of file owner can write to this file as well as execute this file (g+wx) Others can only execute file but can not modify it, Since we have not given w (write permission) to them. (o+x).

mail

$ m a il

who am i

$ w ho am i

To login out

logout (OR press CTRL+D)

Send mail to other person To count lines, words and characters of given file To searches file for line that match a pattern. To sort file in following order -r Reverse normal order -n Sort in numeric order -nr Sort in reverse numeric order

mail

{user-name}

$ lo g o u t ( No t e : It m a y a s k y o u p a s s w o rd t y p e y o u r lo g in p a s s w o rd , In s o m e ca s e t h is fe a t u re is d is a b le d b y S yste m Ad m in is t ra t o r) $ m a il a s h is h

wc

{file-name}

$wc

grep

{word-to-lookup}

sort

-r -n -nr

{filename}

{filename}

http://www.freeos.com/guides/lsst/kershell.htm (4 of 5) [17/08/2001 17.42.02]

$ g re p m y file

$ s o rt

m y file fo x

m y file

Linux Shell Script Tutorial

To print last | first line of tail given file

-|+

cm p

{ linenumber } { file 1 }

To Use to compare files To print file

{filename} { file 2 } OR

d iff pr

{ file 1 } {file-name}

{ file 2 }

© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.

http://www.freeos.com/guides/lsst/kershell.htm (5 of 5) [17/08/2001 17.42.02]

$ t a il + 5

m y file

$ cm p m y file m y file . o ld $ pr

m y file

Linux Shell Script Tutorial

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 To s e e cu rre n t ly ru n n in g p ro ce s s To s t o p a n y p ro ce s s i. e . t o kill p ro ce s s To g e t in fo rm a t io n a b o u t a ll 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 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 co m m a n d a n d p ro g ra m in b a ckg ro u n d )

Us e t h is Co m m a n d

Exa m p le

ps

$ ps

kill

{ PID}

$ k ill 1 0 1 2

ps -ag

$ ps -ag

kill 0

$ k ill 0

lin u x- co m m a n d &

$ ls / - R | w c - l &

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.

http://www.freeos.com/guides/lsst/process.htm [17/08/2001 17.42.03]

Linux Shell Script Tutorial

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) ( 2 ) > > 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 END o f file . No t e t h a t If file e xis t , it will b e o p e n e d a n d n e w in fo rm a t io n / d a t a will b e writ t e n t o END o f file , wit h o u t lo s in g p re vio u s in fo rm a t io n / d a t a , An d if file is n o t e xis t , t h e n 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 d a t e co m m a n d t o a lre a d y e xis t file g ive $ d a t e > > m y file s ( 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 .

http://www.freeos.com/guides/lsst/rpf.htm (1 of 2) [17/08/2001 17.42.10]

Linux Shell Script Tutorial

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 more command So that output is printed one screen full page at a time Here output of who command is given as input to sort command So that it will print sorted list of users Here output of who command is given as input to wc command So that it will number of user who logon to system Here output of ls command is given as input to wc 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 name if he is logon or nothing is printed ( To see for particular user logon)

$ ls | more

$ who | sort

$ who | wc -l

$ ls -l | wc -l

$ who | grep raju

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.

http://www.freeos.com/guides/lsst/rpf.htm (2 of 2) [17/08/2001 17.42.10]

Linux Shell Script Tutorial

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 BASH_VERSION=1.14.7(1)

Our shell name Our shell version name

COLUMNS=80 HOME=/home/vivek

No. of columns for our screen Our home directory

LINES=25 LOGNAME=students

No. of columns for our screen Our logging name

OSTYPE=Linux PATH=/usr/bin:/sbin:/bin:/usr/sbin

Our o/s type : -) Our path settings

PS1=[\u@\h \W]\$ PWD=/home/students/Common

Our prompt settings Our current working directory

SHELL=/bin/bash USERNAME=vivek

Our shell name User name who is currently login to this PC

NOTE t h a t S o m e o f t h e a b o ve s e t t in g s ca n b e d iffe re n t in yo u r PC. Yo u ca n p rin t a n y o f t h e a b o ve va ria b le s co n t a in a s fo llo ws $ e c h o $ US ERN AME $ e c h o $ HOME Ca u t io n : Do n o t m o d ify S ys t e m va ria b le t h is ca n s o m e t im e cre a t e p ro b le m s .

Ho w t o d e fin e Us e r d e fin e d v a ria b le s ( UD V) To d e fin e UDV u s e fo llo win g s yn t a x S yn t a x: v a ria b le n a m e = v a lu e NOTE: He re 'va lu e ' is a s s ig n e d t o g ive n 'va ria b le n a m e ' a n d Va lu e m u s t b e o n rig h t s id e = s ig n Fo r e .g. $ no= 1 0 # t h is is o k $ 1 0 = no # Erro r, NOT Ok, Va lu e m u s t b e o n rig h t s id e o f = s ig n . To d e fin e va ria b le ca lle d 've ch ' h a vin g va lu e Bu s

http://www.freeos.com/guides/lsst/shellprog.htm (1 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

$ 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 ( 3 ) Va ria b le s a re ca s e - s e n s it ive , ju s t like file n a m e in Lin u x. Fo r e . g . $ no= 1 0 $ No = 1 1 $ N O= 2 0 $ n O= 2 Ab o ve a ll a re d iffe re n t va ria b le n a m e , s o t o p rin t va lu e 2 0 we h a ve t o u s e $ e c h o $ N O a n d No t a n y o f t h e fo llo win g $ e cho $ no # will p rin t 1 0 b u t n o t 2 0 $ e cho $ No # will p rin t 1 1 b u t n o t 2 0 $ e cho $ nO # will p rin t 2 b u t n o t 2 0 ( 4 ) Yo u ca n d e fin e NULL va ria b le a s fo llo ws ( NULL va ria b le is va ria b le wh ich h a s n o va lu e a t t h e t im e o f d e fin it io n ) Fo r e . g . $ ve ch= $ v e c h = "" Try t o p rin t it 's va lu e $ e c h o $ v e c h , He re n o t h in g will b e s h o wn b e ca u s e va ria b le h a s n o va lu e i. e . NULL va ria b le . ( 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 .

Ho w t o p rin t o r a c c e s s v a lu e o f UD V ( Us e r d e fin e d v a ria b le s ) To p rin t o r a cce s s UDV u s e fo llo win g s yn t a x S yn t a x: $ v a ria b le n a m e Fo r e g . To p rin t co n t a in s o f va ria b le 've ch ' $ e cho $ ve ch It will p rin t 'Bu s ' ( if p re vio u s ly d e fin e d a s ve ch = Bu s ) , To p rin t co n t a in s o f va ria b le 'n ' $ e c h o $ n It will p rin t '1 0 ' ( if p re vio u s ly d e fin e d a s n = 1 0 ) Ca u t io n : Do n o t t ry $ e c h o v e c h It will p rin t ve ch in s t e a d it s va lu e 'Bu s ' a n d $ e ch o n , It will p rin t n in s t e a d it s va lu e '1 0 ', Yo u m u s t u s e $ fo llo we d b y va ria b le n a m e . Q. 1 . Ho w t o De fin e va ria b le x wit h va lu e 1 0 a n d p rin t it o n s cre e n $ x= 10 $ e cho $ x

http://www.freeos.com/guides/lsst/shellprog.htm (2 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

Q. 2 . Ho w t o De fin e va ria b le xn wit h va lu e Ra n i a n d p rin t it o n s cre e n $ x n = Ra n i $ e cho $ xn 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) Q. 4 . Ho w t o d e fin e t wo va ria b le x= 2 0 , y= 5 a n d t h e n t o p rin t d ivis io n o f x a n d y ( i. e . x/ y) $x= 20 $ y= 5 $ e xpr x / y Q. 5 . Mo d ify a b o ve a n d s t o re d ivis io n o f x a n d y t o va ria b le ca lle d z $ x= 20 $ y= 5 $ z= ` e xpr x / y` $ e cho $ z No t e : Fo r t h ird s t a t e m e n t , re a d 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" http://www.freeos.com/guides/lsst/shellprog.htm (3 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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

No w e ve ry t im e yo u h a ve t o g ive a ll t h is d e t a ile d a s yo u wo rk in o t h e r d ire ct o ry, t h is t a ke t im e a n d yo u h a ve t o re m e m b e r co m p le t e p a t h . Th e re is a n o t h e r wa y, if yo u n o t ice t h a t a ll o f o u r p ro g ra m s ( in fo rm o f e xe cu t a b le file s ) a re m a rke d a s e xe cu t a b le a n d ca n b e d ire ct ly e xe cu t e d fro m p ro m p t fro m a n y d ire ct o ry ( To s e e e xe cu t a b le s o f o u r n o rm a l p ro g ra m g ive co m m a n d $ ls - l / b in o r ls - l / u s r/ b in ) b y t yp in g co m m a n d like $ bc $ c c m y p rg . c $ cal e t c, Ho w t h is h a p p e d ? All o u r e xe cu t a b le s file s a re in s t a lle d in d ire ct o ry ca lle d / b in a n d / b in d ire ct o ry is s e t in yo u r PATH s e t t in g , No w wh e n yo u t yp e n a m e o f a n y co m m a n d a t $ p ro m p t , wh a t s h e ll d o is it firs t lo o k t h a t co m m a n d in it s in t e rn a l p a rt ( ca lle d a s in t e rn a l co m m a n d , wh ich is p a rt o f S h e ll it s e lf, a n d a lwa ys a va ila b le t o e xe cu t e , s in ce t h e y d o n o t n e e d e xt ra e xe cu t a b le file ) , if fo u n d a s in t e rn a l co m m a n d s h e ll will e xe cu t e it , If n o t fo u n d It will lo o k fo r cu rre n t d ire ct o ry, if fo u n d s h e ll will e xe cu t e co m m a n d fro m cu rre n t d ire ct o ry, if n o t fo u n d , t h e n S h e ll will Lo o k PATH s e t t in g , a n d t ry t o fin d o u r re q u e s t e d co m m a n d s e xe cu t a b le file in a ll o f t h e d ire ct o rie s m e n t io n e d in PATH s e t t in g s , if fo u n d it will e xe cu t e it , o t h e rwis e it will g ive m e s s a g e "b a s h : xxxx : co m m a n d n o t fo u n d ", S t ill t h e re is o n e q u e s t io n re m a in ca n I ru n m y s h e ll s crip t s a m e a s t h e s e e xe cu t a b le s . Ye s yo u ca n , fo r

http://www.freeos.com/guides/lsst/shellprog.htm (4 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

t h is p u rp o s e cre a t e b in d ire ct o ry in yo u r h o m e d ire ct o ry a n d t h e n co p y yo u r t e s t e d ve rs io n o f s h e ll s crip t t o t h is b in d ire ct o ry. Aft e r t h is yo u ca n ru n yo u s crip t a s e xe cu t a b le file wit h o u t u s in g $ . / s h e ll s crip t - n a m e s yn t a x, Fo llo win g a re s t e p s $ cd $ m k d ir b in $ c p firs t ~ / b in $ firs t Ea ch o f a b o ve co m m a n d Exp la n a t io n Each of above command $ cd

Explanation Go to your home directory

$ cp first ~/bin

Now created bin directory, to install your own shell script, so that script can be run as independent program or can be accessed from any directory copy your script 'first' to your bin directory

$ first

Test whether script is running or not (It will run)

$ mkdir bin

In s h e ll s crip t co m m e n t is g ive n wit h # ch a ra ct e r. Th is co m m e n t s a re ig n o re d b y yo u r s h e ll. Co m m e n t s a re u s e d t o in d ica t e u s e o f s crip t o r p e rs o n wh o cre a t e s / m a in t a in e d s crip t , o r fo r s o m e p ro g ra m m in g e xp la n a t io n e t c. Re m e m b e r a lwa ys s e t Ex e cu t e p e rm is s io n fo r y o u s crip 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 http://www.freeos.com/guides/lsst/shellprog.htm (5 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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 ls cp

No. of argument to this command 1 2

Actual Argument foo y and y.bak

mv

2

y.bak and y.okay

tail mail

2 1

-10 and myf raj

sort date clear

3 0 0

-r, -n, and myf

NOTE: $ # h o ld s n u m b e r o f a rg u m e n t s s p e cifie d o n co m m a n d lin e . a n d $ * o r $ @ re fe r t o a ll a rg u m e n t s in p a s s e d t o s crip t . No w t o o b t a in t o t a l n o . o f Arg u m e n t t o p a rt icu la r s crip t , yo u r $ # va ria b le .

W h y Co m m a n d Lin e a rg u m e n t s re q u ire d http://www.freeos.com/guides/lsst/shellprog.htm (6 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

Le t 's t a ke rm co m m a n d , wh ich is u s e d t o re m o ve file , Bu t wh ich file yo u wa n t t o re m o ve a n d h o w yo u will yo u t a il t h is t o rm co m m a n d ( Eve n rm co m m a n d d o e s n o t a s k yo u n a m e o f file t h a t wo u ld like t o re m o ve ) . S o wh a t we d o is we writ e a s co m m a n d a s fo llo ws $ rm { file - n a m e } He re rm is co m m a n d a n d file - n a m e is file wh ich yo u wo u ld like t o re m o ve . Th is wa y yo u t a il t o rm co m m a n d wh ich file yo u wo u ld like t o re m o ve . S o we a re d o in g o n e wa y co m m u n ica t io n wit h o u r co m m a n d b y s p e cifyin g file - n a m e . Als o yo u ca n p a s s co m m a n d lin e a rg u m e n t s t o yo u r s crip t t o m a ke it m o re u s e rs frie n d ly. Bu t h o w we a d d re s s o r a cce s s co m m a n d lin e a rg u m e n t in o u r s crip t . Le t s t a ke ls co m m a n d $ ls - a / * Th is co m m a n d h a s 2 co m m a n d lin e a rg u m e n t - a a n d / * is a n o t h e r. Fo r s h e ll s crip t , $ m y s h e ll fo o b a r

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 In s h e ll if we wis h t o re fe r t h is co m m a n d lin e a rg u m e n t we re fe r a b o ve a s fo llo ws m ys h e ll it is $ 0 fo o it is $ 1 b a r it is $ 2 He re $ # will b e 2 ( S in ce fo o a n d b a r o n ly t wo Arg u m e n t s ) , Ple a s e n o t e At a t im e s u ch 9 a rg u m e n t s ca n b e u s e d fro m $ 0 . . $ 9 , Yo u ca n a ls o re fe r a ll o f t h e m b y u s in g $ * ( wh ich e xp a n d t o ` $ 0 , $ 1 , $ 2 . . . $ 9 ` ) No w t ry t o writ e fo llo win g fo r co m m a n d s , S h e ll S crip t Na m e ( $ 0 ) , No . o f Arg u m e n t s ( i. e . $ # ) , An d a ct u a l a rg u m e n t ( i. e . $ 1 , $ 2 e t c) $ s um 1 1 2 0 $ m a th 4 - 7 $ d $ bp -5 m yf + 2 0 $ ls * $ cal $ fin d BS 4 8 2 4 BI G Shell Script Name $0

No. Of Arguments to script $#

Actual Argument ($1,..$9) $0 $1 $2 $3

sum

2

11

20

math d

3 0

4

-

http://www.freeos.com/guides/lsst/shellprog.htm (7 of 19) [17/08/2001 17.42.21]

7

$4

Linux Shell Script Tutorial

bp

3

-5

ls cal

1 0

*

findBS

4

4

myf

+20

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 $ ?

http://www.freeos.com/guides/lsst/shellprog.htm (8 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

$ e cho $ ?

( 6 ) if- t h e n - fi fo r d e cis io n m a k in g is s h e ll s crip t Be fo re m a kin g a n y d e cis io n in S h e ll s crip t yo u m u s t kn o w fo llo win g t h in g s Typ e b c a t $ p ro m p t t o s t a rt Lin u x ca lcu la t o r p ro g ra m $ bc Aft e r t h is co m m a n d b c is s t a rt e d a n d wa it in g fo r yo u co m m a n d s , i. e . g ive it s o m e ca lcu la t io n a s fo llo ws t yp e 5 + 2 a s 5 + 2 7 7 is re s p o n s e o f b c i. e . a d d it io n o f 5 + 2 yo u ca n e ve n t ry 5 - 2 5 / 2 No w wh a t h a p p e n e d if yo u t yp e 5 > 2 a s fo llo ws 5 > 2 0 0 ( Ze ro ) is re s p o n s e o f b c, Ho w? He re it co m p a re 5 wit h 2 a s , Is 5 is g re a t e r t h e n 2 , ( If I a s k s a m e q u e s t io n t o yo u , yo u r a n s we r will b e YES ) In Lin u x ( b c) g ive s t h is 'YES ' a n s we r b y s h o win g 0 ( Ze ro ) va lu e . It m e a n s wh e n e ve r t h e re is a n y t yp e o f co m p a ris o n in Lin u x S h e ll It g ive s o n ly t wo a n s we r o n e is YES a n d NO is o t h e r. Linux Shell Value Zero Value (0) NON-ZERO Value (> 0)

Meaning

Example

Yes/True

0

No/False

-1, 32, 55 anything but not zero

Try fo llo win g in b c t o cle a r yo u r Id e a a n d n o t d o wn b c's re s p o n s e 5 > 12 5 = = 10 5 != 2 5 == 5 12 < 2 Your Answer

BC's Response (i.e. Linux Shell representation in zero & non-zero value) 0

Expression

Meaning to us

5 > 12

Is 5 greater than 12

NO

5 == 10 5 != 2

Is 5 is equal to 10 Is 5 is NOT equal to 2

NO YES

0 1

5 == 5 1 , < , < > 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 http://www.freeos.com/guides/lsst/shellprog.htm (10 of 19) [17/08/2001 17.42.21]

fo o wh a t will b e o u t p u t .

Linux Shell Script Tutorial

B) If b a r file n o t p re s e n t o n yo u r d is k a n d yo u g ive co m m a n d , $ . / t rm fi C) An d if yo u t yp e $ . / t rm fi, Wh a t will b e o u t p u t .

b a r 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 Operator in Shell Script

Meaning

Normal Arithmetical/ Mathematical Statements

But in Shell

-eq

is equal to

5 == 6

For [ expr ] For test statement statement with if with if command command if test 5 -eq 6 if expr [ 5 -eq 6 ]

-ne -lt

is not equal to is less than

5 != 6 5= 6 equal to

http://www.freeos.com/guides/lsst/shellprog.htm (11 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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 != string2

string1 is equal to string2 string1 is NOT equal to string2

string1 -n string1

string1 is NOT NULL or not defined 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 -s file

Meaning Non empty file

-f file -d dir

Is File exist or normal file and not a directory Is Directory exist and not a file

-w file -r file

Is writeable 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 ! expression expression1 -a expression2

Meaning Logical NOT 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

Fo r e g . Writ e S crip t a s fo llo ws $ c a t > is n u m p _ n # !/ b in / s h # http://www.freeos.com/guides/lsst/shellprog.htm (12 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

# 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 http://www.freeos.com/guides/lsst/shellprog.htm (13 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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

Fo r e . g . Writ e s crip t a s $ c a t > e lf # !/ b in / s h # # S c rip t t o t e s t if. . e lif. . . e ls e # # if [ $ 1 - g t 0 ] the n e c h o "$ 1 is p o s it iv e " e lif [ $ 1 - lt 0 ] t h e n e c h o "$ 1 is n e g a t iv e " e lif [ $ 1 - e q 0 ] the n e c h o "$ 1 is z e ro " e ls e e c h o "Op p s ! $ 1 is n o t n u m b e r, g iv e n u m b e r" fi Try a b o ve s crip t wit h $ c h m o d +x e lf $ . / e lf 1 $ . / e lf - 2 $ . / e lf 0 $ . / e lf a He re o / p fo r la s t s a m p le ru n : . / e lf: [ : - g t : u n a ry o p e ra t o r e xp e ct e d . / e lf: [ : - lt : u n a ry o p e ra t o r e xp e ct e d . / e lf: [ : - e q : u n a ry o p e ra t o r e xp e ct e d Op p s ! a is n o t n u m b e r, g ive n u m b e r Ab o ve p ro g ra m g ive s e rro r fo r la s t ru n , h e re in t e g e r co m p a ris o n is e xp e ct e d t h e re fo re e rro r like ". / e lf: [ : - g t : u n a ry o p e ra t o r e xp e ct e d " o ccu rs , b u t s t ill o u r p ro g ra m n o t ify t h is t h in g t o u s e r b y p ro vid in g m e s s a g e "Op p s ! a is n o t n u m b e r, g ive n u m b e r". ( 1 0 ) Lo o p s in S h e ll S crip t s Co m p u t e r ca n re p e a t p a rt icu la r in s t ru ct io n a g a in a n d a g a in , u n t il p a rt icu la r co n d it io n s a t is fie s . A g ro u p o f in s t ru ct io n t h a t is e xe cu t e d re p e a t e d ly is ca lle d a lo o p . ( a ) fo r lo o p S yn t a x: for { variable name } in { list } do execute one for each item in the list until the list is not finished (And repeat all statement between do and done) done 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

http://www.freeos.com/guides/lsst/shellprog.htm (14 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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 ]

http://www.freeos.com/guides/lsst/shellprog.htm (15 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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 variable n. (Here it's set to 7 ) Set variable i to 1

n=$1 i=1

This is our loop condition, here if value of i is less than 10 then, shell execute all statements between do and done Start loop

while [ $i -le 10 ] do

echo "$n * $i = `expr $i \* $n`"

i=`expr $i + 1`

done

Print multiplication table as 7*1=7 7 * 2 = 14 .... 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 will only output 7*1=7 ... ... E (infinite times) Loop stops here if i is not less than 10 i.e. condition of loop is not true. Hence loop is terminated.

Fro m t h e a b o ve d is cu s s io n n o t fo llo win g p o in t s a b o u t lo o p s ( a ) Firs t , t h e va ria b le u s e d in lo o p co n d it io n m u s t b e in it ia lize d , Ne xt e xe cu t io n o f t h e lo o p b e g in s . ( b ) A t e s t ( co n d it io n ) is m a d e a t t h e b e g in n in g o f e a ch it e ra t io n . ( c) Th e b o d y o f lo o p e n d s wit h a s t a t e m e n t t h a t m o d ifie s t h e va lu e o f t h e t e s t ( co n d it io n ) va ria b le . ( 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

http://www.freeos.com/guides/lsst/shellprog.htm (16 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

patternN)

*)

... .. command;; 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 . http://www.freeos.com/guides/lsst/shellprog.htm (17 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

S yn t a x: re a d v a rib le 1 , v a rib le 2 , . . . v a rib le N Cre a t e s crip t a s $ cat > s ayH # # S c rip t t o re a d y o u r n a m e fro m k e y - b o a rd # e c h o "Yo u r firs t n a m e p le a s e :" re a d fn a m e e c h o "He llo $ fn a m e , Le t s b e frie n d !"

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 /bin/arch /bin/ash /bin/ash.static

/bin/awk /bin/basename /bin/bash

/bin/bsh /bin/cat /bin/chgrp

/bin/chmod /bin/chown /bin/consolechars

/bin/cp /bin/cpio /bin/csh

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 /bin/ps /bin/rvi /bin/sleep /bin/touch /bin/view /bin/pwd /bin/rview /bin/sort /bin/true /bin/wcomp /bin/red /bin/sayHello /bin/stty /bin/umount /bin/xconf /bin/remadmin /bin/sed /bin/su /bin/uname /bin/ypdomainname /bin/rm /bin/setserial /bin/sync /bin/userconf /bin/zcat /bin/rmdir /bin/sfxload /bin/tar /bin/usleep /bin/rpm /bin/sh /bin/tcsh /bin/vi ( 1 4 ) co m m a n d 1 ; co m m a n d 2 To ru n t wo co m m a n d wit h o n e co m m a n d lin e . Fo r e g . $ d a t e ;w h o , Will p rin t t o d a y's d a t e fo llo we d http://www.freeos.com/guides/lsst/shellprog.htm (18 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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.

http://www.freeos.com/guides/lsst/shellprog.htm (19 of 19) [17/08/2001 17.42.21]

Linux Shell Script Tutorial

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 NOTE: - Em p t y lin e p rin t e d $ v e c h = Ca r $ e cho $ ve ch Ca r $ e x it $ e cho $ ve ch Bu s Command $ vech=Bus $ echo $vech $ /bin/bash $ echo $vech $ vech=Car $ echo $vech $ exit $ echo $vech

Meaning Create new local variable 'vech' with Bus as value in first shell Print the contains of variable vech Now load second shell in memory (Which ignores all old shell's variable) Print the contains of variable vech Create new local variable 'vech' with Car as value in second shell Print the contains of variable vech Exit from second shell return to first shell Print the contains of variable vech (Now you can see first shells variable and its value)

We ca n co p y o ld s h e ll's va ria b le t o n e w s h e ll ( i. e . firs t s h e lls va ria b le t o s e co n d s s h e ll) , s u ch va ria b le is kn o w a s Glo b a l S h e ll va ria b le . To d o t h is u s e e xp o rt co m m a n d S yn t a x: e x p o rt v a ria b le 1 , v a ria b le 2 , . . . . . v a ria b le N Fo r e . g . $ v e c h = Bu s $ e cho $ ve ch Bu s $ e x p o rt v e c h $ / b in / b a s h $ e cho $ ve ch Bu s $ e x it $ e cho $ ve ch http://www.freeos.com/guides/lsst/maspc.htm (1 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

Bu s Command $ vech=Bus $ echo $vech $ export vech $ /bin/bash $ echo $vech $ exit $ echo $vech

Meaning Create new local variable 'vech' with Bus as value in first shell Print the contains of variable vech Export first shells variable to second shell Now load second shell in memory (Old shell's variable is accessed from second shell, if they are exported ) Print the contains of variable vech Exit from second shell return to first shell 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 )

I / O Re d ire c t io n a n d file d e s c rip t o rs As yo u kn o w I/ O re d ire ct o rs a re u s e d t o s e n d o u t p u t o f co m m a n d t o file o r t o re a d in p u t fro m file . ( S e e In p u t / Ou t p u t re d ire ct io n ) . No w co n s id e r fo llo win g e xa m p le s $ cat > m yf Th is is m y file ^D Ab o ve co m m a n d s e n d o u t p u t o f ca t co m m a n d t o m yf file $ cal Ab o ve co m m a n d p rin t s ca le n d a r o n s cre e n , b u t if yo u wis h t o s t o re t h is ca le n d a r t o file t h e n g ive co m m a n d $ cal > m ycal Th e ca l co m m a n d s e n d o u t p u t t o m yca l file . Th is is ca lle d o u t p u t re d ire ct io n $ s o rt 10 -2 0 11 2 ^D -2 0 2 10 11 He re s o rt co m m a n d t a ke s in p u t fro m ke yb o a rd a n d t h e n s o rt s t h e n u m b e r, If we wis h t o t a ke in p u t fro m file g ive co m m a n d a s fo llo ws $ cat > nos 10 http://www.freeos.com/guides/lsst/maspc.htm (2 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

-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

stdin

File Descriptors number 0

as Standard input

Keyboard

stdout stderr

1 2

as Standard output as Standard error

Screen Screen

Standard File

Use

Example

By d e fa u lt in Lin u x e ve ry p ro g ra m h a s t h re e file s a s s o cia t e d wit h it , ( wh e n we s t a rt o u r p ro g ra m t h e s e t h re e file s a re a u t o m a t ica lly o p e n e d b y yo u r s h e ll) Th e u s e o f firs t t wo file s ( i. e . s t d in a n d s t d o u t ) , a re a lre a d y s e e n b y u s . Th e la s t file s t d e rr ( n u m b e re d a s 2 ) is u s e d b y o u r p ro g ra m t o p rin t e rro r o n s cre e n . Yo u ca n re d ire ct t h e o u t p u t fro m a file d e s crip t o r d ire ct ly t o file wit h fo llo win g S yn t a x: file - d e s crip t o r- n u m b e r> file n a m e Fo r e . g . $ rm b a d _ file _ n a m e 1 1 1 rm : ca n n o t re m o ve ` b a d _ file _ n a m e 1 1 1 ': No s u ch file o r d ire ct o ry , is t h e o u t p u t ( e rro r) o f t h e a b o ve p ro g ra m . No w if we t ry t o re d ire ct t h is e rro r- o u t p u t t o file , it ca n n o t b e s e n d t o file $ rm b a d _ file _ n a m e 1 1 1 > e r S t ill it p rin t s o u t p u t o n s t d e rr a s rm : ca n n o t re m o ve ` b a d _ file _ n a m e 1 1 1 ': No s u ch file o r d ire ct o ry, An d if yo u s e e e r file a s $ ca t e r , Th is file is e m p t y, s in ce o u t p u t is s e n d t o e rro r d e vice a n d yo u ca n n o t re d ire ct it t o co p y t h is e rro r- o u t p u t t o yo u r file 'e r'. To o ve rco m e t h is we h a ve t o u s e fo llo win g co m m a n d $ rm b a d _ file _ n a m e 1 1 1 2 > e r No t e t h a t n o s p a ce a re a llo we d b e t we e n 2 a n d > , Th e 2 > e r d ire ct s t h e s t a n d a rd e rro r o u t p u t t o file . 2 n u m b e r is d e fa u lt n u m b e r o f s t d e rr file . No w co n s id e r a n o t h e r e xa m p le , h e re we a re writ in g s h e ll s crip t a s fo llo ws $ cat > de m o s cr if [ $ # - n e 2 ] the n e c h o "Erro r : N u m b e r a re n o t s u p p lie d " e c h o "Us a g e : $ 0 n u m b e r1 n u m b e r2 " e x it 1 fi ans = ` e xpr $ 1 + $ 2 ` e c h o "S u m is $ a n s " 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 He re fo r firs t s a m p le ru n , o u r s crip t p rin t s e rro r m e s s a g e in d ica t in g t h a t we h a ve n o t g ive n t wo n u m b e r. Fo r s e co n d s a m p le ru n , we h a ve re d ire ct o u t p u t o f o u r s crip t t o file , s in ce it 's e rro r we h a ve t o s h o w it t o

http://www.freeos.com/guides/lsst/maspc.htm (3 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

u s e r, It m e a n s we h a ve t o p rin t o u r e rro r m e s s a g e o n s t d e rr n o t o n s t d o u t . To o ve rco m e t h is p ro b le m re p la ce a b o ve e ch o s t a t e m e n t s a s fo llo ws e c h o "Erro r : N u m b e r a re n o t s u p p lie d " 1 > &2 e c h o "Us a g e : $ 0 n u m b e r1 n u m b e r2 " 1 > &2 No w if yo u ru n a s $ . / d e m o s cr > e r1 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 It will p rin t e rro r m e s s a g e o n s t d e rr a n d n o t o n s t d o u t . Th e 1 > &2 a t t h e e n d o f e ch o s t a t e m e n t , d ire ct s t h e s t a n d a rd o u t p u t ( s t d o u t ) t o s t a n d a rd e rro r ( s t d e rr) d e vice . S yn t a x: fro m > &d e s t in a t io n

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 # http://www.freeos.com/guides/lsst/maspc.htm (4 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

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 } S a ve t h e file a n d e xit it , a ft e r a ll t h is m o d ifica t io n yo u r file m a y lo o k like a s fo llo ws # / 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 } To ru n fu n ct io n firs t co m p le t e ly lo g o u t b y t yp in g e xit a t t h e $ p ro m p t ( Or p re s s CTRL + D, No t e yo u m a y h a ve t o t yp e e xit ( CTRL + D) t wice if yo u lo g in t o ro o t a cco u n t b y u s in g s u co m m a n d ) , t h e n lo g in a n d t yp e $ t o d a y , t h is wa y t o d a y( ) is a va ila b le t o a ll u s e r in yo u r s ys t e m , If yo u wa n t t o a d d p a rt icu la r fu n ct io n t o p a rt icu la r u s e r t h e n o p e n . b a s h rc file in yo u r h o m e d ire ct o ry a s fo llo ws # v i . b a s h rc OR # m c e d it . b a s h rc At t h e e n d o f file a d d fo llo win g in . b a s h rc file 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 } S a ve t h e file a n d e xit it , a ft e r a ll t h is m o d ifica t io n yo u r file m a y lo o k like a s fo llo ws # . 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 ( )

http://www.freeos.com/guides/lsst/maspc.htm (5 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

{ 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 } To ru n fu n ct io n firs t lo g o u t b y t yp in g e xit a t t h e $ p ro m p t ( Or p re s s CTRL + D ) , t h e n lo g o n a n d t yp e $ S a y Bu y , t h is wa y S a yBu y( ) is a va ila b le t o o n ly in yo u r lo g in a n d n o t t o a ll u s e r in s ys t e m , Us e . b a s h rc file in yo u r h o m e d ire ct o ry t o a d d Us e r s p e cific a lia s e s a n d fu n ct io n s o n ly. ( Tip : If yo u wa n t t o s h o w s o m e m e s s a g e o r wa n t t o p e rfo rm s o m e a ct io n wh e n yo u lo g o u t , Op e n file . b a s h _ lo g o u t in yo u r h o m e d ire ct o ry a n d a d d yo u r s t u ff h e re Fo r e . g . Wh e n e ve r I lo g o u t , I wa n t t o s h o w m e s s a g e Bu y! Th e n o p e n yo u r . b a s h _ lo g o u t file u s in g t e xt e d it o r s u ch a s vi a n d a d d s t a t e m e n t e c h o "Bu y $ LOGN AME, P re s s a k e y . . . " re a d S a ve a n d e xit fro m t h e file . Th e n t o t e s t t h is lo g o u t fro m yo u r s ys t e m b y p re s s in g CTRL + D ( o r t yp e e x it ) im m e d ia t e ly yo u will s e e m e s s a g e "Bu y xxxxx, Pre s s a ke y. . . ", a ft e r p re s s in g ke y yo u will b e e xit e d . )

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 " http://www.freeos.com/guides/lsst/maspc.htm (6 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

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 while : do

Explanation Start infinite loop, this loop will only break if you select 5 ( i.e. Exit/Stop menu item) as your menu choice Start loop

clear

Clear the screen, each and every time

echo "-------------------------------------" echo " Main Menu " echo "-------------------------------------" echo "[1] Show Todays date/time" echo "[2] Show files in current directory" echo "[3] Show calendar" echo "[4] Start editor to write letters" echo "[5] Exit/Stop" echo "======================="

Show menu on screen with menu items

echo -n "Enter your menu choice [1-5]: " read yourch case $yourch in 1) echo "Today is `date` , press a key. . ." ; read ;; 2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;; 3) cal ; echo "Press a key. . ." ; read ;; 4) vi ;; 5) exit 0 ;; *) echo "Opps!!! Please select choice 1,2,3,4, or 5"; echo "Press a key. . ." ; read ;; esca done

Ask user to enter menu item number Read menu item number from user

Take appropriate action according to selected menu item, If menu item is not between 1 - 5, then show error and ask user to input number between 1-5 again

Stop loop , if menu item number is 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 :

http://www.freeos.com/guides/lsst/maspc.htm (7 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

$ 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: dialog --title {title} --backtitle {backtitle} {Box options} where Box options can be any one of following --yesno {text} {height} {width} --msgbox {text} {height} {width} --infobox {text} {height} {width} --inputbox {text} {height} {width} [{init}] --textbox {file} {height} {width} --menu {text} {height} {width} {menu} {height} {tag1} item1}...

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 Ab o ve s crip t cre a t e s ye s n o t yp e d ia lo g b o x, wh ich is u s e d t o a s k s o m e q u e s t io n s t o t h e u s e r , a n d a n s we r t o t h o s e q u e s t io n e it h e r ye s o r n o . Aft e r a s kin g q u e s t io n h o w d o we kn o w, wh e t h e r u s e r h a s p re s s ye s o r n o b u t t o n ? Th e a n s we r is e xit s t a t u s , if u s e r p re s s ye s b u t t o n e xit s t a t u s will b e ze ro , if u s e r p re s s n o b u t t o n e xit s t a t u s will b e o n e a n d if u s e r p re s s Es ca p e ke y t o ca n ce l d ia lo g b o x e xit s t a t u s will b e o n e 2 5 5 . Th a t is wh a t we h a ve t e s t e d in o u r a b o ve s h e ll a s http://www.freeos.com/guides/lsst/maspc.htm (8 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

Statement sel=$? case $sel in 0) echo "You select to delete file";; 1) echo "You select not to delete file";; 255) echo "Canceled by you by pressing [Escape] key";; esac

Meaning Get exit status of dialog utility Now take action according to exit status of dialog utility, if exit status is 0 , delete file, if exit status is 1 do not delete file and if 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 0 1

Meaning Command is successful 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 \

http://www.freeos.com/guides/lsst/maspc.htm (9 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

S e le c t " 1 5 5 0 D a t e / t im e Ca le n d a r Ed it o r

3\ "S h o w s D a t e a n d Tim e "\ "To s e e c a le n d a 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" 15

This is text show before menu Height of box

50 3

Width of box Height of menu First menu item called as tag1 (i.e. Date/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 (i.e. "To see calendar") First menu item called as tag3 (i.e. Editor) and description for menu item called as item3 (i.e."To start vi editor") Send sleeted menu item (tag) to this temporary file

Date/time

"Shows Date and Time"

Calendar

"To see calendar "

Editor

"To start vi editor "

2>/tmp/menuitem.$$

Aft e r cre a t in g m e n u s , u s e r s e le ct s m e n u - it e m b y p re s s in g e n t e r ke y t h e s e le ct e d ch o ice is re d ire ct e d t o t e m p o ra ry file , Ne xt t h is m e n u - it e m is re t rie ve d fro m t e m p o ra ry file a n d fo llo win g ca s e s t a t e m e n t co m p a re t h e m e n u - it e m a n d t a ke s a p p ro p ria t e s t e p a cco rd in g t o s e le ct e d m e n u it e m . As yo u s e e , d ia lo g u t ilit y a llo ws m o re p o we rfu l u s e r in t e ra ct io n t h e n t h e o ld e r re a d a n d e ch o s t a t e m e n t . Th e o n ly p ro b le m wit h d ia lo g u t ilit y is it wo rk s lo wly.

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 http://www.freeos.com/guides/lsst/maspc.htm (10 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

$ 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 e c h o "D a t a e n t ry s t a rt d a t a : ` d a t e ` " > / t m p / in p u t 0 . $ $ # # S e t a in fin it e lo o p # w h ile : do e c h o - n "Ap p o in t m e n t Tit le :" re a d n a e c h o - n "Ap p o in t t im e :" re a d t i e c h o - n "An y Re m a rk :" re a d re m a rk e c h o - n "I s d a t a o k a y ( y / n ) ? " re a d a n s 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 http://www.freeos.com/guides/lsst/maspc.htm (11 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

file . Th e n o n e in fin it e lo o p b e g in s , wh ich a s k a p p o in t m e n t t it le , t im e a n d re m a rk, if t h is in fo rm a t io n is co rre ct it s writ t e n t o t e m p o ra ry file , Aft e r t h a t s crip t a s k u s e r , wh e t h e r h e / s h e wa n t s a d d n e xt a p p o in t m e n t re co rd , if ye s t h e n n e xt re co rd is a d d e d , o t h e rwis e a ll re co rd s a re co p ie d fro m t e m p o ra ry file t o d a t a b a s e file a n d t h e n lo o p will b e t e rm in a t e d . Yo u ca n vie w yo u r d a t a b a s e file b y u s in g ca t co m m a n d . No w p ro b le m is t h a t wh ile ru n n in g t h is s crip t , if yo u p re s s CTRL + C, yo u r s h e ll s crip t g e t s t e rm in a t e d a n d t e m p o ra ry file a re le ft in / t m p d ire ct o ry. Fo r e . g . t ry a s fo llo ws $ . / 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 a d d in g a t le a s t o n e a p p o in t m e n t re co rd t o t e m p o ra ry file p re s s CTRL+ C, Ou r s crip t g e t t e rm in a t e d , a n d it le ft t e m p o ra ry file in / t m p d ire ct o ry, yo u ca n ch e ck t h is b y g ivin g co m m a n d a s fo llo ws $ ls / t m p / in p u t * Ou r s crip t n e e d s t o d e t e ct wh e n s u ch s ig n a l ( e ve n t ) o ccu rs , To a ch ie ve t h is we h a ve t o firs t d e t e ct S ig n a l u s in g t ra p co m m a n d S yn t a x: t ra p { co m m a n d s } { s ig n a l n u m b e r lis t } S ig n a l Nu m b e r 0

Wh e n o ccu rs s h e ll e xit

1 2 3 9

hangup in t e rru p t ( CTRL+ C) q u it kill ( ca n n o t b e ca u g h 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 e c h o "D a t a e n t ry s t a rt d a t a : ` d a t e ` " > / t m p / in p u t 0 . $ $ # # S e t a in fin it e lo o p # w h ile : do

http://www.freeos.com/guides/lsst/maspc.htm (12 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

e cho re a d e cho re a d e cho re a d e cho re a d

- n "Ap p o in t m e n t Tit le :" na - n "Ap p o in t t im e :" ti - n "An y Re m a rk :" re m a rk - n "I s d a t a o k a y ( y / n ) ? " ans

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

http://www.freeos.com/guides/lsst/maspc.htm (13 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

- 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

http://www.freeos.com/guides/lsst/maspc.htm (14 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

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 S e e b e ca u s e o f g e t o p t s , we ca n p a s s co m m a n d lin e a rg u m e n t in d iffe re n t s t yle . Fo llo win g a re in va lid o p t io n s fo r a n i s crip t $ a n i - n La s s ie - a 4 - s Fe m a l - w 2 0 Kg He re n o s p a ce b e t we e n o p t io n a n d t h e ir va lu e . $ a n i - n La s s ie - a 4 - s Fe m a l- w 2 0 Kg $ a n i - n La s s ie - a 4 - s Fe m a le - w 2 0 Kg - c Ma m m a l He re - c is n o t o n e o f t h e o p t io n s .

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 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 .

s u p p lie s a s co m m a n d lin e

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 .

http://www.freeos.com/guides/lsst/maspc.htm (15 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

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 1 . Writ e s crip t t o d e t e rm in e wh e t h e r g ive n file e xis t o r n o t , file n a m e is s u p p lie d a s co m m a n d lin e a rg u m e n t , a ls o ch e ck fo r s u fficie n t n u m b e r o f co m m a n d lin e a rg u m e n t An s we r: S e e Q1 1 s h e ll S crip t . Q. 1 2 . Writ e s crip t t o d e t e rm in e wh e t h e r g ive n co m m a n d lin e a rg u m e n t ( $ 1 ) co n t a in s "* " s ym b o l o r n o t , if $ 1 d o e s n o t co n t a in s "* " s ym b o l a d d it t o $ 1 , o t h e rwis e s h o w m e s s a g e "S ym b o l is n o t re q u ire d ". Fo r e . g . If we ca lle d t h is s crip t Q1 2 t h e n a ft e r g ivin g , $ Q1 2 / b in He re $ 1 is / b in , it s h o u ld ch e ck wh e t h e r "* " s ym b o l is p re s e n t o r n o t if n o t it s h o u ld p rin t Re q u ire d i. e . / b in / * , a n d if s ym b o l p re s e n t t h e n S ym b o l is n o t re q u ire d m u s t b e p rin t e d . Te s t yo u r s crip t a s $ Q1 2 / b in $ Q1 2 / b in / * An s we r: S e e Q1 2 s h e ll S crip t Q. 1 3 . Writ e s crip t t o p rin t co n t a in s o f file fro m g ive n lin e n u m b e r t o n e xt g ive n n u m b e r o f lin e s . Fo r e . g . If we ca lle d t h is s crip t a s Q1 3 a n d ru n a s $ Q1 3 5 5 m y f , He re p rin t co n t a in s o f 'm yf' file fro m lin e n u m b e r 5 t o n e xt 5 lin e o f t h a t file . An s we r: S e e Q1 3 s h e ll S crip t Q. 1 4 . Writ e s crip t t o im p le m e n t g e t o p t s s t a t e m e n t , yo u r s crip t s h o u ld u n d e rs t a n d fo llo win g co m m a n d lin e a rg u m e n t ca lle d t h is s crip t Q1 4 , Q1 4 - c - d - m - e Wh e re o p t io n s wo rk a s - c cle a r t h e s cre e n - d s h o w lis t o f file s in cu rre n t wo rkin g d ire ct o ry - m s t a rt m c ( m id n ig h t co m m a n d e r s h e ll) , if in s t a lle d - e { e d it o r } s t a rt t h is { e d it o r } if in s t a lle d An s we r: S e e Q1 4 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

http://www.freeos.com/guides/lsst/maspc.htm (16 of 17) [17/08/2001 17.42.32]

Linux Shell Script Tutorial

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

Da t e / t im e

To s e e cu rre n t d a t e t im e

Ca le n d a r

To s e e cu rre n t ca le n d a r

De le t e

To d e le t e s e le ct e d file

Exit

To Exit t h is s h e ll s crip t

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 u t ilit y 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, 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 / 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

No t e : Cre a t e fu n ct io n fo r a ll a ct io n fo r e . g . To s h o w d a t e / t im e o n s cre e n cre a t e fu n ct io n s h o w_ d a t e t im e ( ) . An s we r: S e e Q1 8 s h e ll S crip t . Q. 1 9 . Writ e s h e ll s crip t t o s h o w va rio u s s ys t e m co n fig u ra t io n like 1 ) Cu rre n t ly lo g g e d u s e r a n d h is lo g n a m e 2 ) Yo u r cu rre n t s h e ll 3 ) Yo u r h o m e d ire ct o ry 4 ) Yo u r o p e ra t in g s ys t e m t yp e 5 ) Yo u r cu rre n t p a t h s e t t in g 6 ) Yo u r cu rre n t wo rkin g d ire ct o ry 7 ) S h o w Cu rre n t ly lo g g e d n u m b e r o f u s e rs 8 ) Ab o u t yo u r o s a n d ve rs io n , re le a s e n u m b e r , ke rn e l ve rs io n 9 ) S h o w a ll a va ila b le s h e lls 1 0 ) S h o w m o u s e s e t t in g s 1 1 ) S h o w co m p u t e r cp u in fo rm a t io n like p ro ce s s o r t yp e , s p e e d e t c 1 2 ) S h o w m e m o ry in fo rm a t io n 1 3 ) S h o w h a rd d is k in fo rm a t io n like s ize o f h a rd - d is k, ca ch e m e m o ry, m o d e l e t c 1 4 ) File s ys t e m ( Mo u n t e d ) An s we r: S e e Q1 9 s h e ll S crip t . Th a t 's a ll!, Th a n ks fo r re a d in g , Th is t u t o ria l e n d s h e re , b u t Lin u x p ro g ra m m in g e n viro n m e n t is b ig , rich a n d p ro d u ct ive ( As yo u s e e fro m a b o ve s h e ll s crip t e xe rcis e ) , yo u s h o u ld co n t in u e t o re a d m o re a d va n ce t o p ics a n d b o o ks . If yo u h a ve a n y s u g g e s t io n o r n e w id e a s o r p ro b le m wit h t h is t u t o ria l, p le a s e fe e l fre e t o co n t a ct m e . My e - m a il is g it e - vive k@u s a . n e t . © 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.

http://www.freeos.com/guides/lsst/maspc.htm (17 of 17) [17/08/2001 17.42.32]