You are on page 1of 13

unlx Shell ScrlpLlng 1uLorlal

Ashley !S Mllls
ashley[ashleymlllscom
CopyrlghL 2003 1he unlverslLy Cf 8lrmlngham
1able of ConLenLs
1lnLroducLlon
1
2LnvlronmenL
1
3 Shell ScrlpLlng
1
31 Shell ScrlpLlng lnLroducLlon 1
32 Shell ScrlpLlng 8aslcs 2
321 Command 8edlrecLlon and lpellnes 2
322varlables 2
323 ConLrol ConsLrucLs 4
324luncLlons 6
1 lnLroducLlon
1lme ls preclous lL ls nonsenselcal Lo wasLe Llme Lyplng a frequenLly used sequence of commands aL a
command prompL more
especlally lf Lhey are abnormally long or complex ScrlpLlng ls a way by whlch one can allevlaLe Lhls
necesslLy by auLomaLlng Lhese
command sequences ln order Lo make ones llfe aL Lhe shell easler and more producLlve ScrlpLlng ls all
abouL maklng Lhe compuLer
Lhe Lool do Lhe work Popefully by Lhe end of Lhls LuLorlal you should have a good ldea of Lhe klnds of
scrlpLlng languages avallable for unlx and how Lo apply Lhem Lo your problems
2 LnvlronmenL
ln order Lo peruse Lhls LuLorlal wlLh ease lL ls probably necessary Lo know a llLLle blL abouL how unlx
works lf you are new Lo unlx
you should read Lhe documenLaLlon Conflgurlng A unlx Worklng LnvlronmenL
/unlxenvars/unlxenvarshomehLml lf you are
noL new Lo unlx you should browse lL Lo check LhaL you know everyLhlng lL dlscusses
unlx conLalns many wonderful and sLrange commands LhaL can be very useful ln Lhe world of scrlpLlng
Lhe more of Lhe Lools you
know and Lhe beLLer you know Lhem Lhe more use you wlll flnd for Lhem ln your scrlpLlng MosL of Lhe
unlx commands and many
of Lhe bullLln commands have man pages man pages conLaln Lhe usage lnsLrucLlons and oLher sLuff
perLalnlng Lo Lhe parenL Lool
1hey are noL always very clear and may requlre readlng several Llmes ln order Lo access a man page ln
unlx Lhe followlng command sequence ls applled
man command
lf a man page exlsLs for Lhe command speclfled Lhe lnLernal vlewer wlll be lnvoked and you wlll be able
Lo read abouL Lhe varlous
opLlons and usage lnsLrucLlons eLc
3 Shell ScrlpLlng
31 Shell ScrlpLlng lnLroducLlon
unlx uses shells Lo accepL commands glven by Lhe user Lhere are qulLe a few dlfferenL shells avallable
1he mosL commonly used
shells are SP(8ourne SPell) CSP(C SPell) and kSP(korn SPell) mosL of Lhe oLher shells you encounLer wlll
be varlanLs of Lhese
shells and wlll share Lhe same synLax kSP ls based on SP and so ls 8ASP(8ourne agaln shell)
1CSP(LxLended C SPell) ls
based on CSP
1he varlous shells all have bullL ln funcLlons whlch allow for Lhe creaLlon of shell scrlpLs LhaL ls Lhe
sLrlnglng LogeLher of shell
commands and consLrucLs Lo auLomaLe whaL can be auLomaLed ln order Lo make llfe easler for Lhe user
WlLh all Lhese dlfferenL shells avallable whaL shell should we scrlpL ln? 1haL ls debaLable lor Lhe
purpose of Lhls LuLorlal we wlll
be uslng SP because lL ls pracLlcally guaranLeed Lo be avallable on mosL unlx sysLems you wlll encounLer
or be supporLed by Lhe
SP based shells ?our defaulL shell may noL be SP lorLunaLely we do noL have Lo be uslng a speclflc shell
ln order Lo explolL lLs
feaLures because we can speclfy Lhe shell we wanL Lo lnLerpreL our shell scrlpL wlLhln Lhe scrlpL lLself by
lncludlng Lhe followlng ln
Lhe flrsL llne
#!/paLh/Lo/shell
usually anyLhlng followlng (#) ls lnLerpreLed as a commenL and lgnored buL lf lL occurs on Lhe flrsL llne
wlLh a (!) followlng lL ls
LreaLed as belng speclal and Lhe fllename followlng Lhe (!) ls consldered Lo polnL Lo Lhe locaLlon of Lhe
shell LhaL should lnLerpreL Lhe
1scrlpL
When a scrlpL ls execuLed lL ls belng lnLerpreLed by an lnvocaLlon of Lhe shell LhaL ls runnlng lL Pence
Lhe shell ls sald Lo be runnlng nonlnLeracLlvely when Lhe shell ls used normally lL ls sald Lo be runnlng
lnLeracLlvely
noLe
1here are many varlaLlons on Lhe baslc commands and exLra lnformaLlon whlch ls Loo speclflc Lo be
menLloned ln Lhls
shorL LuLorlal you should read Lhe man page for your shell Lo geL a more comprehenslve ldea of Lhe
opLlons avallable Lo
you 1hls LuLorlal wlll concenLraLe on hlghllghLlng Lhe mosL ofLen used and useful commands and
consLrucLs
32 Shell ScrlpLlng 8aslcs
321 Command 8edlrecLlon and lpellnes
8y defaulL a normal command accepLs lnpuL from sLandard lnpuL whlch we abbrevlaLe Lo sLdln
sLandard lnpuL ls Lhe command llne
ln Lhe form of argumenLs passed Lo Lhe command 8y defaulL a normal command dlrecLs lLs ouLpuL Lo
sLandard ouLpuL whlch we
abbrevlaLe Lo sLdouL sLandard ouLpuL ls usually Lhe console dlsplay lor some commands Lhls may be
Lhe deslred acLlon buL oLher
Llmes we may wlsh Lo geL our lnpuL for a command from somewhere oLher Lhan sLdln and dlrecL our
ouLpuL Lo somewhere oLher
Lhan sLdouL 1hls ls done by redlrecLlon
- We use Lo redlrecL sLdouL Lo a flle for lnsLance lf we wanLed Lo redlrecL a dlrecLory llsLlng generaLed
by Lhe ls we could do
Lhe followlng
ls flle
- We use Lo speclfy LhaL we wanL Lhe command lmmedlaLely before Lhe redlrecLlon symbol Lo geL lLs
lnpuL from Lhe source
speclfled lmmedlaLely afLer Lhe symbol for lnsLance we could redlrecL Lhe lnpuL Lo grep(whlch searches
for sLrlngs wlLhln
flles) so LhaL lL comes from a flle llke Lhls
grep searchLerm flle
- We use Lo append sLdouL Lo a flle for lnsLance lf we wanLed Lo append Lhe daLe Lo Lhe end of a flle
we could redlrecL Lhe
ouLpuL from daLe llke so
daLe flle
- Cne can redlrecL sLandard error (sLderr) Lo a flle by uslng 2 lf we wanLed Lo redlrecL Lhe sLandard
error from commandA Lo a
flle we would use
commmandA 2
lpellnes are anoLher form of redlrecLlon LhaL are used Lo chaln commands so LhaL powerful composlLe
commands can be consLrucLed Lhe plpe symbol | Lakes Lhe sLdouL from Lhe command precedlng lL and
redlrecLs lL Lo Lhe command followlng lL
ls l | grep searchword | sorL r
1he example above flrsLs requesLs a long (l dlrecLory llsLlng of Lhe currenL dlrecLory uslng Lhe ls
command Lhe ouLpuL from Lhls ls
Lhen plped Lo grep whlch fllLers ouL all Lhe llsLlngs conLalnlng Lhe searchword and Lhen flnally plpes Lhls
Lhrough Lo sorL whlch Lhen
sorLs Lhe ouLpuL ln reverse (r sorL Lhen passes Lhe ouLpuL on normally Lo sLdouL
322 varlables
3221 varlables
When a scrlpL sLarLs all envlronmenL varlables are Lurned lnLo shell varlables new varlables can be
lnsLanLlaLed llke Lhls
namevalue
?ou musL do lL exacLly llke LhaL wlLh no spaces elLher slde of Lhe equals slgn Lhe name musL only be
made up of alphabeLlc characLers numerlc characLers and underscores lL cannoL begln wlLh a numerlc
characLer ?ou should avold uslng keywords llke for or
anyLhlng llke LhaL Lhe lnLerpreLer wlll leL you use Lhem buL dolng so can lead Lo obfuscaLed code )
varlables are referenced llke Lhls $name here ls an example
#!/bln/sh
msg1Pello
msg21here!
echo $msg1 $msg2
1hls would echo Pello 1here! Lo Lhe console dlsplay lf you wanL Lo asslgn a sLrlng Lo a varlable and Lhe
sLrlng conLalns spaces
unlx Shell ScrlpLlng 1uLorlal
2you should enclose Lhe sLrlng ln double quoLes () Lhe double quoLes Lell Lhe shell Lo Lake Lhe conLenLs
llLerally and lgnore keywords however a few keywords are sLlll processed ?ou can sLlll use $ wlLhln a
() quoLed sLrlng Lo lnclude varlables
#!/bln/sh
msg1one
msg2$msg1 Lwo
msg3$msg2 Lhree
echo $msg3
Would echo one Lwo Lhree Lo Lhe screen 1he escape characLer can also be used wlLhln a double
quoLed secLlon Lo ouLpuL speclal
characLers Lhe escape characLer ls lL ouLpuLs Lhe characLer lmmedlaLely followlng lL llLerally so
would ouLpuL A speclal case
ls when Lhe escape characLer ls followed by a newllne Lhe shell lgnores Lhe newllne characLer whlch
allows Lhe spreadlng of long
commands LhaL musL be execuLed on a slngle llne ln reallLy over mulLlple llnes wlLhln Lhe scrlpL 1he
escape characLer can be used
anywhere else Loo LxcepL wlLhln slngle quoLes
Surroundlng anyLhlng wlLhln slngle quoLes causes lL Lo be LreaLed as llLeral LexL LhaL ls lL wlll be passed
on exacLly as lnLended Lhls
can be useful for sendlng command sequences Lo oLher flles ln order Lo creaLe new scrlpLs because Lhe
LexL beLween Lhe slngle
quoLes wlll remaln unLouched lor example
#!/bln/sh
echo msgPello World! hello
echo echo $msg hello
chmod 700 hello
/hello
1hls would cause msgPello World! Lo be echoed and redlrecLed Lo Lhe flle hello echo $msg would
Lhen be echoed and
redlrecLed Lo Lhe flle hello buL Lhls Llme appended Lo Lhe end 1he chmod llne changes Lhe flle
permlsslons of hello so LhaL we
can execuLe lL 1he flnal llne execuLes hello causlng lL ouLpuL Pello World lf we had noL used llLeral
quoLes we never would
have had Lo use escape characLers Lo ensure LhaL ($) and () were echoed Lo Lhe flle Lhls makes Lhe code
a llLLle clearer
A varlable may be referenced llke so $vA8lA8LLnAML Lhls allows one Lo place characLers lmmedlaLely
precedlng Lhe varlable
llke $vA8lA8LLnAMLaaa wlLhouL Lhe shell lnLerpreLlng aaa as belng parL of Lhe varlable name
3222 Command Llne ArgumenLs
Command llne argumenLs are LreaLed as speclal varlables wlLhln Lhe scrlpL Lhe reason l am calllng Lhem
varlables ls because Lhey
can be changed wlLh Lhe shlfL command 1he command llne argumenLs are enumeraLed ln Lhe followlng
manner $0 $1 $2 $3 $4
$3 $6 $7 $8 and $9 $0 ls speclal ln LhaL lL corresponds Lo Lhe name of Lhe scrlpL lLself $1 ls Lhe flrsL
argumenL $2 ls Lhe second
argumenL and so on 1o reference afLer Lhe nlnLh argumenL you musL enclose Lhe number ln brackeLs
llke Lhls $nn ?ou can use
Lhe shlfL command Lo shlfL Lhe argumenLs 1 varlable Lo Lhe lefL so LhaL $2 becomes $1 $1 becomes $0
and so on $0 geLs scrapped
because lL has nowhere Lo go Lhls can be useful Lo process all Lhe argumenLs uslng a loop uslng one
varlable Lo reference Lhe flrsL
argumenL and shlfLlng unLll you have exhausLed Lhe argumenLs llsL
As well as Lhe commandllne argumenLs Lhere are some speclal bullLln varlables
- $# represenLs Lhe parameLer counL useful for conLrolllng loop consLrucLs LhaL need Lo process each
parameLer
- $[ expands Lo all Lhe parameLers separaLed by spaces useful for passlng all Lhe parameLers Lo some
oLher funcLlon or program
- $ expands Lo Lhe flags(opLlons) Lhe shell was lnvoked wlLh useful for conLrolllng program flow based
on Lhe flags seL
- $$ expands Lo Lhe process ld of Lhe shell lnnovaLed Lo run Lhe scrlpL useful for creaLlng unlque
Lemporary fllenames relaLlve Lo
Lhls lnsLanLlaLlon of Lhe scrlpL
noLe
1he commandllne argumenLs wlll be referred Lo as parameLers from now on Lhls ls because SP also
allows Lhe deflnlLlon
of funcLlons whlch can Lake parameLers and when called Lhe $n famlly wlll be redeflned hence Lhese
varlables are always
parameLers lLs [usL LhaL ln Lhe case of Lhe parenL scrlpL Lhe parameLers are passed vla Lhe command
llne Cne excepLlon ls
$0 whlch ls always seL Lo Lhe name of Lhe parenL scrlpL regardless of wheLher lL ls lnslde a funcLlon or
noL
3223 Command SubsLlLuLlon
ln Lhe words of Lhe SP manual Command subsLlLuLlon allows Lhe ouLpuL of a command Lo be
subsLlLuLed ln place of Lhe command
name lLself" 1here are Lwo ways Lhls can be done 1he flrsL ls Lo enclose Lhe command llke Lhls
$(command)
1he second ls Lo enclose Lhe command ln back quoLes llke Lhls
unlx Shell ScrlpLlng 1uLorlal
3`command`
1he command wlll be execuLed ln a subshell envlronmenL and Lhe sLandard ouLpuL of Lhe shell wlll
replace Lhe command subsLlLuLlon when Lhe command compleLes
3224 ArlLhmeLlc Lxpanslon
ArlLhmeLlc expanslon ls also allowed and comes ln Lhe form
$((expresslon))
1he value of Lhe expresslon wlll replace Lhe subsLlLuLlon Lg
!#/bln/sh
echo $((1 + 3 + 4))
Wlll echo 8 Lo sLdouL
323 ConLrol ConsLrucLs
1he flow of conLrol wlLhln SP scrlpLs ls done vla four maln consLrucLs lfLhenellfelse dowhlle for
and case
3231 lf1henLllfLlse
1hls consLrucL Lakes Lhe followlng generlc form 1he parLs enclosed wlLhln () and () are opLlonal
lf llsL
Lhen llsL
ellf llsL
Lhen llsL
else llsL
fl
When a unlx command exlLs lL exlLs wlLh whaL ls known as an exlL sLaLus Lhls lndlcaLes Lo anyone who
wanLs Lo know Lhe degree
of success Lhe command had ln performlng whaLever Lask lL was supposed Lo do usually when a
command execuLes wlLhouL error
lL LermlnaLes wlLh an exlL sLaLus of zero An exlL sLaLus of some oLher value would lndlcaLe LhaL some
error had occurred Lhe deLalls
of whlch would be speclflc Lo Lhe command 1he commands manual pages deLall Lhe exlL sLaLus
messages LhaL Lhey produce
A llsL ls deflned ln Lhe SP as a sequence of zero or more commands separaLed by newllnes semlcolons
or ampersands and opLlonally LermlnaLed by one of Lhese Lhree characLers hence ln Lhe generlc
deflnlLlon of Lhe lf above Lhe llsL wlll deLermlne whlch
of Lhe execuLlon paLhs Lhe scrlpL Lakes lor example Lhere ls a command called LesL on unlx whlch
evaluaLes an expresslon and lf
lL evaluaLes Lo Lrue wlll reLurn zero and wlll reLurn one oLherwlse Lhls ls how we can LesL condlLlons ln
Lhe llsL parL(s) of Lhe lf consLrucL because LesL ls a command
We do noL acLually have Lo Lype Lhe LesL command dlrecLly lnLo Lhe llsL Lo use lL lL can be lmplled by
encaslng Lhe LesL case wlLhln
() and () characLers as lllusLraLed by Lhe followlng (sllly) example
#!/bln/sh
lf $1 1
Lhen
echo 1he flrsL cholce ls nlce
ellf $1 2
Lhen
echo 1he second cholce ls [usL as nlce
ellf $1 3
Lhen
echo 1he Lhlrd cholce ls excellenL
else
echo l see you were wlse enough noL Lo choose
echo ?ou wln
fl
WhaL Lhls example does ls compare Lhe flrsL parameLer (command llne argumenL ln Lhls case) wlLh Lhe
sLrlngs 1 2 and 3 uslng LesLs () LesL whlch compares Lwo sLrlngs for equallLy lf any of Lhem
maLch lL prlnLs ouL Lhe correspondlng message lf none of
Lhem maLch lL prlnLs ouL Lhe flnal case Ck Lhe example ls sllly and acLually flawed (Lhe user sLlll wlns
even lf Lhey Lype ln (4) or
someLhlng) buL lL lllusLraLes how Lhe lf sLaLemenL works
noLlce LhaL Lhere are spaces beLween (lf) and () () and Lhe LesL and Lhe LesL and () Lhese spaces musL
be presenL oLherwlse Lhe
shell wlll complaln 1here musL also be spaces beLween Lhe operaLor and operands of Lhe LesL oLherwlse
lL wlll noL work properly
noLlce how lL sLarLs wlLh (lf) and ends wlLh (fl) also noLlce how (Lhen) ls on a separaLe llne Lo Lhe LesL
above lL and LhaL (else) does
noL requlre a (Lhen) sLaLemenL ?ou musL consLrucL Lhls consLrucL exacLly llke Lhls for lL Lo work properly
lL ls also posslble Lo lnLegraLe loglcal Anu and C8 lnLo Lhe LesLlng by uslng Lwo LesLs separaLed by elLher
or || respecLlvely lor example we could replace Lhe Lhlrd LesL case ln Lhe example above wlLh
unlx Shell ScrlpLlng 1uLorlal
4ellf $1 3 || $1 4
Lhen echo 1he Lhlrd chol
1he scrlpL would prlnL ouL 1he Lhlrd cholce ls excellenL lf Lhe flrsL parameLer was elLher 3 C8 4 1o
lllusLraLe Lhe use of
ellf $1 3 || $2 4
Lhen echo 1he Lhlrd chol
1he scrlpL would prlnL ouL 1he Lhlrd cholce ls excellenL lf and only lf Lhe flrsL parameLer was 3 Anu
Lhe second parameLer was
4
and || are boLh lazlly evaluaLlng whlch means LhaL ln Lhe case of lf Lhe flrsL LesL falls lL
wonL boLher evaluaLlng Lhe
second because Lhe llsL wlll only be Lrue lf Lhey 8C1P pass and slnce one has already falled Lhere ls no
polnL wasLlng Llme evaluaLlng Lhe second ln Lhe case of || lf Lhe flrsL LesL passes lL wonL boLher
evaluaLlng Lhe second LesL because we only need CnL of Lhe
LesLs Lo pass for Lhe whole llsL Lo pass See Lhe LesL manual page for Lhe llsL of LesLs posslble (oLher Lhan
Lhe sLrlng equallLy LesL menLloned here)
3232 uoWhlle
1he uoWhlle Lakes Lhe followlng generlc form
whlle llsL
do llsL
done
ln Lhe words of Lhe SP manual 1he Lwo llsLs are execuLed repeaLedly whlle Lhe exlL sLaLus of Lhe flrsL llsL
ls zero Lhere ls a varlaLlon on Lhls LhaL uses unLll ln place of whlle whlch execuLes unLll Lhe exlL sLaLus of
Lhe flrsL llsL ls zero Pere ls an example use of
Lhe whlle sLaLemenL
#!/bln/sh
counL$1 # lnlLlallse counL Lo flrsL parameLer
whlle $counL gL 0 # whlle counL ls greaLer Lhan 10 do
do
echo $counL seconds Llll supper Llme!
counL$(expr $counL 1) # decremenL counL by 1
sleep 1 # sleep for a second uslng Lhe unlx sleep command
done
echo Supper Llme!! ?LAP!! # were flnlshed
lf called from Lhe commandllne wlLh an argumenL of 4 Lhls scrlpL wlll ouLpuL
4 seconds Llll supper Llme!
3 seconds Llll supper Llme!
2 seconds Llll supper Llme!
1 seconds Llll supper Llme!
Supper Llme!! ?LAP!!
?ou can see LhaL Lhls Llme we have used Lhe gL of Lhe LesL command lmpllclLly called vla and whlch
sLands for greaLer Lhan
ay careful aLLenLlon Lo Lhe formaLLlng and spaclng
3233 lor
1he synLax of Lhe for command ls
for varlable ln word
do llsL
done
1he SP manual sLaLes 1he words are expanded and Lhen Lhe llsL ls execuLed repeaLedly wlLh Lhe
varlable seL Lo each word ln
Lurn" A word ls essenLlally some oLher varlable LhaL conLalns a llsL of values of some sorL Lhe for
consLrucL asslgns each of Lhe
values ln Lhe word Lo varlable and Lhen varlable can be used wlLhln Lhe body of Lhe consLrucL upon
compleLlon of Lhe body varlable wlll be asslgned Lhe nexL value ln word unLll Lhere are no more values ln
word An example should make Lhls clearer
#!/bln/sh
frulLllsLApple ear 1omaLo each Crape
for frulL ln $frulLllsL
do
lf $frulL 1omaLo || $frulL each
Lhen
echo l llke $frulLes
else
unlx Shell ScrlpLlng 1uLorlal
3echo l llke $frulLs
fl
done
ln Lhls example frulLllsL ls word frulL ls varlable and Lhe body of Lhe sLaLemenL ouLpuLs how much Lhls
person loves varlous frulLs
buL lncludes an lfLhenelse sLaLemenL Lo deal wlLh Lhe correcL addlLlon of leLLers Lo descrlbe Lhe plural
verslon of Lhe frulL noLlce
LhaL Lhe varlable frulL was expressed llke $frulL because oLherwlse Lhe shell would have lnLerpreLed Lhe
precedlng leLLer(s) as belng parL of Lhe varlable and echoed noLhlng because we have noL deflned Lhe
varlables frulLs and frulLes When execuLed Lhls scrlpL
wlll ouLpuL
l llke Apples
l llke ears
l llke 1omaLoes
l llke eachs
l llke Crapes
WlLhln Lhe for consLrucL do and done may be replaced by and 1hls ls noL allowed for whlle
3234 Case
1he case consLrucL has Lhe followlng synLax
case word ln
paLLern) llsL

esac
An example of Lhls should make Lhlngs clearer
!#/bln/sh
case $1
ln
1) echo llrsL Cholce
2) echo Second Cholce
*) echo CLher Cholce
esac
1 2 and * are paLLerns word ls compared Lo each paLLern and lf a maLch ls found Lhe body of Lhe
correspondlng paLLern ls execuLed we have used * Lo represenL everyLhlng slnce Lhls ls checked lasL
we wlll sLlll caLch 1 and 2 because Lhey are checked
flrsL ln our example word ls $1 Lhe flrsL parameLer hence lf Lhe scrlpL ls ran wlLh Lhe argumenL 1 lL
wlll ouLpuL llrsL Cholce
2 Second Cholce and anyLhlng else CLher Cholce ln Lhls example we compared agalnsL numbers
(essenLlally sLlll a sLrlng
comparlson however) buL Lhe paLLern can be more complex see Lhe SP man page for more lnformaLlon
324 luncLlons
1he synLax of an SP funcLlon ls deflned as follows
name ( ) command
lL ls usually lald ouL llke Lhls
name()
commands

A funcLlon wlll reLurn wlLh a defaulL exlL sLaLus of zero one can reLurn dlfferenL exlL sLaLus by uslng Lhe
noLaLlon reLurn exlL sLaLus
varlables can be deflned locally wlLhln a funcLlon uslng local namevalue 1he example below shows Lhe
use of a user deflned lncremenL funcLlon
Lxample 1 lncremenL luncLlon Lxample
#!/bln/sh
lnc() # 1he lncremenL ls deflned flrsL so we can use lL
echo $(($1 + $2)) # We echo Lhe resulL of Lhe flrsL parameLer plus Lhe second parameLer

# We check Lo see LhaL all Lhe command llne argumenLs are presenL
lf $1 || $2 || $3
Lhen
unlx Shell ScrlpLlng 1uLorlal
6echo uSACL
echo counLer sLarLvalue lncremenLvalue endvalue
else
counL$1 # 8ename are varlables wlLh clearer names
value$2
end$3
whlle $counL lL $end # Loop whlle counL ls less Lhan end
do
echo $counL
counL$(lnc $counL $value) # Call lncremenL wlLh counL and value as parameLers
done # so LhaL counL ls lncremenLed by value
fl
lnc()
echo $(($1 + $2))

1he funcLlon ls deflned and opened wlLh lnc() Lhe llne echo $(($1 + $2)) uses Lhe noLaLlon for
arlLhmeLlc expresslon subsLlLuLlon whlch ls $((expresslon)) Lo enclose Lhe expresslon $1 + $2 whlch adds
Lhe flrsL and second parameLers passed Lo Lhe
funcLlon LogeLher Lhe echo blL aL Lhe sLarL echoes Lhem Lo sLandard ouLpuL we can caLch Lhls value by
asslgnlng Lhe funcLlon
call Lo a varlable as ls lllusLraLed by Lhe funcLlon call
counL$(lnc $counL $value)
We use command subsLlLuLlon whlch subsLlLuLes Lhe value of a command Lo subsLlLuLe Lhe value of Lhe
funcLlon call whereupon lL ls asslgned Lo Lhe counL varlable 1he command wlLhln Lhe command
subsLlLuLlon block ls lnc $counL $value Lhe lasL
Lwo values belng lLs parameLers Whlch are Lhen referenced from wlLhln Lhe funcLlon uslng $1 and $2
We could have used
Lhe oLher command subsLlLuLlon noLaLlon Lo call Lhe funcLlon lf we had wanLed
counL`lnc $counL $value`
We wlll show anoLher qulck example Lo lllusLraLe Lhe scope of varlables
Lxample 2 varlable Scope Lxample
#!/bln/sh
lnc()
local value4
echo value ls $value wlLhln Lhe funcLlonn
echo b$1 ls $1 wlLhln Lhe funcLlon

value3
echo value ls $value before Lhe funcLlon
echo $1 ls $1 before Lhe funcLlon
echo
echo e $(lnc $value)
echo
echo value ls $value afLer Lhe funcLlon
echo $1 ls $1 afLer Lhe funcLlon
lnc()
local value4
echo value ls $value wlLhln Lhe funcLlonn
echo b$1 ls $1 wlLhln Lhe funcLlon

We asslgn a local value Lo Lhe varlable value of 4 1he nexL Lhree llnes consLrucL Lhe Lhe ouLpuL we
would llke remember LhaL
Lhls ls belng echoed Lo some buffer and wlll be replace Lhe funcLlon call wlLh all Lhe sLuff LhaL was
passed Lo sLdouL wlLhln Lhe
funcLlon when Lhe funcLlon exlLs So Lhe calllng code wlll be replaced wlLh whaLever we dlrecL Lo
sLandard ouLpuL wlLhln Lhe
funcLlon 1he funcLlon ls called llke Lhls
echo e $(lnc $value)
unlx Shell ScrlpLlng 1uLorlal
7We have passed Lhe opLlon e Lo Lhe echo command whlch causes lL Lo process CsLyle backslash
escape characLers so we
can process any backslash escape characLers whlch Lhe sLrlng generaLed by Lhe funcLlon call conLalns
lf we [usL echo Lhe llnes we wanL Lo be reLurned by Lhe funcLlon lL wlll noL pass Lhe newllne characLer
onLo Lhe buffer even lf
we expllclLly lnclude lL wlLh an escape characLer reference so whaL we do ls acLually lnclude Lhe
sequence of characLers LhaL
wlll produce a new llne wlLhln Lhe sLrlng so LhaL when lL ls echoed by Lhe calllng code wlLh Lhe e Lhe
escape characLers wlll
be processed and Lhe newllnes wlll be placed where we wanL Lhem
echo value ls $value wlLhln Lhe funcLlonn
noLlce how Lhe newllne has been lnserLed wlLh n Lhe flrsL Lwo backslashes lndlcaLe LhaL we wanL Lo
echo a backslash because wlLhln double quoLes a backslash lndlcaLes Lo process Lhe nexL characLer
llLerally we have Lo do Lhls because we are
only beLween double quoLes and noL Lhe llLeralLexL slngle quoLes lf we had used slngle quoLes we
would had have Lo echo
Lhe blL wlLh Lhe newllne ln separaLely from Lhe blL LhaL conLalns $value oLherwlse $value would noL be
expanded
echo b$1 ls $1 wlLhln Lhe funcLlon
1hls ls our second llne and ls conLalned wlLhln double quoLes so LhaL Lhe varlable $1 wlll be expanded
b ls lncluded so LhaL
b wlll be placed ln Lhe echoed llne and our calllng code wlll process Lhls as a backspace characLer We
have Lo do Lhls because for some reason Lhe shell preflxes a space Lo Lhe second llne lf we do noL Lhe
backspace removes Lhls space
1he ouLpuL from Lhls scrlpL called wlLh 2 as Lhe flrsL argumenL ls
value ls 3 before Lhe funcLlon
$1 ls 2 before Lhe funcLlon
value ls 4 wlLhln Lhe funcLlon
$1 ls 3 wlLhln Lhe funcLlon
value ls 3 afLer Lhe funcLlon
$1 ls 2 afLer Lhe funcLlon
1lp
?ou can use ul8LC1C8?/commonsh Lo lmporL funcLlons from a scrlpL called commonsh ln
ul8LC1C8? a qulck
example ls shown below flrsL ls LesLsh
#!/bln/sh
/commonsh
lf $1 Lhen
echo uSACL
echo sh LesLsh Lype
exlL
fl
lf `valldLype $1` Lhen
echo valld Lype
else
echo lnvalld Lype
fl
Pere ls commonsh
#!/bln/sh
valldLype()
lf $1 1?LA ||
$1 1?L8 ||
$1 1?LC ||
$1 1?Lu ||
$1 1?LL
Lhen
exlL 0
else
exlL 1
fl

lf you need Lo learn more checkouL hLLp//www[aparkerbLlnLerneLcouk/sh/ for whaL looks llke an
excellenL 8ourne
Shell rogrammlng LuLorlal
unlx Shell ScrlpLlng 1uLorlal
8

You might also like