[Link]? Ashellscriptismerelyanexecutablefilecontainingmultipleshellcommands [Link]: ashelldefinitionstatement comments commands TheKornshellsupportsanumberoffeaturesandfunctionsthatmake learningshellscriptingtechniquespreferabletolearningastandard programminglanguagesuchasCorC++.Anaddedbonusofusingashell scriptisthatitisinterpreted,meaningthatitdoesnothavetobecompiled likemostprogramminglanguages. 5.1AnOverview [Link] familiarwithtwosimpleshellscriptsthatexecuteautomaticallyeachtimeyou [Link].
kshrcdefineacustomized "lookandfile"[Link] commandsforyousothatyoudon'thavetoenterthemmanuallyeachtime youlogon. [Link] simplethatprintsacopyofthecontentsofyourpresentworkingdirectory [Link]: #!/usr/bin/ksh #simpleprintsacopyofyourpwdtoafile print"Alistingof$PWD">[Link] lsl>>[Link] exit Typethecontentsofthescriptabove,namethefilesimple,andyouhavea [Link] astypingthescriptname,providedyoukeepinmindtherulesofexecution coveredinthenextsection. 5.2ShellScriptExecution Onceashellscriptiscreated,[Link],
beforeanyKornshellscriptbecanexecuted,itmustbeassignedtheproper [Link],in thiscasegivingexecutepermissiontothesimplefile: $chmod+xsimple Afterthat,youcanexecutethescriptbyspecifyingthefilenameasan argumenttothekshcommand: $kshsimple [Link],forthat methodtowork,thedirectorycontainingthescriptmustbedefinedinyour [Link],youmay havenoticedthatthePATH=$PATH:$HOMEdefinitionwasalreadyinplace. Thisenablesyoutorunscriptslocatedinyourhomedirectory($HOME) [Link],becauseofthatpredefined PATHvariable,thesimplescriptcanberunfromthecommandlinelikethis: $simple (Forthepurposesofthiscourse,we'llsimplifythingsbyrunningallscriptsby theirscriptnameonly,notasanargumenttothekshcommand.) Youcanalsoinvokethescriptfromyourcurrentshellbyopeninga backgroundsubprocessorsubshellwheretheactualcommandprocessing [Link]'tseeitrunning,butitwillfreeupyourexistingshellso [Link], processingintensivescriptsthatwouldotherwisetakeoveryourcurrentshell untiltheycomplete. Torunthescriptyoucreatedinthebackground,invokeitthisway: $simple& Whenthescriptcompletes,you'llseeoutputsimilartothisinthecurrentshell: [1]Done(127)simple ItisimportanttounderstandthatKornshellscriptsruninasomewhat [Link],variablesdefinedin theKornshellaren'tunderstoodoutsideofthedefiningorparentshell.
Theymustbeexplicitlyexportedfromtheparentshelltoworkinasubsequent [Link] variableknownoutsidetheparentshell,anysubshellwillautomatically inheritthevaluesyoudefinedfortheparentshell. Forexample,here'sascriptnamedlookmeupthatdoesnothingmorethan printalinetostandardoutputusingthemyaddress(definedas123Anystreet USA)variable: $catlookmeup print"Iliveat$myaddress" Ifyouopenanewshell(usingthekshcommand)fromtheparentshelland runthescript,youseethatmyaddressisundefined: $ksh $lookmeup Iliveat $ However,ifyouexportmyaddressfromtheparentshell: $exit $exportmyaddress andthenopenanewshellandrunthelookmeupscriptagain,thevariableis nowdefined: $ksh $lookmeup Iliveat123AnystreetUSA Toillustratefurtherhowtheparentshelltakesprocessingprecedence,let's changethevalueofmyaddressinthesubshell: $myaddress='Houston,Texas' $print$myaddress Houston,Texas Now,ifyouexitthenewshellandgobacktotheparentshellandtypethe samecommand: $exit
$print$myaddress 123AnystreetUSA youseethattheoriginalvalueintheparentshellwasnotaffectedbywhat youdidinthesubshell. Awaytoexportvariablesautomaticallyistousethesetoallexport [Link] subshell,butcanexportvariablescreatedintheparentshelltoallsubshells [Link],itcanautomaticallyexport variablescreatedinsubshellstonewsubshellscreatedafterrunningthe [Link]. NOTE: Ifascriptiswritingoutputtoafile,youcaninteractivelymonitortheprogress oftheoutputfile(andtherebythescript)byusingthetailfoutputfile [Link],useCtrlC. 5.3ShellScriptsAnIllustratedView Attheriskofsoundingredundant,let'srecap:shellscriptsaresimplyfiles [Link]'sgoabit furtherandlookatashellscript,linebyline. AnyKornshellscriptshouldcontainthislineattheverybeginning: #!/usr/bin/ksh Asyouprobablyalreadyknow,the#signmarksanythingthatfollowsiton thelineasacommentanythingcomingafteritwon'tbeinterpretedor [Link],whenthe#characterisfollowedbya! (commonlycalled"bang"),[Link] theKornshellwillbe(orshouldbe)[Link] specified,thesystemwillattempttoexecutethescriptusingwhateverits defaultshelltypeis,[Link] somecommandsthatothershellsdonot,thiscansometimescauseaproblem. Tobevalid,thislinemustbeontheveryfirstlineofthescript. [Link],a systemadministratormightusethefollowingscript,nameddiskusehere,to keeptrackofdiskspaceusage: #!/usr/bin/ksh #diskuse
#Showsdiskusageinblocksfor/home cd/var/log [Link].0 cd/home dusk*>/var/log/[Link] cat/var/log/[Link] Shownagainbutthistimewithannotationthescript'sprocessingstepsare clear: #!/usr/bin/ksh #SCRIPTNAME:diskuse #SCRIPTPURPOSE:Showsdiskusageinblocksfor/home #[Link] cd/var/log #[Link] [Link].0 #changetothetargetdirectory cd/home #runthedusk*commandonallfiles #in/homeandredirecttheoutput #to/var/log/[Link] dusk*>/var/log/[Link] #displaytheoutputofthedusk* #commandtostandardoutput cat/var/log/[Link] It'snotagoodideatohardcodepathnamesintoyourscriptslikewedidinthe [Link]/var/logasthetargetdirectoryseveraltimes, butwhatifthelocationofthefileschanged?Inashortscriptlikethisone,the [Link],somescriptscanbehundredsoflineslong, [Link] createavariabletotaketheplaceofthefullpathname,suchas: LOGDIR=/var/log Thefourthlineofthescriptwouldchangefrom:
[Link].0 to: cp${LOGDIR}/[Link]${LOGDIR}/[Link].0 Then,[Link],youwouldonlyhaveto [Link] aredefiningthepathnamewiththeLOGDIRvariable,thecd/var/loglinein [Link],thedusk*>/var/log/[Link] /var/log/[Link]${LOGDIR}for/var/log.