methodsbash

bash  时间:2021-01-31  阅读:()
BashGuideforBeginnersMachteltGarrelsGarrelsBVBAVersion1.
11Lastupdated20081227EditionTableofContentsIntroduction.
11.
Whythisguide12.
Whoshouldreadthisbook13.
Newversions,translationsandavailability.
24.
RevisionHistory.
25.
Contributions.
36.
Feedback.
37.
Copyrightinformation.
38.
Whatdoyouneed49.
Conventionsusedinthisdocument.
410.
Organizationofthisdocument.
5Chapter1.
BashandBashscripts61.
1.
Commonshellprograms.
61.
1.
1.
Generalshellfunctions.
61.
1.
2.
Shelltypes.
61.
2.
AdvantagesoftheBourneAgainSHell.
71.
2.
1.
BashistheGNUshell.
71.
2.
2.
Featuresonlyfoundinbash71.
3.
Executingcommands.
121.
3.
1.
General.
121.
3.
2.
Shellbuilt-incommands121.
3.
3.
Executingprogramsfromascript.
131.
4.
Buildingblocks.
131.
4.
1.
Shellbuildingblocks.
131.
5.
Developinggoodscripts.
151.
5.
1.
Propertiesofgoodscripts.
161.
5.
2.
Structure.
161.
5.
3.
Terminology.
161.
5.
4.
Awordonorderandlogic161.
5.
5.
AnexampleBashscript:mysystem.
sh.
171.
5.
6.
Exampleinitscript181.
6.
Summary.
191.
7.
Exercises.
19Chapter2.
Writinganddebuggingscripts.
212.
1.
Creatingandrunningascript.
212.
1.
1.
Writingandnaming.
212.
1.
2.
script1.
sh222.
1.
3.
Executingthescript.
232.
2.
Scriptbasics.
242.
2.
1.
Whichshellwillrunthescript242.
2.
2.
Addingcomments242.
3.
DebuggingBashscripts.
252.
3.
1.
Debuggingontheentirescript.
252.
3.
2.
Debuggingonpart(s)ofthescript262.
4.
Summary.
282.
5.
Exercises.
28BashGuideforBeginnersiTableofContentsChapter3.
TheBashenvironment.
293.
1.
Shellinitializationfiles.
293.
1.
1.
System-wideconfigurationfiles293.
1.
2.
Individualuserconfigurationfiles.
313.
1.
3.
Changingshellconfigurationfiles.
333.
2.
Variables.
343.
2.
1.
Typesofvariables.
343.
2.
2.
Creatingvariables.
373.
2.
3.
Exportingvariables383.
2.
4.
Reservedvariables393.
2.
5.
Specialparameters413.
2.
6.
Scriptrecyclingwithvariables.
433.
3.
Quotingcharacters.
443.
3.
1.
Why453.
3.
2.
Escapecharacters.
453.
3.
3.
Singlequotes.
453.
3.
4.
Doublequotes.
453.
3.
5.
ANSI-Cquoting.
463.
3.
6.
Locales463.
4.
Shellexpansion.
463.
4.
1.
General.
463.
4.
2.
Braceexpansion.
463.
4.
3.
Tildeexpansion.
473.
4.
4.
Shellparameterandvariableexpansion.
473.
4.
5.
Commandsubstitution483.
4.
6.
Arithmeticexpansion.
493.
4.
7.
Processsubstitution.
503.
4.
8.
Wordsplitting.
503.
4.
9.
Filenameexpansion.
513.
5.
Aliases.
513.
5.
1.
Whatarealiases513.
5.
2.
Creatingandremovingaliases.
523.
6.
MoreBashoptions.
533.
6.
1.
Displayingoptions533.
6.
2.
Changingoptions543.
7.
Summary.
553.
8.
Exercises.
55Chapter4.
Regularexpressions.
564.
1.
Regularexpressions.
564.
1.
1.
Whatareregularexpressions564.
1.
2.
Regularexpressionmetacharacters.
564.
1.
3.
Basicversusextendedregularexpressions.
574.
2.
Examplesusinggrep.
574.
2.
1.
Whatisgrep574.
2.
2.
Grepandregularexpressions.
584.
3.
PatternmatchingusingBashfeatures.
604.
3.
1.
Characterranges.
60BashGuideforBeginnersiiTableofContentsChapter4.
Regularexpressions4.
3.
2.
Characterclasses.
604.
4.
Summary.
614.
5.
Exercises.
61Chapter5.
TheGNUsedstreameditor.
625.
1.
Introduction.
625.
1.
1.
Whatissed625.
1.
2.
sedcommands.
625.
2.
Interactiveediting.
635.
2.
1.
Printinglinescontainingapattern.
635.
2.
2.
Deletinglinesofinputcontainingapattern.
645.
2.
3.
Rangesoflines.
645.
2.
4.
Findandreplacewithsed.
655.
3.
Non-interactiveediting.
665.
3.
1.
Readingsedcommandsfromafile.
665.
3.
2.
Writingoutputfiles.
665.
4.
Summary.
675.
5.
Exercises.
68Chapter6.
TheGNUawkprogramminglanguage696.
1.
Gettingstartedwithgawk.
696.
1.
1.
Whatisgawk696.
1.
2.
Gawkcommands.
696.
2.
Theprintprogram.
706.
2.
1.
Printingselectedfields.
706.
2.
2.
Formattingfields.
716.
2.
3.
Theprintcommandandregularexpressions726.
2.
4.
Specialpatterns726.
2.
5.
Gawkscripts.
736.
3.
Gawkvariables736.
3.
1.
Theinputfieldseparator736.
3.
2.
Theoutputseparators.
746.
3.
3.
Thenumberofrecords.
756.
3.
4.
Userdefinedvariables.
766.
3.
5.
Moreexamples.
766.
3.
6.
Theprintfprogram.
776.
4.
Summary.
776.
5.
Exercises.
77Chapter7.
Conditionalstatements.
797.
1.
Introductiontoif.
797.
1.
1.
General.
797.
1.
2.
Simpleapplicationsofif827.
2.
Moreadvancedifusage.
847.
2.
1.
if/then/elseconstructs.
847.
2.
2.
if/then/elif/elseconstructs877.
2.
3.
Nestedifstatements88BashGuideforBeginnersiiiTableofContentsChapter7.
Conditionalstatements7.
2.
4.
Booleanoperations.
887.
2.
5.
Usingtheexitstatementandif.
897.
3.
Usingcasestatements.
907.
3.
1.
Simplifiedconditions.
907.
3.
2.
Initscriptexample.
927.
4.
Summary.
927.
5.
Exercises.
93Chapter8.
Writinginteractivescripts.
948.
1.
Displayingusermessages.
948.
1.
1.
Interactiveornot948.
1.
2.
Usingtheechobuilt-incommand.
948.
2.
Catchinguserinput.
978.
2.
1.
Usingthereadbuilt-incommand.
978.
2.
2.
Promptingforuserinput988.
2.
3.
Redirectionandfiledescriptors998.
2.
4.
Fileinputandoutput1018.
3.
Summary.
1068.
4.
Exercises.
106Chapter9.
Repetitivetasks.
1089.
1.
Theforloop1089.
1.
1.
Howdoesitwork1089.
1.
2.
Examples.
1089.
2.
Thewhileloop.
1099.
2.
1.
Whatisit1099.
2.
2.
Examples.
1109.
3.
Theuntilloop.
1129.
3.
1.
Whatisit1129.
3.
2.
Example.
1129.
4.
I/Oredirectionandloops.
1139.
4.
1.
Inputredirection.
1139.
4.
2.
Outputredirection1139.
5.
Breakandcontinue.
1149.
5.
1.
Thebreakbuilt-in.
1149.
5.
2.
Thecontinuebuilt-in.
1159.
5.
3.
Examples.
1169.
6.
Makingmenuswiththeselectbuilt-in.
1179.
6.
1.
General.
1179.
6.
2.
Submenus.
1189.
7.
Theshiftbuilt-in.
1189.
7.
1.
Whatdoesitdo1189.
7.
2.
Examples.
1199.
8.
Summary.
1209.
9.
Exercises.
120BashGuideforBeginnersivTableofContentsChapter10.
Moreonvariables.
12110.
1.
Typesofvariables.
12110.
1.
1.
Generalassignmentofvalues.
12110.
1.
2.
Usingthedeclarebuilt-in.
12110.
1.
3.
Constants.
12210.
2.
Arrayvariables.
12310.
2.
1.
Creatingarrays.
12310.
2.
2.
Dereferencingthevariablesinanarray12310.
2.
3.
Deletingarrayvariables12410.
2.
4.
Examplesofarrays.
12410.
3.
Operationsonvariables12610.
3.
1.
Arithmeticonvariables.
12610.
3.
2.
Lengthofavariable12610.
3.
3.
Transformationsofvariables.
12710.
4.
Summary.
12910.
5.
Exercises.
129Chapter11.
Functions.
13111.
1.
Introduction.
13111.
1.
1.
Whatarefunctions13111.
1.
2.
Functionsyntax.
13111.
1.
3.
Positionalparametersinfunctions.
13211.
1.
4.
Displayingfunctions13311.
2.
Examplesoffunctionsinscripts.
13311.
2.
1.
Recycling.
13311.
2.
2.
Settingthepath.
13411.
2.
3.
Remotebackups.
13411.
3.
Summary.
13611.
4.
Exercises.
136Chapter12.
Catchingsignals.
13712.
1.
Signals.
13712.
1.
1.
Introduction.
13712.
1.
2.
Usageofsignalswithkill.
13812.
2.
Traps.
13912.
2.
1.
General.
13912.
2.
2.
HowBashinterpretstraps.
13912.
2.
3.
Moreexamples.
14012.
3.
Summary.
14012.
4.
Exercises.
140AppendixA.
ShellFeatures142A.
1.
Commonfeatures.
142A.
2.
Differingfeatures.
143Glossary.
146A146B.
146BashGuideforBeginnersvTableofContentsGlossaryC.
146D147E.
148F.
148G148H149I.
149J.
149K150L.
150M.
150N151P.
152Q152R.
152S.
153T.
153U154V154W.
155X155Z.
156Index.
157A157B.
157C.
158D158E.
158F.
159G160H160I.
160J.
161K161L.
161M.
161N161O162P.
162Q162R.
162S.
163T.
164U164V164W.
165BashGuideforBeginnersviTableofContentsIndexX165Y165Z.
165BashGuideforBeginnersviiIntroduction1.
WhythisguideTheprimaryreasonforwritingthisdocumentisthatalotofreadersfeeltheexistingHOWTOtobetooshortandincomplete,whiletheBashScriptingguideistoomuchofareferencework.
Thereisnothinginbetweenthesetwoextremes.
Ialsowrotethisguideonthegeneralprincipalthatnotenoughfreebasiccoursesareavailable,thoughtheyshouldbe.
Thisisapracticalguidewhich,whilenotalwaysbeingtooserious,triestogivereal-lifeinsteadoftheoreticalexamples.
IpartlywroteitbecauseIdon'tgetexcitedwithstrippeddownandover-simplifiedexampleswrittenbypeoplewhoknowwhattheyaretalkingabout,showingsomereallycoolBashfeaturesomuchoutofitscontextthatyoucannoteveruseitinpracticalcircumstances.
Youcanreadthatsortofstuffafterfinishingthisbook,whichcontainsexercisesandexamplesthatwillhelpyousurviveintherealworld.
FrommyexperienceasUNIX/Linuxuser,systemadministratorandtrainer,Iknowthatpeoplecanhaveyearsofdailyinteractionwiththeirsystems,withouthavingtheslightestknowledgeoftaskautomation.
ThustheyoftenthinkthatUNIXisnotuserfriendly,andevenworse,theygettheimpressionthatitisslowandold-fashioned.
Thisproblemisanotheronethatcanberemediedbythisguide.
2.
WhoshouldreadthisbookEverybodyworkingonaUNIXorUNIX-likesystemwhowantstomakelifeeasieronthemselves,powerusersandsysadminsalike,canbenefitfromreadingthisbook.
Readerswhoalreadyhaveagraspofworkingthesystemusingthecommandlinewilllearntheinsandoutsofshellscriptingthateaseexecutionofdailytasks.
Systemadministrationreliesagreatdealonshellscripting;commontasksareoftenautomatedusingsimplescripts.
Thisdocumentisfullofexamplesthatwillencourageyoutowriteyourownandthatwillinspireyoutoimproveonexistingscripts.
Prerequisites/notinthiscourse:YoushouldbeanexperiencedUNIXorLinuxuser,familiarwithbasiccommands,manpagesanddocumentationBeingabletouseatexteditorUnderstandsystembootandshutdownprocesses,initandinitscriptsCreateusersandgroups,setpasswordsPermissions,specialmodesUnderstandnamingconventionsfordevices,partitioning,mounting/unmountingfilesystemsAdding/removingsoftwareonyoursystemSeeIntroductiontoLinux(oryourlocalTLDPmirror)ifyouhaven'tmasteredoneormoreofthesetopics.
Additionalinformationcanbefoundinyoursystemdocumentation(manandinfopages),orattheLinuxDocumentationProject.
Introduction13.
Newversions,translationsandavailabilityThemostrecenteditioncanbefoundathttp://tille.
garrels.
be/training/bash/.
Youshouldfindthesameversionathttp://tldp.
org/LDP/Bash-Beginners-Guide/html/index.
html.
ThisguideisavailableinprintfromFultus.
com.
Figure1.
BashGuideforBeginnersfrontcoverThisguidehasbeentranslated:Chinesetranslationathttp://xiaowang.
net/bgb-cn/,byWangWei.
Ukrainiantranslationathttp://docs.
linux.
org.
ua/index.
php/LDP:Bash_beginners_guide,byYaroslavFedevychandhisteam.
Afrenchtranslationisinthemakingandwillbelinkedtoassoonasitisfinished.
4.
RevisionHistoryRevisionHistoryRevision1.
112008-12-27Revisedby:MGProcessedinputfromreaders.
Revision1.
102008-06-06Revisedby:MGaddresschangeRevision1.
92006-10-10Revisedby:MGIncorporatedreaderremarks,addedindexusingDocBooktags.
Revision1.
82006-03-15Revisedby:MGclarifiedexampleinChap4,correctedheredocinchap9,generalchecksandcorrectionoftypos,addedlinktoChineseandUkrainiantranslation,noteandstufftoknowaboutawkinchap6.
Revision1.
72005-09-05Revisedby:MGCorrectedtyposinchapter3,6and7,incorporateduserremarks,addedanoteinchap7.
BashGuideforBeginnersIntroduction2Revision1.
62005-03-01Revisedby:MGMinordebugging,addedmorekeywords,infoaboutnewBash3.
0,tookoutblankimage.
Revision1.
02004-04-27Revisedby:TMInitialreleaseforLDP;moreexercises,moremarkup,lesserrorsandabuse;addedglossary.
Revision1.
0-beta2003-04-20Revisedby:MGPre-release5.
ContributionsThankstoallthefriendswhohelped(ortriedto)andtomyhusband;yourencouragingwordsmadethisworkpossible.
Thankstoallthepeoplewhosubmittedbugreports,examplesandremarks-amongmany,manyothers:HansBol,oneofthegroupiesMikeSim,remarksonstyleDanRichter,forarrayexamplesGergFerguson,forideasonthetitleMendelLeoCooper,formakingroom#linux.
be,forkeepingmyfeetonthegroundFrankWang,forhisdetailedremarksonallthethingsIdidwrong;-)SpecialthankstoTabathaMarshall,whovolunteeredtodoacompletereviewandspellandgrammarcheck.
Wemakeagreatteam:sheworkswhenIsleep.
Andviceversa;-)6.
FeedbackMissinginformation,missinglinks,missingcharacters,remarksMailittothemaintainerofthisdocument.
7.
Copyrightinformation*Copyright(c)2002-2007,MachteltGarrels*Allrightsreserved.
*Redistributionanduseinsourceandbinaryforms,withorwithout*modification,arepermittedprovidedthatthefollowingconditionsaremet:***Redistributionsofsourcecodemustretaintheabovecopyright*notice,thislistofconditionsandthefollowingdisclaimer.
**Redistributionsinbinaryformmustreproducetheabovecopyright*notice,thislistofconditionsandthefollowingdisclaimerinthe*documentationand/orothermaterialsprovidedwiththedistribution.
**Neitherthenameoftheauthor,MachteltGarrels,northe*namesofitscontributorsmaybeusedtoendorseorpromoteproducts*derivedfromthissoftwarewithoutspecificpriorwrittenpermission.
**THISSOFTWAREISPROVIDEDBYTHEAUTHORANDCONTRIBUTORS"ASIS"ANDANY*EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDBashGuideforBeginnersIntroduction3*WARRANTIESOFMERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEARE*DISCLAIMED.
INNOEVENTSHALLTHEAUTHORANDCONTRIBUTORSBELIABLEFORANY*DIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES*(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;*LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDAND*ONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT*(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS*SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.
Theauthorandpublisherhavemadeeveryeffortinthepreparationofthisbooktoensuretheaccuracyoftheinformation.
However,theinformationcontainedinthisbookisofferedwithoutwarranty,eitherexpressorimplied.
Neithertheauthornorthepublishernoranydealerordistributorwillbeheldliableforanydamagescausedorallegedtobecausedeitherdirectlyorindirectlybythisbook.
Thelogos,trademarksandsymbolsusedinthisbookarethepropertiesoftheirrespectiveowners.
8.
Whatdoyouneedbash,availablefromhttp://www.
gnu.
org/directory/GNU/.
TheBashshellisavailableonnearlyeveryLinuxsystem,andcanthesedaysbefoundonawidevarietyofUNIXsystems.
Compileseasilyifyouneedtomakeyourown,testedonawidevarietyofUNIX,Linux,MSWindowsandothersystems.
9.
ConventionsusedinthisdocumentThefollowingtypographicandusageconventionsoccurinthistext:Table1.
TypographicandusageconventionsTexttypeMeaning"Quotedtext"Quotesfrompeople,quotedcomputeroutput.
terminalviewLiteralcomputerinputandoutputcapturedfromtheterminal,usuallyrenderedwithalightgreybackground.
commandNameofacommandthatcanbeenteredonthecommandline.
VARIABLENameofavariableorpointertocontentofavariable,asin$VARNAME.
optionOptiontoacommand,asin"the-aoptiontothelscommand".
argumentArgumenttoacommand,asin"readmanls".
commandoptionsargumentsCommandsynopsisorgeneralusage,onaseparatedline.
filenameNameofafileordirectory,forexample"Changetothe/usr/bindirectory.
"KeyKeystohitonthekeyboard,suchas"typeQtoquit".
ButtonGraphicalbuttontoclick,liketheOKbutton.
Menu->ChoiceChoicetoselectfromagraphicalmenu,forinstance:"SelectHelp->AboutMozillainyourbrowser.
"TerminologyImportanttermorconcept:"TheLinuxkernelistheheartofthesystem.
"\Thebackslashinaterminalvieworcommandsynopsisindicatesanunfinishedline.
Inotherwords,ifyouseealongcommandthatiscutintomultiplelines,\meansBashGuideforBeginnersIntroduction4"Don'tpressEnteryet!
"SeeChapter1linktorelatedsubjectwithinthisguide.
TheauthorClickablelinktoanexternalwebresource.
10.
OrganizationofthisdocumentThisguidediscussesconceptsusefulinthedailylifeoftheseriousBashuser.
Whileabasicknowledgeoftheusageoftheshellisrequired,westartwithadiscussionofthebasicshellcomponentsandpracticesinthefirstthreechapters.
Chaptersfourtosixarediscussionsofbasictoolsthatarecommonlyusedinshellscripts.
Chapterseighttotwelvediscussthemostcommonconstructsinshellscripts.
Allchapterscomewithexercisesthatwilltestyourpreparednessforthenextchapter.
Chapter1:Bashbasics:whyBashissogood,buildingblocks,firstguidelinesondevelopinggoodscripts.
Chapter2:Scriptbasics:writinganddebugging.
Chapter3:TheBashEnvironment:initializationfiles,variables,quotingcharacters,shellexpansionorder,aliases,options.
Chapter4:Regularexpressions:anintroduction.
Chapter5:Sed:anintroductiontothesedlineeditor.
Chapter6:Awk:introductiontotheawkprogramminglanguage.
Chapter7:Conditionalstatements:constructsusedinBashtotestconditions.
Chapter8:Interactivescripts:makingscriptsuser-friendly,catchinguserinput.
Chapter9:Executingcommandsrepetitively:constructsusedinBashtoautomatecommandexecution.
Chapter10:Advancedvariables:specifyingvariabletypes,introductiontoarraysofvariables,operationsonvariables.
Chapter11:Functions:anintroduction.
Chapter12:Catchingsignals:introductiontoprocesssignalling,trappinguser-sentsignals.
BashGuideforBeginnersIntroduction5Chapter1.
BashandBashscriptsInthisintroductionmoduleweDescribesomecommonshellsPointoutGNUBashadvantagesandfeaturesDescribetheshell'sbuildingblocksDiscussBashinitializationfilesSeehowtheshellexecutescommandsLookintosomesimplescriptexamples1.
1.
Commonshellprograms1.
1.
1.
GeneralshellfunctionsTheUNIXshellprograminterpretsusercommands,whichareeitherdirectlyenteredbytheuser,orwhichcanbereadfromafilecalledtheshellscriptorshellprogram.
Shellscriptsareinterpreted,notcompiled.
Theshellreadscommandsfromthescriptlineperlineandsearchesforthosecommandsonthesystem(seeSection1.
2),whileacompilerconvertsaprogramintomachinereadableform,anexecutablefile-whichmaythenbeusedinashellscript.
Apartfrompassingcommandstothekernel,themaintaskofashellisprovidingauserenvironment,whichcanbeconfiguredindividuallyusingshellresourceconfigurationfiles.
1.
1.
2.
ShelltypesJustlikepeopleknowdifferentlanguagesanddialects,yourUNIXsystemwillusuallyofferavarietyofshelltypes:shorBourneShell:theoriginalshellstillusedonUNIXsystemsandinUNIX-relatedenvironments.
Thisisthebasicshell,asmallprogramwithfewfeatures.
Whilethisisnotthestandardshell,itisstillavailableoneveryLinuxsystemforcompatibilitywithUNIXprograms.
bashorBourneAgainshell:thestandardGNUshell,intuitiveandflexible.
Probablymostadvisableforbeginninguserswhilebeingatthesametimeapowerfultoolfortheadvancedandprofessionaluser.
OnLinux,bashisthestandardshellforcommonusers.
Thisshellisaso-calledsupersetoftheBourneshell,asetofadd-onsandplug-ins.
ThismeansthattheBourneAgainshelliscompatiblewiththeBourneshell:commandsthatworkinsh,alsoworkinbash.
However,thereverseisnotalwaysthecase.
Allexamplesandexercisesinthisbookusebash.
cshorCshell:thesyntaxofthisshellresemblesthatoftheCprogramminglanguage.
Sometimesaskedforbyprogrammers.
tcshorTENEXCshell:asupersetofthecommonCshell,enhancinguser-friendlinessandspeed.
ThatiswhysomealsocallittheTurboCshell.
kshortheKornshell:sometimesappreciatedbypeoplewithaUNIXbackground.
AsupersetoftheBourneshell;withstandardconfigurationanightmareforbeginningusers.
Thefile/etc/shellsgivesanoverviewofknownshellsonaLinuxsystem:Chapter1.
BashandBashscripts6mia:~>cat/etc/shells/bin/bash/bin/sh/bin/tcsh/bin/cshYourdefaultshellissetinthe/etc/passwdfile,likethislineforusermia:mia:L2NOfqdlPrHwE:504:504:MiaMaya:/home/mia:/bin/bashToswitchfromoneshelltoanother,justenterthenameofthenewshellintheactiveterminal.
ThesystemfindsthedirectorywherethenameoccursusingthePATHsettings,andsinceashellisanexecutablefile(program),thecurrentshellactivatesitanditgetsexecuted.
Anewpromptisusuallyshown,becauseeachshellhasitstypicalappearance:mia:~>tcsh[mia@post21~]$1.
2.
AdvantagesoftheBourneAgainSHell1.
2.
1.
BashistheGNUshellTheGNUproject(GNU'sNotUNIX)providestoolsforUNIX-likesystemadministrationwhicharefreesoftwareandcomplytoUNIXstandards.
Bashisansh-compatibleshellthatincorporatesusefulfeaturesfromtheKornshell(ksh)andCshell(csh).
ItisintendedtoconformtotheIEEEPOSIXP1003.
2/ISO9945.
2ShellandToolsstandard.
Itoffersfunctionalimprovementsovershforbothprogrammingandinteractiveuse;theseincludecommandlineediting,unlimitedsizecommandhistory,jobcontrol,shellfunctionsandaliases,indexedarraysofunlimitedsize,andintegerarithmeticinanybasefromtwotosixty-four.
Bashcanrunmostshscriptswithoutmodification.
LiketheotherGNUprojects,thebashinitiativewasstartedtopreserve,protectandpromotethefreedomtouse,study,copy,modifyandredistributesoftware.
Itisgenerallyknownthatsuchconditionsstimulatecreativity.
Thiswasalsothecasewiththebashprogram,whichhasalotofextrafeaturesthatothershellscan'toffer.
1.
2.
2.
Featuresonlyfoundinbash1.
2.
2.
1.
InvocationInadditiontothesingle-charactershellcommandlineoptionswhichcangenerallybeconfiguredusingthesetshellbuilt-incommand,thereareseveralmulti-characteroptionsthatyoucanuse.
Wewillcomeacrossacoupleofthemorepopularoptionsinthisandthefollowingchapters;thecompletelistcanbefoundintheBashinfopages,Bashfeatures->InvokingBash.
1.
2.
2.
2.
BashstartupfilesStartupfilesarescriptsthatarereadandexecutedbyBashwhenitstarts.
Thefollowingsubsectionsdescribedifferentwaystostarttheshell,andthestartupfilesthatarereadconsequently.
BashGuideforBeginnersChapter1.
BashandBashscripts71.
2.
2.
2.
1.
Invokedasaninteractiveloginshell,orwith`--login'Interactivemeansyoucanentercommands.
Theshellisnotrunningbecauseascripthasbeenactivated.
Aloginshellmeansthatyougottheshellafterauthenticatingtothesystem,usuallybygivingyourusernameandpassword.
Filesread:/etc/profile~/.
bash_profile,~/.
bash_loginor~/.
profile:firstexistingreadablefileisread~/.
bash_logoutuponlogout.
Errormessagesareprintedifconfigurationfilesexistbutarenotreadable.
Ifafiledoesnotexist,bashsearchesforthenext.
1.
2.
2.
2.
2.
Invokedasaninteractivenon-loginshellAnon-loginshellmeansthatyoudidnothavetoauthenticatetothesystem.
Forinstance,whenyouopenaterminalusinganicon,oramenuitem,thatisanon-loginshell.
Filesread:~/.
bashrcThisfileisusuallyreferredtoin~/.
bash_profile:if[-f~/.
bashrc];then.
~/.
bashrc;fiSeeChapter7formoreinformationontheifconstruct.
1.
2.
2.
2.
3.
Invokednon-interactivelyAllscriptsusenon-interactiveshells.
Theyareprogrammedtodocertaintasksandcannotbeinstructedtodootherjobsthanthoseforwhichtheyareprogrammed.
Filesread:definedbyBASH_ENVPATHisnotusedtosearchforthisfile,soifyouwanttouseit,bestrefertoitbygivingthefullpathandfilename.
1.
2.
2.
2.
4.
InvokedwiththeshcommandBashtriestobehaveasthehistoricalBourneshprogramwhileconformingtothePOSIXstandardaswell.
Filesread:/etc/profile~/.
profileBashGuideforBeginnersChapter1.
BashandBashscripts8Wheninvokedinteractively,theENVvariablecanpointtoextrastartupinformation.
1.
2.
2.
2.
5.
POSIXmodeThisoptionisenabledeitherusingthesetbuilt-in:set-oposixorbycallingthebashprogramwiththe--posixoption.
BashwillthentrytobehaveascompliantaspossibletothePOSIXstandardforshells.
SettingthePOSIXLY_CORRECTvariabledoesthesame.
Filesread:definedbyENVvariable.
1.
2.
2.
2.
6.
InvokedremotelyFilesreadwheninvokedbyrshd:~/.
bashrcAvoiduseofr-toolsBeawareofthedangerswhenusingtoolssuchasrlogin,telnet,rshandrcp.
Theyareintrinsicallyinsecurebecauseconfidentialdataissentoverthenetworkunencrypted.
Ifyouneedtoolsforremoteexecution,filetransferandsoon,useanimplementationofSecureSHell,generallyknownasSSH,freelyavailablefromhttp://www.
openssh.
org.
Differentclientprogramsareavailablefornon-UNIXsystemsaswell,seeyourlocalsoftwaremirror.
1.
2.
2.
2.
7.
InvokedwhenUIDisnotequaltoEUIDNostartupfilesarereadinthiscase.
1.
2.
2.
3.
Interactiveshells1.
2.
2.
3.
1.
WhatisaninteractiveshellAninteractiveshellgenerallyreadsfrom,andwritesto,auser'sterminal:inputandoutputareconnectedtoaterminal.
Bashinteractivebehaviorisstartedwhenthebashcommandiscalleduponwithoutnon-optionarguments,exceptwhentheoptionisastringtoreadfromorwhentheshellisinvokedtoreadfromstandardinput,whichallowsforpositionalparameterstobeset(seeChapter3).
1.
2.
2.
3.
2.
IsthisshellinteractiveTestbylookingatthecontentofthespecialparameter-,itcontainsan'i'whentheshellisinteractive:eddy:~>echo$-himBHInnon-interactiveshells,theprompt,PS1,isunset.
BashGuideforBeginnersChapter1.
BashandBashscripts91.
2.
2.
3.
3.
InteractiveshellbehaviorDifferencesininteractivemode:Bashreadsstartupfiles.
Jobcontrolenabledbydefault.
Promptsareset,PS2isenabledformulti-linecommands,itisusuallysetto">".
Thisisalsothepromptyougetwhentheshellthinksyouenteredanunfinishedcommand,forinstancewhenyouforgetquotes,commandstructuresthatcannotbeleftout,etc.
Commandsarebydefaultreadfromthecommandlineusingreadline.
BashinterpretstheshelloptionignoreeofinsteadofexitingimmediatelyuponreceivingEOF(EndOfFile).
Commandhistoryandhistoryexpansionareenabledbydefault.
HistoryissavedinthefilepointedtobyHISTFILEwhentheshellexits.
Bydefault,HISTFILEpointsto~/.
bash_history.
Aliasexpansionisenabled.
Intheabsenceoftraps,theSIGTERMsignalisignored.
Intheabsenceoftraps,SIGINTiscaughtandhandled.
Thus,typingCtrl+C,forexample,willnotquityourinteractiveshell.
SendingSIGHUPsignalstoalljobsonexitisconfiguredwiththehuponexitoption.
Commandsareexecuteduponread.
Bashchecksformailperiodically.
Bashcanbeconfiguredtoexitwhenitencountersunreferencedvariables.
Ininteractivemodethisbehaviorisdisabled.
Whenshellbuilt-incommandsencounterredirectionerrors,thiswillnotcausetheshelltoexit.
Specialbuilt-insreturningerrorswhenusedinPOSIXmodedon'tcausetheshelltoexit.
Thebuilt-incommandsarelistedinSection1.
3.
2.
Failureofexecwillnotexittheshell.
Parsersyntaxerrorsdon'tcausetheshelltoexit.
Simplespellcheckfortheargumentstothecdbuilt-inisenabledbydefault.
AutomaticexitafterthelengthoftimespecifiedintheTMOUTvariablehaspassed,isenabled.
Moreinformation:Section3.
2Section3.
6SeeChapter12formoreaboutsignals.
Section3.
4discussesthevariousexpansionsperformeduponenteringacommand.
1.
2.
2.
4.
ConditionalsConditionalexpressionsareusedbythe[[compoundcommandandbythetestand[built-incommands.
Expressionsmaybeunaryorbinary.
Unaryexpressionsareoftenusedtoexaminethestatusofafile.
Youonlyneedoneobject,forinstanceafile,todotheoperationon.
Therearestringoperatorsandnumericcomparisonoperatorsaswell;thesearebinaryoperators,requiringtwoobjectstodotheoperationon.
IftheFILEargumenttooneoftheprimariesisintheform/dev/fd/N,thenfiledescriptorNischecked.
IftheFILEargumenttooneoftheprimariesisoneof/dev/stdin,/dev/stdoutor/dev/stderr,thenfiledescriptor0,1or2respectivelyischecked.
ConditionalsarediscussedindetailinChapter7.
BashGuideforBeginnersChapter1.
BashandBashscripts10MoreinformationaboutthefiledescriptorsinSection8.
2.
3.
1.
2.
2.
5.
ShellarithmeticTheshellallowsarithmeticexpressionstobeevaluated,asoneoftheshellexpansionsorbytheletbuilt-in.
Evaluationisdoneinfixed-widthintegerswithnocheckforoverflow,thoughdivisionby0istrappedandflaggedasanerror.
TheoperatorsandtheirprecedenceandassociativityarethesameasintheClanguage,seeChapter3.
1.
2.
2.
6.
AliasesAliasesallowastringtobesubstitutedforawordwhenitisusedasthefirstwordofasimplecommand.
Theshellmaintainsalistofaliasesthatmaybesetandunsetwiththealiasandunaliascommands.
Bashalwaysreadsatleastonecompletelineofinputbeforeexecutinganyofthecommandsonthatline.
Aliasesareexpandedwhenacommandisread,notwhenitisexecuted.
Therefore,analiasdefinitionappearingonthesamelineasanothercommanddoesnottakeeffectuntilthenextlineofinputisread.
Thecommandsfollowingthealiasdefinitiononthatlinearenotaffectedbythenewalias.
Aliasesareexpandedwhenafunctiondefinitionisread,notwhenthefunctionisexecuted,becauseafunctiondefinitionisitselfacompoundcommand.
Asaconsequence,aliasesdefinedinafunctionarenotavailableuntilafterthatfunctionisexecuted.
WewilldiscussaliasesindetailinSection3.
5.
1.
2.
2.
7.
ArraysBashprovidesone-dimensionalarrayvariables.
Anyvariablemaybeusedasanarray;thedeclarebuilt-inwillexplicitlydeclareanarray.
Thereisnomaximumlimitonthesizeofanarray,noranyrequirementthatmembersbeindexedorassignedcontiguously.
Arraysarezero-based.
SeeChapter10.
1.
2.
2.
8.
DirectorystackThedirectorystackisalistofrecently-visiteddirectories.
Thepushdbuilt-inaddsdirectoriestothestackasitchangesthecurrentdirectory,andthepopdbuilt-inremovesspecifieddirectoriesfromthestackandchangesthecurrentdirectorytothedirectoryremoved.
ContentcanbedisplayedissuingthedirscommandorbycheckingthecontentoftheDIRSTACKvariable.
MoreinformationabouttheworkingsofthismechanismcanbefoundintheBashinfopages.
1.
2.
2.
9.
ThepromptBashmakesplayingwiththepromptevenmorefun.
SeethesectionControllingthePromptintheBashinfopages.
BashGuideforBeginnersChapter1.
BashandBashscripts111.
2.
2.
10.
TherestrictedshellWheninvokedasrbashorwiththe--restrictedor-roption,thefollowinghappens:Thecdbuilt-inisdisabled.
SettingorunsettingSHELL,PATH,ENVorBASH_ENVisnotpossible.
Commandnamescannolongercontainslashes.
Filenamescontainingaslasharenotallowedwiththe.
(source)built-incommand.
Thehashbuilt-indoesnotacceptslasheswiththe-poption.
Importoffunctionsatstartupisdisabled.
SHELLOPTSisignoredatstartup.
Outputredirectionusingand>>isdisabled.
Theexecbuilt-inisdisabled.
The-fand-doptionsaredisabledfortheenablebuilt-in.
AdefaultPATHcannotbespecifiedwiththecommandbuilt-in.
Turningoffrestrictedmodeisnotpossible.
Whenacommandthatisfoundtobeashellscriptisexecuted,rbashturnsoffanyrestrictionsintheshellspawnedtoexecutethescript.
Moreinformation:Section3.
2Section3.
6InfoBash->BasicShellFeatures->RedirectionsSection8.
2.
3:advancedredirection1.
3.
Executingcommands1.
3.
1.
GeneralBashdeterminesthetypeofprogramthatistobeexecuted.
Normalprogramsaresystemcommandsthatexistincompiledformonyoursystem.
Whensuchaprogramisexecuted,anewprocessiscreatedbecauseBashmakesanexactcopyofitself.
Thischildprocesshasthesameenvironmentasitsparent,onlytheprocessIDnumberisdifferent.
Thisprocedureiscalledforking.
Aftertheforkingprocess,theaddressspaceofthechildprocessisoverwrittenwiththenewprocessdata.
Thisisdonethroughanexeccalltothesystem.
Thefork-and-execmechanismthusswitchesanoldcommandwithanew,whiletheenvironmentinwhichthenewprogramisexecutedremainsthesame,includingconfigurationofinputandoutputdevices,environmentvariablesandpriority.
ThismechanismisusedtocreateallUNIXprocesses,soitalsoappliestotheLinuxoperatingsystem.
Eventhefirstprocess,init,withprocessID1,isforkedduringthebootprocedureintheso-calledbootstrappingprocedure.
1.
3.
2.
Shellbuilt-incommandsBuilt-incommandsarecontainedwithintheshellitself.
Whenthenameofabuilt-incommandisusedasthefirstwordofasimplecommand,theshellexecutesthecommanddirectly,withoutcreatinganewprocess.
BashGuideforBeginnersChapter1.
BashandBashscripts12Built-incommandsarenecessarytoimplementfunctionalityimpossibleorinconvenienttoobtainwithseparateutilities.
Bashsupports3typesofbuilt-incommands:BourneShellbuilt-ins::,.
,break,cd,continue,eval,exec,exit,export,getopts,hash,pwd,readonly,return,set,shift,test,[,times,trap,umaskandunset.
Bashbuilt-incommands:alias,bind,builtin,command,declare,echo,enable,help,let,local,logout,printf,read,shopt,type,typeset,ulimitandunalias.
Specialbuilt-incommands:WhenBashisexecutinginPOSIXmode,thespecialbuilt-insdifferfromotherbuilt-incommandsinthreerespects:Specialbuilt-insarefoundbeforeshellfunctionsduringcommandlookup.
1.
Ifaspecialbuilt-inreturnsanerrorstatus,anon-interactiveshellexits.
2.
Assignmentstatementsprecedingthecommandstayineffectintheshellenvironmentafterthecommandcompletes.
3.
ThePOSIXspecialbuilt-insare:,.
,break,continue,eval,exec,exit,export,readonly,return,set,shift,trapandunset.
Mostofthesebuilt-inswillbediscussedinthenextchapters.
Forthosecommandsforwhichthisisnotthecase,werefertotheInfopages.
1.
3.
3.
ExecutingprogramsfromascriptWhentheprogrambeingexecutedisashellscript,bashwillcreateanewbashprocessusingafork.
Thissubshellreadsthelinesfromtheshellscriptonelineatatime.
Commandsoneachlineareread,interpretedandexecutedasiftheywouldhavecomedirectlyfromthekeyboard.
Whilethesubshellprocesseseachlineofthescript,theparentshellwaitsforitschildprocesstofinish.
Whentherearenomorelinesintheshellscripttoread,thesubshellterminates.
Theparentshellawakesanddisplaysanewprompt.
1.
4.
Buildingblocks1.
4.
1.
Shellbuildingblocks1.
4.
1.
1.
ShellsyntaxIfinputisnotcommented,theshellreadsitanddividesitintowordsandoperators,employingquotingrulestodefinethemeaningofeachcharacterofinput.
Thenthesewordsandoperatorsaretranslatedintocommandsandotherconstructs,whichreturnanexitstatusavailableforinspectionorprocessing.
Theabovefork-and-execschemeisonlyappliedaftertheshellhasanalyzedinputinthefollowingway:BashGuideforBeginnersChapter1.
BashandBashscripts13Theshellreadsitsinputfromafile,fromastringorfromtheuser'sterminal.
Inputisbrokenupintowordsandoperators,obeyingthequotingrules,seeChapter3.
Thesetokensareseparatedbymetacharacters.
Aliasexpansionisperformed.
Theshellparses(analyzesandsubstitutes)thetokensintosimpleandcompoundcommands.
Bashperformsvariousshellexpansions,breakingtheexpandedtokensintolistsoffilenamesandcommandsandarguments.
Redirectionisperformedifnecessary,redirectionoperatorsandtheiroperandsareremovedfromtheargumentlist.
Commandsareexecuted.
Optionallytheshellwaitsforthecommandtocompleteandcollectsitsexitstatus.
1.
4.
1.
2.
ShellcommandsAsimpleshellcommandsuchastouchfile1file2file3consistsofthecommanditselffollowedbyarguments,separatedbyspaces.
Morecomplexshellcommandsarecomposedofsimplecommandsarrangedtogetherinavarietyofways:inapipelineinwhichtheoutputofonecommandbecomestheinputofasecond,inalooporconditionalconstruct,orinsomeothergrouping.
Acoupleofexamples:ls|moregunzipfile.
tar.
gz|tarxvf-1.
4.
1.
3.
ShellfunctionsShellfunctionsareawaytogroupcommandsforlaterexecutionusingasinglenameforthegroup.
Theyareexecutedjustlikea"regular"command.
Whenthenameofashellfunctionisusedasasimplecommandname,thelistofcommandsassociatedwiththatfunctionnameisexecuted.
Shellfunctionsareexecutedinthecurrentshellcontext;nonewprocessiscreatedtointerpretthem.
FunctionsareexplainedinChapter11.
1.
4.
1.
4.
ShellparametersAparameterisanentitythatstoresvalues.
Itcanbeaname,anumberoraspecialvalue.
Fortheshell'spurpose,avariableisaparameterthatstoresaname.
Avariablehasavalueandzeroormoreattributes.
Variablesarecreatedwiththedeclareshellbuilt-incommand.
Ifnovalueisgiven,avariableisassignedthenullstring.
Variablescanonlyberemovedwiththeunsetbuilt-in.
AssigningvariablesisdiscussedinSection3.
2,advanceduseofvariablesinChapter10.
1.
4.
1.
5.
ShellexpansionsShellexpansionisperformedaftereachcommandlinehasbeensplitintotokens.
Thesearetheexpansionsperformed:BashGuideforBeginnersChapter1.
BashandBashscripts14BraceexpansionTildeexpansionParameterandvariableexpansionCommandsubstitutionArithmeticexpansionWordsplittingFilenameexpansionWe'lldiscusstheseexpansiontypesindetailinSection3.
4.
1.
4.
1.
6.
RedirectionsBeforeacommandisexecuted,itsinputandoutputmayberedirectedusingaspecialnotationinterpretedbytheshell.
Redirectionmayalsobeusedtoopenandclosefilesforthecurrentshellexecutionenvironment.
1.
4.
1.
7.
ExecutingcommandsWhenexecutingacommand,thewordsthattheparserhasmarkedasvariableassignments(precedingthecommandname)andredirectionsaresavedforlaterreference.
Wordsthatarenotvariableassignmentsorredirectionsareexpanded;thefirstremainingwordafterexpansionistakentobethenameofthecommandandtherestareargumentstothatcommand.
Thenredirectionsareperformed,thenstringsassignedtovariablesareexpanded.
Ifnocommandnameresults,variableswillaffectthecurrentshellenvironment.
Animportantpartofthetasksoftheshellistosearchforcommands.
Bashdoesthisasfollows:Checkwhetherthecommandcontainsslashes.
Ifnot,firstcheckwiththefunctionlisttoseeifitcontainsacommandbythenamewearelookingfor.
Ifcommandisnotafunction,checkforitinthebuilt-inlist.
Ifcommandisneitherafunctionnorabuilt-in,lookforitanalyzingthedirectorieslistedinPATH.
Bashusesahashtable(datastorageareainmemory)torememberthefullpathnamesofexecutablessoextensivePATHsearchescanbeavoided.
Ifthesearchisunsuccessful,bashprintsanerrormessageandreturnsanexitstatusof127.
Ifthesearchwassuccessfulorifthecommandcontainsslashes,theshellexecutesthecommandinaseparateexecutionenvironment.
Ifexecutionfailsbecausethefileisnotexecutableandnotadirectory,itisassumedtobeashellscript.
Ifthecommandwasnotbegunasynchronously,theshellwaitsforthecommandtocompleteandcollectsitsexitstatus.
1.
4.
1.
8.
ShellscriptsWhenafilecontainingshellcommandsisusedasthefirstnon-optionargumentwheninvokingBash(without-cor-s,thiswillcreateanon-interactiveshell.
Thisshellfirstsearchesforthescriptfileinthecurrentdirectory,thenlooksinPATHifthefilecannotbefoundthere.
1.
5.
DevelopinggoodscriptsBashGuideforBeginnersChapter1.
BashandBashscripts151.
5.
1.
PropertiesofgoodscriptsThisguideismainlyaboutthelastshellbuildingblock,scripts.
Somegeneralconsiderationsbeforewecontinue:Ascriptshouldrunwithouterrors.
1.
Itshouldperformthetaskforwhichitisintended.
2.
Programlogicisclearlydefinedandapparent.
3.
Ascriptdoesnotdounnecessarywork.
4.
Scriptsshouldbereusable.
5.
1.
5.
2.
StructureThestructureofashellscriptisveryflexible.
EventhoughinBashalotoffreedomisgranted,youmustensurecorrectlogic,flowcontrolandefficiencysothatusersexecutingthescriptcandosoeasilyandcorrectly.
Whenstartingonanewscript,askyourselfthefollowingquestions:WillIbeneedinganyinformationfromtheuserorfromtheuser'senvironmentHowwillIstorethatinformationArethereanyfilesthatneedtobecreatedWhereandwithwhichpermissionsandownershipsWhatcommandswillIuseWhenusingthescriptondifferentsystems,doallthesesystemshavethesecommandsintherequiredversionsDoestheuserneedanynotificationsWhenandwhy1.
5.
3.
TerminologyThetablebelowgivesanoverviewofprogrammingtermsthatyouneedtobefamiliarwith:Table1-1.
OverviewofprogrammingtermsTermWhatisitCommandcontrolTestingexitstatusofacommandinordertodeterminewhetheraportionoftheprogramshouldbeexecuted.
ConditionalbranchLogicalpointintheprogramwhenaconditiondetermineswhathappensnext.
LogicflowTheoveralldesignoftheprogram.
Determineslogicalsequenceoftaskssothattheresultissuccessfulandcontrolled.
LoopPartoftheprogramthatisperformedzeroormoretimes.
UserinputInformationprovidedbyanexternalsourcewhiletheprogramisrunning,canbestoredandrecalledwhenneeded.
1.
5.
4.
AwordonorderandlogicInordertospeedupthedevelopingprocess,thelogicalorderofaprogramshouldbethoughtoverinadvance.
Thisisyourfirststepwhendevelopingascript.
BashGuideforBeginnersChapter1.
BashandBashscripts16Anumberofmethodscanbeused;oneofthemostcommonisworkingwithlists.
Itemizingthelistoftasksinvolvedinaprogramallowsyoutodescribeeachprocess.
Individualtaskscanbereferencedbytheiritemnumber.
Usingyourownspokenlanguagetopindownthetaskstobeexecutedbyyourprogramwillhelpyoutocreateanunderstandableformofyourprogram.
Later,youcanreplacetheeverydaylanguagestatementswithshelllanguagewordsandconstructs.
Theexamplebelowshowssuchalogicflowdesign.
Itdescribestherotationoflogfiles.
Thisexampleshowsapossiblerepetitiveloop,controlledbythenumberofbaselogfilesyouwanttorotate:DoyouwanttorotatelogsIfyes:Enterdirectorynamecontainingthelogstoberotated.
i.
Enterbasenameofthelogfile.
ii.
Enternumberofdayslogsshouldbekept.
iii.
Makesettingspermanentinuser'scrontabfile.
iv.
a.
Ifno,gotostep3.
b.
1.
DoyouwanttorotateanothersetoflogsIfyes:repeatstep1.
a.
Ifno:gotostep3.
b.
2.
Exit3.
Theusershouldprovideinformationfortheprogramtodosomething.
Inputfromtheusermustbeobtainedandstored.
Theusershouldbenotifiedthathiscrontabwillchange.
1.
5.
5.
AnexampleBashscript:mysystem.
shThemysystem.
shscriptbelowexecutessomewell-knowncommands(date,w,uname,uptime)todisplayinformationaboutyouandyourmachine.
tom:~>cat-nmysystem.
sh1#!
/bin/bash2clear3echo"Thisisinformationprovidedbymysystem.
sh.
Programstartsnow.
"45echo"Hello,$USER"6echo78echo"Today'sdateis`date`,thisisweek`date+"%V"`.
"9echo1011echo"Theseusersarecurrentlyconnected:"12w|cut-d""-f1-|grep-vUSER|sort-u13echo1415echo"Thisis`uname-s`runningona`uname-m`processor.
"16echo1718echo"Thisistheuptimeinformation:"19uptimeBashGuideforBeginnersChapter1.
BashandBashscripts1720echo2122echo"That'sallfolks!
"Ascriptalwaysstartswiththesametwocharacters,"#!
".
Afterthat,theshellthatwillexecutethecommandsfollowingthefirstlineisdefined.
Thisscriptstartswithclearingthescreenonline2.
Line3makesitprintamessage,informingtheuseraboutwhatisgoingtohappen.
Line5greetstheuser.
Lines6,9,13,16and20areonlytherefororderlyoutputdisplaypurposes.
Line8printsthecurrentdateandthenumberoftheweek.
Line11isagainaninformativemessage,likelines3,18and22.
Line12formatstheoutputofthew;line15showsoperatingsystemandCPUinformation.
Line19givestheuptimeandloadinformation.
BothechoandprintfareBashbuilt-incommands.
Thefirstalwaysexitswitha0status,andsimplyprintsargumentsfollowedbyanendoflinecharacteronthestandardoutput,whilethelatterallowsfordefinitionofaformattingstringandgivesanon-zeroexitstatuscodeuponfailure.
Thisisthesamescriptusingtheprintfbuilt-in:tom:~>catmysystem.
sh#!
/bin/bashclearprintf"Thisisinformationprovidedbymysystem.
sh.
Programstartsnow.
\n"printf"Hello,$USER.
\n\n"printf"Today'sdateis`date`,thisisweek`date+"%V"`.
\n\n"printf"Theseusersarecurrentlyconnected:\n"w|cut-d""-f1-|grep-vUSER|sort-uprintf"\n"printf"Thisis`uname-s`runningona`uname-m`processor.
\n\n"printf"Thisistheuptimeinformation:\n"uptimeprintf"\n"printf"That'sallfolks!
\n"CreatinguserfriendlyscriptsbymeansofinsertingmessagesistreatedinChapter8.
StandardlocationoftheBourneAgainshellThisimpliesthatthebashprogramisinstalledin/bin.
IfstdoutisnotavailableIfyouexecuteascriptfromcron,supplyfullpathnamesandredirectoutputanderrors.
Sincetheshellrunsinnon-interactivemode,anyerrorswillcausethescripttoexitprematurelyifyoudon'tthinkaboutthis.
Thefollowingchapterswilldiscussthedetailsoftheabovescripts.
1.
5.
6.
ExampleinitscriptAninitscriptstartssystemservicesonUNIXandLinuxmachines.
Thesystemlogdaemon,thepowermanagementdaemon,thenameandmaildaemonsarecommonexamples.
Thesescripts,alsoknownasstartupscripts,arestoredinaspecificlocationonyoursystem,suchas/etc/rc.
d/init.
dor/etc/init.
d.
Init,theinitialprocess,readsitsconfigurationfilesanddecideswhichservicestostartorBashGuideforBeginnersChapter1.
BashandBashscripts18stopineachrunlevel.
Arunlevelisaconfigurationofprocesses;eachsystemhasasingleuserrunlevel,forinstance,forperformingadministrativetasks,forwhichthesystemhastobeinanunusedstateasmuchaspossible,suchasrecoveringacriticalfilesystemfromabackup.
Rebootandshutdownrunlevelsareusuallyalsoconfigured.
Thetaskstobeexecuteduponstartingaserviceorstoppingitarelistedinthestartupscripts.
Itisoneofthesystemadministrator'staskstoconfigureinit,sothatservicesarestartedandstoppedatthecorrectmoment.
Whenconfrontedwiththistask,youneedagoodunderstandingofthestartupandshutdownproceduresonyoursystem.
Wethereforeadvisethatyoureadthemanpagesforinitandinittabbeforestartingonyourowninitializationscripts.
Hereisaverysimpleexample,thatwillplayasounduponstartingandstoppingyourmachine:#!
/bin/bash#Thisscriptisfor/etc/rc.
d/init.
d#Linkinrc3.
d/S99audio-greetingandrc0.
d/K01audio-greetingcase"$1"in'start')cat/usr/share/audio/at_your_service.
au>/dev/audio;;'stop')cat/usr/share/audio/oh_no_not_again.
au>/dev/audio;;esacexit0ThecasestatementoftenusedinthiskindofscriptisdescribedinSection7.
2.
5.
1.
6.
SummaryBashistheGNUshell,compatiblewiththeBourneshellandincorporatingmanyusefulfeaturesfromothershells.
Whentheshellisstarted,itreadsitsconfigurationfiles.
Themostimportantare:/etc/profile~/.
bash_profile~/.
bashrcBashbehavesdifferentwhenininteractivemodeandalsohasaPOSIXcompliantandarestrictedmode.
Shellcommandscanbesplitupinthreegroups:theshellfunctions,shellbuilt-insandexistingcommandsinadirectoryonyoursystem.
Bashsupportsadditionalbuilt-insnotfoundintheplainBourneshell.
Shellscriptsconsistofthesecommandsarrangedasshellsyntaxdictates.
Scriptsarereadandexecutedlineperlineandshouldhavealogicalstructure.
1.
7.
ExercisesThesearesomeexercisestowarmyouupforthenextchapter:Whereisthebashprogramlocatedonyoursystem1.
Usethe--versionoptiontofindoutwhichversionyouarerunning.
2.
BashGuideforBeginnersChapter1.
BashandBashscripts19Whichshellconfigurationfilesarereadwhenyoulogintoyoursystemusingthegraphicaluserinterfaceandthenopeningaterminalwindow3.
ArethefollowingshellsinteractiveshellsAretheyloginshellsAshellopenedbyclickingonthebackgroundofyourgraphicaldesktop,selecting"Terminal"orsuchfromamenu.
Ashellthatyougetafterissuingthecommandsshlocalhost.
Ashellthatyougetwhenloggingintotheconsoleintextmode.
Ashellobtainedbythecommandxterm&.
Ashellopenedbythemysystem.
shscript.
Ashellthatyougetonaremotehost,forwhichyoudidn'thavetogivetheloginand/orpasswordbecauseyouuseSSHandmaybeSSHkeys.
4.
CanyouexplainwhybashdoesnotexitwhenyoutypeCtrl+Conthecommandline5.
Displaydirectorystackcontent.
6.
Ifitisnotyetthecase,setyourpromptsothatitdisplaysyourlocationinthefilesystemhierarchy,forinstanceaddthislineto~/.
bashrc:exportPS1="\u@\h\w>"7.
Displayhashedcommandsforyourcurrentshellsession.
8.
HowmanyprocessesarecurrentlyrunningonyoursystemUsepsandwc,thefirstlineofoutputofpsisnotaprocess!
9.
HowtodisplaythesystemhostnameOnlythename,nothingmore!
10.
BashGuideforBeginnersChapter1.
BashandBashscripts20Chapter2.
WritinganddebuggingscriptsAftergoingthroughthischapter,youwillbeableto:WriteasimplescriptDefinetheshelltypethatshouldexecutethescriptPutcommentsinascriptChangepermissionsonascriptExecuteanddebugascript2.
1.
Creatingandrunningascript2.
1.
1.
WritingandnamingAshellscriptisasequenceofcommandsforwhichyouhavearepeateduse.
Thissequenceistypicallyexecutedbyenteringthenameofthescriptonthecommandline.
Alternatively,youcanusescriptstoautomatetasksusingthecronfacility.
AnotheruseforscriptsisintheUNIXbootandshutdownprocedure,whereoperationofdaemonsandservicesaredefinedininitscripts.
Tocreateashellscript,openanewemptyfileinyoureditor.
Anytexteditorwilldo:vim,emacs,gedit,dtpadetceteraareallvalid.
Youmightwanttochoseamoreadvancededitorlikevimoremacs,however,becausethesecanbeconfiguredtorecognizeshellandBashsyntaxandcanbeagreathelpinpreventingthoseerrorsthatbeginnersfrequentlymake,suchasforgettingbracketsandsemi-colons.
SyntaxhighlightinginvimInordertoactivatesyntaxhighlightinginvim,usethecommand:syntaxenableor:syenableor:synenableYoucanaddthissettingtoyour.
vimrcfiletomakeitpermanent.
PutUNIXcommandsinthenewemptyfile,likeyouwouldenterthemonthecommandline.
Asdiscussedinthepreviouschapter(seeSection1.
3),commandscanbeshellfunctions,shellbuilt-ins,UNIXcommandsandotherscripts.
Giveyourscriptasensiblenamethatgivesahintaboutwhatthescriptdoes.
Makesurethatyourscriptnamedoesnotconflictwithexistingcommands.
Inordertoensurethatnoconfusioncanrise,scriptnamesoftenendin.
sh;evenso,theremightbeotherscriptsonyoursystemwiththesamenameastheoneyouchose.
Checkusingwhich,whereisandothercommandsforfindinginformationaboutprogramsandfiles:Chapter2.
Writinganddebuggingscripts21which-ascript_namewhereisscript_namelocatescript_name2.
1.
2.
script1.
shInthisexampleweusetheechoBashbuilt-intoinformtheuseraboutwhatisgoingtohappen,beforethetaskthatwillcreatetheoutputisexecuted.
Itisstronglyadvisedtoinformusersaboutwhatascriptisdoing,inordertopreventthemfrombecomingnervousbecausethescriptisnotdoinganything.
WewillreturntothesubjectofnotifyingusersinChapter8.
Figure2-1.
script1.
shWritethisscriptforyourselfaswell.
Itmightbeagoodideatocreateadirectory~/scriptstoholdyourscripts.
AddthedirectorytothecontentsofthePATHvariable:exportPATH="$PATH:~/scripts"IfyouarejustgettingstartedwithBash,useatexteditorthatusesdifferentcoloursfordifferentshellconstructs.
Syntaxhighlightingissupportedbyvim,gvim,(x)emacs,kwriteandmanyothereditors;checkBashGuideforBeginnersChapter2.
Writinganddebuggingscripts22thedocumentationofyourfavoriteeditor.
DifferentpromptsThepromptsthroughoutthiscoursevarydependingontheauthor'smood.
Thisresemblesmuchmorereallifesituationsthanthestandardeducational$prompt.
Theonlyconventionwestickto,isthattherootpromptendsinahashmark(#).
2.
1.
3.
ExecutingthescriptThescriptshouldhaveexecutepermissionsforthecorrectownersinordertoberunnable.
Whensettingpermissions,checkthatyoureallyobtainedthepermissionsthatyouwant.
Whenthisisdone,thescriptcanrunlikeanyothercommand:willy:~/scripts>chmodu+xscript1.
shwilly:~/scripts>ls-lscript1.
sh-rwxrw-r--1willywilly456Dec2417:11script1.
shwilly:~>script1.
shThescriptstartsnow.
Hi,willy!
Iwillnowfetchyoualistofconnectedusers:3:38pmup18days,5:37,4users,loadaverage:0.
12,0.
22,0.
15USERTTYFROMLOGIN@IDLEJCPUPCPUWHATroottty2-Sat2pm4:25m0.
24s0.
05s-bashwilly:0-Sat2pm0.
00s-willypts/3-Sat2pm3:33m36.
39s36.
39sBitchXwillyirwillypts/2-Sat2pm3:33m0.
13s0.
06s/usr/bin/screenI'msettingtwovariablesnow.
Thisisastring:blackAndthisisanumber:9I'mgivingyoubackyourpromptnow.
willy:~/scripts>echo$COLOURwilly:~/scripts>echo$VALUEwilly:~/scripts>Thisisthemostcommonwaytoexecuteascript.
Itispreferredtoexecutethescriptlikethisinasubshell.
Thevariables,functionsandaliasescreatedinthissubshellareonlyknowntotheparticularbashsessionofthatsubshell.
Whenthatshellexitsandtheparentregainscontrol,everythingiscleanedupandallchangestothestateoftheshellmadebythescript,areforgotten.
IfyoudidnotputthescriptsdirectoryinyourPATH,and.
(thecurrentdirectory)isnotinthePATHeither,youcanactivatethescriptlikethis:.
/script_name.
shAscriptcanalsoexplicitlybeexecutedbyagivenshell,butgenerallyweonlydothisifwewanttoobtainspecialbehavior,suchascheckingifthescriptworkswithanothershellorprintingtracesfordebugging:BashGuideforBeginnersChapter2.
Writinganddebuggingscripts23rbashscript_name.
shshscript_name.
shbash-xscript_name.
shThespecifiedshellwillstartasasubshellofyourcurrentshellandexecutethescript.
Thisisdonewhenyouwantthescripttostartupwithspecificoptionsorunderspecificconditionswhicharenotspecifiedinthescript.
Ifyoudon'twanttostartanewshellbutexecutethescriptinthecurrentshell,yousourceit:sourcescript_name.
shsource=.
TheBashsourcebuilt-inisasynonymfortheBourneshell.
(dot)command.
Thescriptdoesnotneedexecutepermissioninthiscase.
Commandsareexecutedinthecurrentshellcontext,soanychangesmadetoyourenvironmentwillbevisiblewhenthescriptfinishesexecution:willy:~/scripts>sourcescript1.
sh--outputommitted--willy:~/scripts>echo$VALUE9willy:~/scripts>2.
2.
Scriptbasics2.
2.
1.
WhichshellwillrunthescriptWhenrunningascriptinasubshell,youshoulddefinewhichshellshouldrunthescript.
Theshelltypeinwhichyouwrotethescriptmightnotbethedefaultonyoursystem,socommandsyouenteredmightresultinerrorswhenexecutedbythewrongshell.
Thefirstlineofthescriptdeterminestheshelltostart.
Thefirsttwocharactersofthefirstlineshouldbe#!
,thenfollowsthepathtotheshellthatshouldinterpretthecommandsthatfollow.
Blanklinesarealsoconsideredtobelines,sodon'tstartyourscriptwithanemptyline.
Forthepurposeofthiscourse,allscriptswillstartwiththeline#!
/bin/bashAsnotedbefore,thisimpliesthattheBashexecutablecanbefoundin/bin.
2.
2.
2.
AddingcommentsYoushouldbeawareofthefactthatyoumightnotbetheonlypersonreadingyourcode.
Alotofusersandsystemadministratorsrunscriptsthatwerewrittenbyotherpeople.
Iftheywanttoseehowyoudidit,commentsareusefultoenlightenthereader.
BashGuideforBeginnersChapter2.
Writinganddebuggingscripts24Commentsalsomakeyourownlifeeasier.
Saythatyouhadtoreadalotofmanpagesinordertoachieveaparticularresultwithsomecommandthatyouusedinyourscript.
Youwon'trememberhowitworkedifyouneedtochangeyourscriptafterafewweeksormonths,unlessyouhavecommentedwhatyoudid,howyoudiditand/orwhyyoudidit.
Takethescript1.
shexampleandcopyittocommented-script1.
sh,whichweeditsothatthecommentsreflectwhatthescriptdoes.
Everythingtheshellencountersafterahashmarkonalineisignoredandonlyvisibleuponopeningtheshellscriptfile:#!
/bin/bash#Thisscriptclearstheterminal,displaysagreetingandgivesinformation#aboutcurrentlyconnectedusers.
Thetwoexamplevariablesaresetanddisplayed.
clear#clearterminalwindowecho"Thescriptstartsnow.
"echo"Hi,$USER!
"#dollarsignisusedtogetcontentofvariableechoecho"Iwillnowfetchyoualistofconnectedusers:"echow#showwhoisloggedonandecho#whattheyaredoingecho"I'msettingtwovariablesnow.
"COLOUR="black"#setalocalshellvariableVALUE="9"#setalocalshellvariableecho"Thisisastring:$COLOUR"#displaycontentofvariableecho"Andthisisanumber:$VALUE"#displaycontentofvariableechoecho"I'mgivingyoubackyourpromptnow.
"echoInadecentscript,thefirstlinesareusuallycommentaboutwhattoexpect.
Theneachbigchunkofcommandswillbecommentedasneededforclarity'ssake.
Linuxinitscripts,asanexample,inyoursystem'sinit.
ddirectory,areusuallywellcommentedsincetheyhavetobereadableandeditablebyeveryonerunningLinux.
2.
3.
DebuggingBashscripts2.
3.
1.
DebuggingontheentirescriptWhenthingsdon'tgoaccordingtoplan,youneedtodeterminewhatexactlycausesthescripttofail.
Bashprovidesextensivedebuggingfeatures.
Themostcommonistostartupthesubshellwiththe-xoption,whichwillruntheentirescriptindebugmode.
Tracesofeachcommandplusitsargumentsareprintedtostandardoutputafterthecommandshavebeenexpandedbutbeforetheyareexecuted.
Thisisthecommented-script1.
shscriptranindebugmode.
Noteagainthattheaddedcommentsarenotvisibleintheoutputofthescript.
willy:~/scripts>bash-xscript1.
sh+clear+echo'Thescriptstartsnow.
'Thescriptstartsnow.
BashGuideforBeginnersChapter2.
Writinganddebuggingscripts25+echo'Hi,willy!
'Hi,willy!
+echo+echo'Iwillnowfetchyoualistofconnectedusers:'Iwillnowfetchyoualistofconnectedusers:+echo+w4:50pmup18days,6:49,4users,loadaverage:0.
58,0.
62,0.
40USERTTYFROMLOGIN@IDLEJCPUPCPUWHATroottty2-Sat2pm5:36m0.
24s0.
05s-bashwilly:0-Sat2pm0.
00s-willypts/3-Sat2pm43:1336.
82s36.
82sBitchXwillyirwillypts/2-Sat2pm43:130.
13s0.
06s/usr/bin/screen+echo+echo'I'\''msettingtwovariablesnow.
'I'msettingtwovariablesnow.
+COLOUR=black+VALUE=9+echo'Thisisastring:'Thisisastring:+echo'Andthisisanumber:'Andthisisanumber:+echo+echo'I'\''mgivingyoubackyourpromptnow.
'I'mgivingyoubackyourpromptnow.
+echoThereisnowafull-fledgeddebuggerforBash,availableatSourceForge.
ThesedebuggingfeaturesareavailableinmostmodernversionsofBash,startingfrom3.
x.
2.
3.
2.
Debuggingonpart(s)ofthescriptUsingthesetBashbuilt-inyoucanruninnormalmodethoseportionsofthescriptofwhichyouaresuretheyarewithoutfault,anddisplaydebugginginformationonlyfortroublesomezones.
Saywearenotsurewhatthewcommandwilldointheexamplecommented-script1.
sh,thenwecouldencloseitinthescriptlikethis:set-x#activatedebuggingfromherewset+x#stopdebuggingfromhereOutputthenlookslikethis:willy:~/scripts>script1.
shThescriptstartsnow.
Hi,willy!
Iwillnowfetchyoualistofconnectedusers:+w5:00pmup18days,7:00,4users,loadaverage:0.
79,0.
39,0.
33USERTTYFROMLOGIN@IDLEJCPUPCPUWHATroottty2-Sat2pm5:47m0.
24s0.
05s-bashwilly:0-Sat2pm0.
00s-willypts/3-Sat2pm54:0236.
88s36.
88sBitchXwillykewillypts/2-Sat2pm54:020.
13s0.
06s/usr/bin/screenBashGuideforBeginnersChapter2.
Writinganddebuggingscripts26+set+xI'msettingtwovariablesnow.
Thisisastring:Andthisisanumber:I'mgivingyoubackyourpromptnow.
willy:~/scripts>Youcanswitchdebuggingmodeonandoffasmanytimesasyouwantwithinthesamescript.
ThetablebelowgivesanoverviewofotherusefulBashoptions:Table2-1.
OverviewofsetdebuggingoptionsShortnotationLongnotationResultset-fset-onoglobDisablefilenamegenerationusingmetacharacters(globbing).
set-vset-overbosePrintsshellinputlinesastheyareread.
set-xset-oxtracePrintcommandtracesbeforeexecutingcommand.
Thedashisusedtoactivateashelloptionandaplustodeactivateit.
Don'tletthisconfuseyou!
Intheexamplebelow,wedemonstratetheseoptionsonthecommandline:willy:~/scripts>set-vwilly:~/scripts>lslscommented-scripts.
shscript1.
shwilly:~/scripts>set+vset+vwilly:~/scripts>ls*commented-scripts.
shscript1.
shwilly:~/scripts>set-fwilly:~/scripts>ls*ls:*:Nosuchfileordirectorywilly:~/scripts>touch*willy:~/scripts>ls*commented-scripts.
shscript1.
shwilly:~/scripts>rm*willy:~/scripts>lscommented-scripts.
shscript1.
shAlternatively,thesemodescanbespecifiedinthescriptitself,byaddingthedesiredoptionstothefirstlineshelldeclaration.
Optionscanbecombined,asisusuallythecasewithUNIXcommands:#!
/bin/bash-xvBashGuideforBeginnersChapter2.
Writinganddebuggingscripts27Onceyoufoundthebuggypartofyourscript,youcanaddechostatementsbeforeeachcommandofwhichyouareunsure,sothatyouwillseeexactlywhereandwhythingsdon'twork.
Intheexamplecommented-script1.
shscript,itcouldbedonelikethis,stillassumingthatthedisplayingofusersgivesusproblems:echo"debugmessage:nowattemptingtostartwcommand";wInmoreadvancedscripts,theechocanbeinsertedtodisplaythecontentofvariablesatdifferentstagesinthescript,sothatflawscanbedetected:echo"VariableVARNAMEisnowsetto$VARNAME.
"2.
4.
SummaryAshellscriptisareusableseriesofcommandsputinanexecutabletextfile.
Anytexteditorcanbeusedtowritescripts.
Scriptsstartwith#!
followedbythepathtotheshellexecutingthecommandsfromthescript.
Commentsareaddedtoascriptforyourownfuturereference,andalsotomakeitunderstandableforotherusers.
Itisbettertohavetoomanyexplanationsthannotenough.
Debuggingascriptcanbedoneusingshelloptions.
Shelloptionscanbeusedforpartialdebuggingorforanalyzingtheentirescript.
Insertingechocommandsatstrategiclocationsisalsoacommontroubleshootingtechnique.
2.
5.
ExercisesThisexercisewillhelpyoutocreateyourfirstscript.
Writeascriptusingyourfavoriteeditor.
Thescriptshoulddisplaythepathtoyourhomedirectoryandtheterminaltypethatyouareusing.
Additionallyitshowsalltheservicesstartedupinrunlevel3onyoursystem.
(hint:useHOME,TERMandls/etc/rc3.
d/S*)1.
Addcommentsinyourscript.
2.
Addinformationfortheusersofyourscript.
3.
Changepermissionsonyourscriptsothatyoucanrunit.
4.
Runthescriptinnormalmodeandindebugmode.
Itshouldrunwithouterrors.
5.
Makeerrorsinyourscript:seewhathappensifyoumisspellcommands,ifyouleaveoutthefirstlineorputsomethingunintelligiblethere,orifyoumisspellshellvariablenamesorwritetheminlowercasecharactersaftertheyhavebeendeclaredincapitals.
Checkwhatthedebugcommentssayaboutthis.
6.
BashGuideforBeginnersChapter2.
Writinganddebuggingscripts28Chapter3.
TheBashenvironmentInthischapterwewilldiscussthevariouswaysinwhichtheBashenvironmentcanbeinfluenced:EditingshellinitializationfilesUsingvariablesUsingdifferentquotestylesPerformarithmeticcalculationsAssigningaliasesUsingexpansionandsubstitution3.
1.
Shellinitializationfiles3.
1.
1.
System-wideconfigurationfiles3.
1.
1.
1.
/etc/profileWheninvokedinteractivelywiththe--loginoptionorwheninvokedassh,Bashreadsthe/etc/profileinstructions.
TheseusuallysettheshellvariablesPATH,USER,MAIL,HOSTNAMEandHISTSIZE.
Onsomesystems,theumaskvalueisconfiguredin/etc/profile;onothersystemsthisfileholdspointerstootherconfigurationfilessuchas:/etc/inputrc,thesystem-wideReadlineinitializationfilewhereyoucanconfigurethecommandlinebell-style.
the/etc/profile.
ddirectory,whichcontainsfilesconfiguringsystem-widebehaviorofspecificprograms.
Allsettingsthatyouwanttoapplytoallyourusers'environmentsshouldbeinthisfile.
Itmightlooklikethis:#/etc/profile#Systemwideenvironmentandstartupprograms,forloginsetupPATH=$PATH:/usr/X11R6/bin#Nocorefilesbydefaultulimit-S-c0>/dev/null2>&1USER="`id-un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER"HOSTNAME=`/bin/hostname`HISTSIZE=1000#Keyboard,bell,displaystyle:thereadlineconfigfile:if[-z"$INPUTRC"-a!
-f"$HOME/.
inputrc"];thenINPUTRC=/etc/inputrcChapter3.
TheBashenvironment29fiPS1="\u@\h\W"exportPATHUSERLOGNAMEMAILHOSTNAMEHISTSIZEINPUTRCPS1#Sourceinitializationfilesforspecificprograms(ls,vim,less,.
.
.
)foriin/etc/profile.
d/*.
sh;doif[-r"$i"];then.
$ifidone#Settingsforprograminitializationsource/etc/java.
confexportNPX_PLUGIN_PATH="$JRE_HOME/plugin/ns4plugin/:/usr/lib/netscape/plugins"PAGER="/usr/bin/less"unsetiThisconfigurationfilesetssomebasicshellenvironmentvariablesaswellassomevariablesrequiredbyusersrunningJavaand/orJavaapplicationsintheirwebbrowser.
SeeSection3.
2.
SeeChapter7formoreontheconditionalifusedinthisfile;Chapter9discussesloopssuchastheforconstruct.
TheBashsourcecontainssampleprofilefilesforgeneralorindividualuse.
Theseandtheoneintheexampleaboveneedchangesinorderforthemtoworkinyourenvironment!
3.
1.
1.
2.
/etc/bashrcOnsystemsofferingmultipletypesofshells,itmightbebettertoputBash-specificconfigurationsinthisfile,since/etc/profileisalsoreadbyothershells,suchastheBourneshell.
Errorsgeneratedbyshellsthatdon'tunderstandtheBashsyntaxarepreventedbysplittingtheconfigurationfilesforthedifferenttypesofshells.
Insuchcases,theuser's~/.
bashrcmightpointto/etc/bashrcinordertoincludeitintheshellinitializationprocessuponlogin.
Youmightalsofindthat/etc/profileonyoursystemonlyholdsshellenvironmentandprogramstartupsettings,while/etc/bashrccontainssystem-widedefinitionsforshellfunctionsandaliases.
The/etc/bashrcfilemightbereferredtoin/etc/profileorinindividualusershellinitializationfiles.
Thesourcecontainssamplebashrcfiles,oryoumightfindacopyin/usr/share/doc/bash-2.
05b/startup-files.
ThisispartofthebashrcthatcomeswiththeBashdocumentation:aliasll='ls-l'aliasdir='ls-ba'aliasc='clear'aliasls='ls--color'aliasmroe='more'aliaspdw='pwd'aliassl='ls--color'pskill(){BashGuideforBeginnersChapter3.
TheBashenvironment30localpidpid=$(ps-ax|grep$1|grep-vgrep|gawk'{print$1}')echo-n"killing$1(process$pid).
.
.
"kill-9$pidecho"slaughtered.
"}Apartfromgeneralaliases,itcontainsusefulaliaseswhichmakecommandsworkevenifyoumisspellthem.
WewilldiscussaliasesinSection3.
5.
2.
Thisfilecontainsafunction,pskill;functionswillbestudiedindetailinChapter11.
3.
1.
2.
IndividualuserconfigurationfilesIdon'thavethesefiles!
Thesefilesmightnotbeinyourhomedirectorybydefault;createthemifneeded.
3.
1.
2.
1.
~/.
bash_profileThisisthepreferredconfigurationfileforconfiguringuserenvironmentsindividually.
Inthisfile,userscanaddextraconfigurationoptionsorchangedefaultsettings:franky~>cat.
bash_profile###.
bash_profilefile####Executedfromthebashshellwhenyoulogin.
###source~/.
bashrcsource~/.
bash_logincase"$OS"inIRIX)sttysanedecsttyerase;;#SunOS)#sttyerase#;;*)sttysane;;esacThisuserconfiguresthebackspacecharacterforloginondifferentoperatingsystems.
Apartfromthat,theuser's.
bashrcand.
bash_loginareread.
3.
1.
2.
2.
~/.
bash_loginThisfilecontainsspecificsettingsthatarenormallyonlyexecutedwhenyoulogintothesystem.
Intheexample,weuseittoconfiguretheumaskvalueandtoshowalistofconnectedusersuponlogin.
Thisuseralsogetsthecalendarforthecurrentmonth:BashGuideforBeginnersChapter3.
TheBashenvironment31###Bash_loginfile####commandstoperformfromthebashshellatlogintime##(sourcedfrom.
bash_profile)####fileprotectionumask002#alltome,readtogroupandothers#miscellaneouswcal`date+"%m"``date+"%Y"`Intheabsenceof~/.
bash_profile,thisfilewillberead.
3.
1.
2.
3.
~/.
profileIntheabsenceof~/.
bash_profileand~/.
bash_login,~/.
profileisread.
Itcanholdthesameconfigurations,whicharethenalsoaccessiblebyothershells.
MindthatothershellsmightnotunderstandtheBashsyntax.
3.
1.
2.
4.
~/.
bashrcToday,itismorecommontouseanon-loginshell,forinstancewhenloggedingraphicallyusingXterminalwindows.
Uponopeningsuchawindow,theuserdoesnothavetoprovideausernameorpassword;noauthenticationisdone.
Bashsearchesfor~/.
bashrcwhenthishappens,soitisreferredtointhefilesreaduponloginaswell,whichmeansyoudon'thavetoenterthesamesettingsinmultiplefiles.
Inthisuser's.
bashrcacoupleofaliasesaredefinedandvariablesforspecificprogramsaresetafterthesystem-wide/etc/bashrcisread:franky~>cat.
bashrc#/home/franky/.
bashrc#Sourceglobaldefinitionsif[-f/etc/bashrc];then.
/etc/bashrcfi#shelloptionsset-onoclobber#myshellvariablesexportPS1="\[\033[1;44m\]\u\w\[\033[0m\]"exportPATH="$PATH:~/bin:~/scripts"#myaliasesaliascdrecord='cdrecord-dev0,0,0-speed=8'aliasss='sshoctarine'aliasll='ls-la'#mozillafixBashGuideforBeginnersChapter3.
TheBashenvironment32MOZILLA_FIVE_HOME=/usr/lib/mozillaLD_LIBRARY_PATH=/usr/lib/mozilla:/usr/lib/mozilla/pluginsMOZ_DIST_BIN=/usr/lib/mozillaMOZ_PROGRAM=/usr/lib/mozilla/mozilla-binexportMOZILLA_FIVE_HOMELD_LIBRARY_PATHMOZ_DIST_BINMOZ_PROGRAM#fontfixaliasxt='xterm-bgblack-fgwhite&'#BitchXsettingsexportIRCNAME="frnk"#THEENDfranky~>MoreexamplescanbefoundintheBashpackage.
Rememberthatsamplefilesmightneedchangesinordertoworkinyourenvironment.
AliasesarediscussedinSection3.
5.
3.
1.
2.
5.
~/.
bash_logoutThisfilecontainsspecificinstructionsforthelogoutprocedure.
Intheexample,theterminalwindowiscleareduponlogout.
Thisisusefulforremoteconnections,whichwillleaveacleanwindowafterclosingthem.
franky~>cat.
bash_logout###Bash_logoutfile####commandstoperformfromthebashshellatlogouttime###clearfranky~>3.
1.
3.
ChangingshellconfigurationfilesWhenmakingchangestoanyoftheabovefiles,usershavetoeitherreconnecttothesystemorsourcethealteredfileforthechangestotakeeffect.
Byinterpretingthescriptthisway,changesareappliedtothecurrentshellsession:Figure3-1.
DifferentpromptsfordifferentusersBashGuideforBeginnersChapter3.
TheBashenvironment33Mostshellscriptsexecuteinaprivateenvironment:variablesarenotinheritedbychildprocessesunlesstheyareexportedbytheparentshell.
Sourcingafilecontainingshellcommandsisawayofapplyingchangestoyourownenvironmentandsettingvariablesinthecurrentshell.
Thisexamplealsodemonstratestheuseofdifferentpromptsettingsbydifferentusers.
Inthiscase,redmeansdanger.
Whenyouhaveagreenprompt,don'tworrytoomuch.
Notethatsourceresourcefileisthesameas.
resourcefile.
Shouldyougetlostinalltheseconfigurationfiles,andfindyourselfconfrontedwithsettingsofwhichtheoriginisnotclear,useechostatements,justlikefordebuggingscripts;seeSection2.
3.
2.
Youmightaddlineslikethis:echo"Nowexecuting.
bash_profile.
.
"orlikethis:echo"NowsettingPS1in.
bashrc:"exportPS1="[somevalue]"echo"PS1isnowsetto$PS1"3.
2.
Variables3.
2.
1.
TypesofvariablesAsseenintheexamplesabove,shellvariablesareinuppercasecharactersbyconvention.
Bashkeepsalistoftwotypesofvariables:3.
2.
1.
1.
GlobalvariablesGlobalvariablesorenvironmentvariablesareavailableinallshells.
Theenvorprintenvcommandscanbeusedtodisplayenvironmentvariables.
Theseprogramscomewiththesh-utilspackage.
Belowisatypicaloutput:BashGuideforBeginnersChapter3.
TheBashenvironment34franky~>printenvCC=gccCDPATH=.
:~:/usr/local:/usr:/CFLAGS=-O2-fomit-frame-pointerCOLORTERM=gnome-terminalCXXFLAGS=-O2-fomit-frame-pointerDISPLAY=:0DOMAIN=hq.
garrels.
bee=TOR=viFCEDIT=viFIGNORE=.
o:~G_BROKEN_FILENAMES=1GDK_USE_XFT=1GDMSESSION=DefaultGNOME_DESKTOP_SESSION_ID=DefaultGTK_RC_FILES=/etc/gtk/gtkrc:/nethome/franky/.
gtkrc-1.
2-gnome2GWMCOLOR=darkgreenGWMTERM=xtermHISTFILESIZE=5000history_control=ignoredupsHISTSIZE=2000HOME=/nethome/frankyHOSTNAME=octarine.
hq.
garrels.
beINPUTRC=/etc/inputrcIRCNAME=frankyJAVA_HOME=/usr/java/j2sdk1.
4.
0LANG=en_USLDFLAGS=-sLD_LIBRARY_PATH=/usr/lib/mozilla:/usr/lib/mozilla/pluginsLESSCHARSET=latin1LESS=-edfMQLESSOPEN=|/usr/bin/lesspipe.
sh%sLEX=flexLOCAL_MACHINE=octarineLOGNAME=frankyLS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:MACHINES=octarineMAILCHECK=60MAIL=/var/mail/frankyMANPATH=/usr/man:/usr/share/man/:/usr/local/man:/usr/X11R6/manMEAN_MACHINES=octarineMOZ_DIST_BIN=/usr/lib/mozillaMOZILLA_FIVE_HOME=/usr/lib/mozillaMOZ_PROGRAM=/usr/lib/mozilla/mozilla-binMTOOLS_FAT_COMPATIBILITY=1MYMALLOC=0NNTPPORT=119NNTPSERVER=newsNPX_PLUGIN_PATH=/plugin/ns4plugin/:/usr/lib/netscape/pluginsOLDPWD=/nethome/frankyOS=LinuxPAGER=lessPATH=/nethome/franky/bin.
Linux:/nethome/franky/bin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:PS1=\[\033[1;44m\]frankyisin\w\[\033[0m\]PS2=Moreinput>PWD=/nethome/frankySESSION_MANAGER=local/octarine.
hq.
garrels.
be:/tmp/.
ICE-unix/22106SHELL=/bin/bashSHELL_LOGIN=--loginSHLVL=2SSH_AGENT_PID=22161BashGuideforBeginnersChapter3.
TheBashenvironment35SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSSH_AUTH_SOCK=/tmp/ssh-XXmhQ4fC/agent.
22106START_WM=twmTERM=xtermTYPE=typeUSERNAME=frankyUSER=franky_=/usr/bin/printenvVISUAL=viWINDOWID=20971661XAPPLRESDIR=/nethome/franky/app-defaultsXAUTHORITY=/nethome/franky/.
XauthorityXENVIRONMENT=/nethome/franky/.
XdefaultsXFILESEARCHPATH=/usr/X11R6/lib/X11/%L/%T/%N%C%S:/usr/X11R6/lib/X11/%l/%T/%N%C%S:/usr/X11R6/lib/X1XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDBXMODIFIERS=@im=noneXTERMID=XWINHOME=/usr/X11R6X=X11R6YACC=bison-y3.
2.
1.
2.
LocalvariablesLocalvariablesareonlyavailableinthecurrentshell.
Usingthesetbuilt-incommandwithoutanyoptionswilldisplayalistofallvariables(includingenvironmentvariables)andfunctions.
Theoutputwillbesortedaccordingtothecurrentlocaleanddisplayedinareusableformat.
Belowisadifffilemadebycomparingprintenvandsetoutput,afterleavingoutthefunctionswhicharealsodisplayedbythesetcommand:franky~>diffset.
sortedprintenv.
sorted|grep"export1number=1bash:export:`1number=1':notavalididentifierTosetavariableintheshell,useVARNAME="value"Puttingspacesaroundtheequalsignwillcauseerrors.
Itisagoodhabittoquotecontentstringswhenassigningvaluestovariables:thiswillreducethechancethatyoumakeerrors.
Someexamplesusingupperandlowercases,numbersandspaces:franky~>MYVAR1="2"franky~>echo$MYVAR12franky~>first_name="Franky"franky~>echo$first_nameFrankyfranky~>full_name="FrankyM.
Singh"franky~>echo$full_nameFrankyM.
Singhfranky~>MYVAR-2="2"bash:MYVAR-2=2:commandnotfoundfranky~>MYVAR1="2"bash:MYVAR1:commandnotfoundfranky~>MYVAR1="2"BashGuideforBeginnersChapter3.
TheBashenvironment37bash:2:commandnotfoundfranky~>unsetMYVAR1first_namefull_namefranky~>echo$MYVAR1$first_name$full_namefranky~>3.
2.
3.
ExportingvariablesAvariablecreatedliketheonesintheexampleaboveisonlyavailabletothecurrentshell.
Itisalocalvariable:childprocessesofthecurrentshellwillnotbeawareofthisvariable.
Inordertopassvariablestoasubshell,weneedtoexportthemusingtheexportbuilt-incommand.
Variablesthatareexportedarereferredtoasenvironmentvariables.
Settingandexportingisusuallydoneinonestep:exportVARNAME="value"Asubshellcanchangevariablesitinheritedfromtheparent,butthechangesmadebythechilddon'taffecttheparent.
Thisisdemonstratedintheexample:franky~>full_name="FrankyM.
Singh"franky~>bashfranky~>echo$full_namefranky~>exitfranky~>exportfull_namefranky~>bashfranky~>echo$full_nameFrankyM.
Singhfranky~>exportfull_name="CharlestheGreat"franky~>echo$full_nameCharlestheGreatfranky~>exitfranky~>echo$full_nameFrankyM.
Singhfranky~>Whenfirsttryingtoreadthevalueoffull_nameinasubshell,itisnotthere(echoshowsanullstring).
Thesubshellquits,andfull_nameisexportedintheparent-avariablecanbeexportedafterithasbeenassignedavalue.
Thenanewsubshellisstarted,inwhichthevariableexportedfromtheparentisvisible.
Thevariableischangedtoholdanothername,butthevalueforthisvariableintheparentstaysthesame.
BashGuideforBeginnersChapter3.
TheBashenvironment383.
2.
4.
Reservedvariables3.
2.
4.
1.
BourneshellreservedvariablesBashusescertainshellvariablesinthesamewayastheBourneshell.
Insomecases,Bashassignsadefaultvaluetothevariable.
Thetablebelowgivesanoverviewoftheseplainshellvariables:Table3-1.
ReservedBourneshellvariablesVariablenameDefinitionCDPATHAcolon-separatedlistofdirectoriesusedasasearchpathforthecdbuilt-incommand.
HOMEThecurrentuser'shomedirectory;thedefaultforthecdbuilt-in.
Thevalueofthisvariableisalsousedbytildeexpansion.
IFSAlistofcharactersthatseparatefields;usedwhentheshellsplitswordsaspartofexpansion.
MAILIfthisparameterissettoafilenameandtheMAILPATHvariableisnotset,Bashinformstheuserofthearrivalofmailinthespecifiedfile.
MAILPATHAcolon-separatedlistoffilenameswhichtheshellperiodicallychecksfornewmail.
OPTARGThevalueofthelastoptionargumentprocessedbythegetoptsbuilt-in.
OPTINDTheindexofthelastoptionargumentprocessedbythegetoptsbuilt-in.
PATHAcolon-separatedlistofdirectoriesinwhichtheshelllooksforcommands.
PS1Theprimarypromptstring.
Thedefaultvalueis"'\s-\v\$'".
PS2Thesecondarypromptstring.
Thedefaultvalueis"'>'".
3.
2.
4.
2.
BashreservedvariablesThesevariablesaresetorusedbyBash,butothershellsdonotnormallytreatthemspecially.
Table3-2.
ReservedBashvariablesVariablenameDefinitionauto_resumeThisvariablecontrolshowtheshellinteractswiththeuserandjobcontrol.
BASHThefullpathnameusedtoexecutethecurrentinstanceofBash.
BASH_ENVIfthisvariableissetwhenBashisinvokedtoexecuteashellscript,itsvalueisexpandedandusedasthenameofastartupfiletoreadbeforeexecutingthescript.
BASH_VERSIONTheversionnumberofthecurrentinstanceofBash.
BASH_VERSINFOAread-onlyarrayvariablewhosemembersholdversioninformationforthisinstanceofBash.
COLUMNSUsedbytheselectbuilt-intodeterminetheterminalwidthwhenprintingselectionlists.
AutomaticallysetuponreceiptofaSIGWINCHsignal.
COMP_CWORDAnindexinto${COMP_WORDS}ofthewordcontainingthecurrentcursorposition.
COMP_LINEThecurrentcommandline.
COMP_POINTBashGuideforBeginnersChapter3.
TheBashenvironment39Theindexofthecurrentcursorpositionrelativetothebeginningofthecurrentcommand.
COMP_WORDSAnarrayvariableconsistingoftheindividualwordsinthecurrentcommandline.
COMPREPLYAnarrayvariablefromwhichBashreadsthepossiblecompletionsgeneratedbyashellfunctioninvokedbytheprogrammablecompletionfacility.
DIRSTACKAnarrayvariablecontainingthecurrentcontentsofthedirectorystack.
EUIDThenumericeffectiveuserIDofthecurrentuser.
FCEDITTheeditorusedasadefaultbythe-eoptiontothefcbuilt-incommand.
FIGNOREAcolon-separatedlistofsuffixestoignorewhenperformingfilenamecompletion.
FUNCNAMEThenameofanycurrently-executingshellfunction.
GLOBIGNOREAcolon-separatedlistofpatternsdefiningthesetoffilenamestobeignoredbyfilenameexpansion.
GROUPSAnarrayvariablecontainingthelistofgroupsofwhichthecurrentuserisamember.
histcharsUptothreecharacterswhichcontrolhistoryexpansion,quicksubstitution,andtokenization.
HISTCMDThehistorynumber,orindexinthehistorylist,ofthecurrentcommand.
HISTCONTROLDefineswhetheracommandisaddedtothehistoryfile.
HISTFILEThenameofthefiletowhichthecommandhistoryissaved.
Thedefaultvalueis~/.
bash_history.
HISTFILESIZEThemaximumnumberoflinescontainedinthehistoryfile,defaultsto500.
HISTIGNOREAcolon-separatedlistofpatternsusedtodecidewhichcommandlinesshouldbesavedinthehistorylist.
HISTSIZEThemaximumnumberofcommandstorememberonthehistorylist,defaultis500.
HOSTFILEContainsthenameofafileinthesameformatas/etc/hoststhatshouldbereadwhentheshellneedstocompleteahostname.
HOSTNAMEThenameofthecurrenthost.
HOSTTYPEAstringdescribingthemachineBashisrunningon.
IGNOREEOFControlstheactionoftheshellonreceiptofanEOFcharacterasthesoleinput.
INPUTRCThenameoftheReadlineinitializationfile,overridingthedefault/etc/inputrc.
LANGUsedtodeterminethelocalecategoryforanycategorynotspecificallyselectedwithavariablestartingwithLC_.
LC_ALLThisvariableoverridesthevalueofLANGandanyotherLC_variablespecifyingalocalecategory.
LC_COLLATEThisvariabledeterminesthecollationorderusedwhensortingtheresultsoffilenameexpansion,anddeterminesthebehaviorofrangeexpressions,equivalenceclasses,andcollatingsequenceswithinfilenameexpansionandpatternmatching.
LC_CTYPEThisvariabledeterminestheinterpretationofcharactersandthebehaviorofcharacterclasseswithinfilenameexpansionandpatternmatching.
LC_MESSAGESThisvariabledeterminesthelocaleusedtotranslatedouble-quotedstringsprecededbya"$"sign.
LC_NUMERICThisvariabledeterminesthelocalecategoryusedfornumberformatting.
LINENOThelinenumberinthescriptorshellfunctioncurrentlyexecuting.
BashGuideforBeginnersChapter3.
TheBashenvironment40LINESUsedbytheselectbuilt-intodeterminethecolumnlengthforprintingselectionlists.
MACHTYPEAstringthatfullydescribesthesystemtypeonwhichBashisexecuting,inthestandardGNUCPU-COMPANY-SYSTEMformat.
MAILCHECKHowoften(inseconds)thattheshellshouldcheckformailinthefilesspecifiedintheMAILPATHorMAILvariables.
OLDPWDThepreviousworkingdirectoryassetbythecdbuilt-in.
OPTERRIfsettothevalue1,Bashdisplayserrormessagesgeneratedbythegetoptsbuilt-in.
OSTYPEAstringdescribingtheoperatingsystemBashisrunningon.
PIPESTATUSAnarrayvariablecontainingalistofexitstatusvaluesfromtheprocessesinthemostrecentlyexecutedforegroundpipeline(whichmaycontainonlyasinglecommand).
POSIXLY_CORRECTIfthisvariableisintheenvironmentwhenbashstarts,theshellentersPOSIXmode.
PPIDTheprocessIDoftheshell'sparentprocess.
PROMPT_COMMANDIfset,thevalueisinterpretedasacommandtoexecutebeforetheprintingofeachprimaryprompt(PS1).
PS3Thevalueofthisvariableisusedasthepromptfortheselectcommand.
Defaultsto"'#'"PS4Thevalueisthepromptprintedbeforethecommandlineisechoedwhenthe-xoptionisset;defaultsto"'+'".
PWDThecurrentworkingdirectoryassetbythecdbuilt-incommand.
RANDOMEachtimethisparameterisreferenced,arandomintegerbetween0and32767isgenerated.
Assigningavaluetothisvariableseedstherandomnumbergenerator.
REPLYThedefaultvariableforthereadbuilt-in.
SECONDSThisvariableexpandstothenumberofsecondssincetheshellwasstarted.
SHELLOPTSAcolon-separatedlistofenabledshelloptions.
SHLVLIncrementedbyoneeachtimeanewinstanceofBashisstarted.
TIMEFORMATThevalueofthisparameterisusedasaformatstringspecifyinghowthetiminginformationforpipelinesprefixedwiththetimereservedwordshouldbedisplayed.
TMOUTIfsettoavaluegreaterthanzero,TMOUTistreatedasthedefaulttimeoutforthereadbuilt-in.
Inaninterativeshell,thevalueisinterpretedasthenumberofsecondstowaitforinputafterissuingtheprimarypromptwhentheshellisinteractive.
Bashterminatesafterthatnumberofsecondsifinputdoesnotarrive.
UIDThenumeric,realuserIDofthecurrentuser.
ChecktheBashman,infoordocpagesforextendedinformation.
Somevariablesareread-only,somearesetautomaticallyandsomelosetheirmeaningwhensettoadifferentvaluethanthedefault.
3.
2.
5.
SpecialparametersTheshelltreatsseveralparametersspecially.
Theseparametersmayonlybereferenced;assignmenttothemisnotallowed.
Table3-3.
SpecialbashvariablesBashGuideforBeginnersChapter3.
TheBashenvironment41CharacterDefinition$*Expandstothepositionalparameters,startingfromone.
Whentheexpansionoccurswithindoublequotes,itexpandstoasinglewordwiththevalueofeachparameterseparatedbythefirstcharacteroftheIFSspecialvariable.
$@Expandstothepositionalparameters,startingfromone.
Whentheexpansionoccurswithindoublequotes,eachparameterexpandstoaseparateword.
$#Expandstothenumberofpositionalparametersindecimal.
$Expandstotheexitstatusofthemostrecentlyexecutedforegroundpipeline.
$-Ahyphenexpandstothecurrentoptionflagsasspecifieduponinvocation,bythesetbuilt-incommand,orthosesetbytheshellitself(suchasthe-i).
$$ExpandstotheprocessIDoftheshell.
$!
ExpandstotheprocessIDofthemostrecentlyexecutedbackground(asynchronous)command.
$0Expandstothenameoftheshellorshellscript.
$_Theunderscorevariableissetatshellstartupandcontainstheabsolutefilenameoftheshellorscriptbeingexecutedaspassedintheargumentlist.
Subsequently,itexpandstothelastargumenttothepreviouscommand,afterexpansion.
Itisalsosettothefullpathnameofeachcommandexecutedandplacedintheenvironmentexportedtothatcommand.
Whencheckingmail,thisparameterholdsthenameofthemailfile.
$*vs.
$@Theimplementationof"$*"hasalwaysbeenaproblemandrealisticallyshouldhavebeenreplacedwiththebehaviorof"$@".
Inalmosteverycasewherecodersuse"$*",theymeanCancausebugsandevensecurityholesinyoursoftware.
Thepositionalparametersarethewordsfollowingthenameofashellscript.
Theyareputintothevariables$1,$2,$3andsoon.
Aslongasneeded,variablesareaddedtoaninternalarray.
$#holdsthetotalnumberofparameters,asisdemonstratedwiththissimplescript:#!
/bin/bash#positional.
sh#Thisscriptreads3positionalparametersandprintsthemout.
POSPAR1="$1"POSPAR2="$2"POSPAR3="$3"echo"$1isthefirstpositionalparameter,\$1.
"echo"$2isthesecondpositionalparameter,\$2.
"echo"$3isthethirdpositionalparameter,\$3.
"echoecho"Thetotalnumberofpositionalparametersis$#.
"Uponexecutiononecouldgiveanynumbersofarguments:franky~>positional.
shonetwothreefourfiveoneisthefirstpositionalparameter,$1.
twoisthesecondpositionalparameter,$2.
threeisthethirdpositionalparameter,$3.
Thetotalnumberofpositionalparametersis5.
franky~>positional.
shonetwooneisthefirstpositionalparameter,$1.
BashGuideforBeginnersChapter3.
TheBashenvironment42twoisthesecondpositionalparameter,$2.
isthethirdpositionalparameter,$3.
Thetotalnumberofpositionalparametersis2.
MoreonevaluatingtheseparametersisinChapter7andSection9.
7.
Someexamplesontheotherspecialparameters:franky~>grepdictionary/usr/share/dict/wordsdictionaryfranky~>echo$_/usr/share/dict/wordsfranky~>echo$$10662franky~>mozilla&[1]11064franky~>echo$!
11064franky~>echo$0bashfranky~>echo$0franky~>lsdoesnotexistls:doesnotexist:Nosuchfileordirectoryfranky~>echo$1franky~>Userfrankystartsenteringthegrepcommand,whichresultsintheassignmentofthe_variable.
TheprocessIDofhisshellis10662.
Afterputtingajobinthebackground,the!
holdstheprocessIDofthebackgroundedjob.
Theshellrunningisbash.
Whenamistakeismade,holdsanexitcodedifferentfrom0(zero).
3.
2.
6.
ScriptrecyclingwithvariablesApartfrommakingthescriptmorereadable,variableswillalsoenableyoutofasterapplyascriptinanotherenvironmentorforanotherpurpose.
Considerthefollowingexample,averysimplescriptthatmakesabackupoffranky'shomedirectorytoaremoteserver:#!
/bin/bash#Thisscriptmakesabackupofmyhomedirectory.
cd/home#Thiscreatesthearchivetarcf/var/tmp/home_franky.
tarfranky>/dev/null2>&1#Firstremovetheoldbzip2file.
Redirecterrorsbecausethisgeneratessomeifthearchive#doesnotexist.
Thencreateanewcompressedfile.
rm/var/tmp/home_franky.
tar.
bz22>/dev/nullBashGuideforBeginnersChapter3.
TheBashenvironment43bzip2/var/tmp/home_franky.
tar#Copythefiletoanotherhost-wehavesshkeysformakingthisworkwithoutintervention.
scp/var/tmp/home_franky.
tar.
bz2bordeaux:/opt/backup/franky>/dev/null2>&1#Createatimestampinalogfile.
date>>/home/franky/log/home_backup.
logechobackupsucceeded>>/home/franky/log/home_backup.
logFirstofall,youaremorelikelytomakeerrorsifyounamefilesanddirectoriesmanuallyeachtimeyouneedthem.
Secondly,supposefrankywantstogivethisscripttocarol,thencarolwillhavetodoquitesomeeditingbeforeshecanusethescripttobackupherhomedirectory.
Thesameistrueiffrankywantstousethisscriptforbackingupotherdirectories.
Foreasyrecycling,makeallfiles,directories,usernames,servernamesetceteravariable.
Thus,youonlyneedtoeditavalueonce,withouthavingtogothroughtheentirescripttocheckwhereaparameteroccurs.
Thisisanexample:#!
/bin/bash#Thisscriptmakesabackupofmyhomedirectory.
#Changethevaluesofthevariablestomakethescriptworkforyou:BACKUPDIR=/homeBACKUPFILES=frankyTARFILE=/var/tmp/home_franky.
tarBZIPFILE=/var/tmp/home_franky.
tar.
bz2SERVER=bordeauxREMOTEDIR=/opt/backup/frankyLOGFILE=/home/franky/log/home_backup.
logcd$BACKUPDIR#Thiscreatesthearchivetarcf$TARFILE$BACKUPFILES>/dev/null2>&1#Firstremovetheoldbzip2file.
Redirecterrorsbecausethisgeneratessomeifthearchive#doesnotexist.
Thencreateanewcompressedfile.
rm$BZIPFILE2>/dev/nullbzip2$TARFILE#Copythefiletoanotherhost-wehavesshkeysformakingthisworkwithoutintervention.
scp$BZIPFILE$SERVER:$REMOTEDIR>/dev/null2>&1#Createatimestampinalogfile.
date>>$LOGFILEechobackupsucceeded>>$LOGFILELargedirectoriesandlowbandwidthTheaboveispurelyanexamplethateverybodycanunderstand,usingasmalldirectoryandahostonthesamesubnet.
Dependingonyourbandwidth,thesizeofthedirectoryandthelocationoftheremoteserver,itcantakeanawfullotoftimetomakebackupsusingthismechanism.
Forlargerdirectoriesandlowerbandwidth,usersynctokeepthedirectoriesatbothendssynchronized.
3.
3.
QuotingcharactersBashGuideforBeginnersChapter3.
TheBashenvironment443.
3.
1.
WhyAlotofkeyshavespecialmeaningsinsomecontextorother.
Quotingisusedtoremovethespecialmeaningofcharactersorwords:quotescandisablespecialtreatmentforspecialcharacters,theycanpreventreservedwordsfrombeingrecognizedassuchandtheycandisableparameterexpansion.
3.
3.
2.
EscapecharactersEscapecharactersareusedtoremovethespecialmeaningfromasinglecharacter.
Anon-quotedbackslash,\,isusedasanescapecharacterinBash.
Itpreservestheliteralvalueofthenextcharacterthatfollows,withtheexceptionofnewline.
Ifanewlinecharacterappearsimmediatelyafterthebackslash,itmarksthecontinuationofalinewhenitislongerthatthewidthoftheterminal;thebackslashisremovedfromtheinputstreamandeffectivelyignored.
franky~>date=20021226franky~>echo$date20021226franky~>echo\$date$dateInthisexample,thevariabledateiscreatedandsettoholdavalue.
Thefirstechodisplaysthevalueofthevariable,butforthesecond,thedollarsignisescaped.
3.
3.
3.
SinglequotesSinglequotes('')areusedtopreservetheliteralvalueofeachcharacterenclosedwithinthequotes.
Asinglequotemaynotoccurbetweensinglequotes,evenwhenprecededbyabackslash.
Wecontinuewiththepreviousexample:franky~>echo'$date'$date3.
3.
4.
DoublequotesUsingdoublequotestheliteralvalueofallcharactersenclosedispreserved,exceptforthedollarsign,thebackticks(backwardsinglequotes,``)andthebackslash.
Thedollarsignandthebackticksretaintheirspecialmeaningwithinthedoublequotes.
Thebackslashretainsitsmeaningonlywhenfollowedbydollar,backtick,doublequote,backslashornewline.
Withindoublequotes,thebackslashesareremovedfromtheinputstreamwhenfollowedbyoneofthesecharacters.
Backslashesprecedingcharactersthatdon'thaveaspecialmeaningareleftunmodifiedforprocessingbytheshellinterpreter.
Adoublequotemaybequotedwithindoublequotesbyprecedingitwithabackslash.
franky~>echo"$date"20021226BashGuideforBeginnersChapter3.
TheBashenvironment45franky~>echo"`date`"SunApr2011:22:06CEST2003franky~>echo"I'dsay:\"Goforit!
\""I'dsay:"Goforit!
"franky~>echo"\"Moreinput>"franky~>echo"\\"\3.
3.
5.
ANSI-CquotingWordsintheform"$'STRING'"aretreatedinaspecialway.
Thewordexpandstoastring,withbackslash-escapedcharactersreplacedasspecifiedbytheANSI-Cstandard.
BackslashescapesequencescanbefoundintheBashdocumentation.
3.
3.
6.
LocalesAdouble-quotedstringprecededbyadollarsignwillcausethestringtobetranslatedaccordingtothecurrentlocale.
Ifthecurrentlocaleis"C"or"POSIX",thedollarsignisignored.
Ifthestringistranslatedandreplaced,thereplacementisdouble-quoted.
3.
4.
Shellexpansion3.
4.
1.
GeneralAfterthecommandhasbeensplitintotokens(seeSection1.
4.
1.
1),thesetokensorwordsareexpandedorresolved.
Thereareeightkindsofexpansionperformed,whichwewilldiscussinthenextsections,intheorderthattheyareexpanded.
Afterallexpansions,quoteremovalisperformed.
3.
4.
2.
BraceexpansionBraceexpansionisamechanismbywhicharbitrarystringsmaybegenerated.
Patternstobebrace-expandedtaketheformofanoptionalPREAMBLE,followedbyaseriesofcomma-separatedstringsbetweenapairofbraces,followedbyanoptionalPOSTSCRIPT.
Thepreambleisprefixedtoeachstringcontainedwithinthebraces,andthepostscriptisthenappendedtoeachresultingstring,expandinglefttoright.
Braceexpansionsmaybenested.
Theresultsofeachexpandedstringarenotsorted;lefttorightorderispreserved:franky~>echosp{el,il,al}lspellspillspallBraceexpansionisperformedbeforeanyotherexpansions,andanycharactersspecialtootherexpansionsarepreservedintheresult.
Itisstrictlytextual.
Bashdoesnotapplyanysyntacticinterpretationtothecontextoftheexpansionorthetextbetweenthebraces.
Toavoidconflictswithparameterexpansion,thestring"${"isnotconsideredeligibleforbraceexpansion.
BashGuideforBeginnersChapter3.
TheBashenvironment46Acorrectly-formedbraceexpansionmustcontainunquotedopeningandclosingbraces,andatleastoneunquotedcomma.
Anyincorrectlyformedbraceexpansionisleftunchanged.
3.
4.
3.
TildeexpansionIfawordbeginswithanunquotedtildecharacter("~"),allofthecharactersuptothefirstunquotedslash(orallcharacters,ifthereisnounquotedslash)areconsideredatilde-prefix.
Ifnoneofthecharactersinthetilde-prefixarequoted,thecharactersinthetilde-prefixfollowingthetildearetreatedasapossibleloginname.
Ifthisloginnameisthenullstring,thetildeisreplacedwiththevalueoftheHOMEshellvariable.
IfHOMEisunset,thehomedirectoryoftheuserexecutingtheshellissubstitutedinstead.
Otherwise,thetilde-prefixisreplacedwiththehomedirectoryassociatedwiththespecifiedloginname.
Ifthetilde-prefixis"~+",thevalueoftheshellvariablePWDreplacesthetilde-prefix.
Ifthetilde-prefixis"~-",thevalueoftheshellvariableOLDPWD,ifitisset,issubstituted.
Ifthecharactersfollowingthetildeinthetilde-prefixconsistofanumberN,optionallyprefixedbya"+"ora"-",thetilde-prefixisreplacedwiththecorrespondingelementfromthedirectorystack,asitwouldbedisplayedbythedirsbuilt-ininvokedwiththecharactersfollowingtildeinthetilde-prefixasanargument.
Ifthetilde-prefix,withoutthetilde,consistsofanumberwithoutaleading"+"orisassumed.
Iftheloginnameisinvalid,orthetildeexpansionfails,thewordisleftunchanged.
Eachvariableassignmentischeckedforunquotedtilde-prefixesimmediatelyfollowinga":"or"=".
Inthesecases,tildeexpansionisalsoperformed.
Consequently,onemayusefilenameswithtildesinassignmentstoPATH,MAILPATH,andCDPATH,andtheshellassignstheexpandedvalue.
Example:franky~>exportPATH="$PATH:~/testdir"~/testdirwillbeexpandedto$HOME/testdir,soif$HOMEis/var/home/franky,thedirectory/var/home/franky/testdirwillbeaddedtothecontentofthePATHvariable.
3.
4.
4.
ShellparameterandvariableexpansionThe"$"characterintroducesparameterexpansion,commandsubstitution,orarithmeticexpansion.
Theparameternameorsymboltobeexpandedmaybeenclosedinbraces,whichareoptionalbutservetoprotectthevariabletobeexpandedfromcharactersimmediatelyfollowingitwhichcouldbeinterpretedaspartofthename.
Whenbracesareused,thematchingendingbraceisthefirst"}"notescapedbyabackslashorwithinaquotedstring,andnotwithinanembeddedarithmeticexpansion,commandsubstitution,orparameterexpansion.
Thebasicformofparameterexpansionis"${PARAMETER}".
Thevalueof"PARAMETER"issubstituted.
Thebracesarerequiredwhen"PARAMETER"isapositionalparameterwithmorethanonedigit,orwhen"PARAMETER"isfollowedbyacharacterthatisnottobeinterpretedaspartofitsname.
Ifthefirstcharacterof"PARAMETER"isanexclamationpoint,Bashusesthevalueofthevariableformedfromtherestof"PARAMETER"asthenameofthevariable;thisvariableisthenexpandedandthatvalueisusedintherestofthesubstitution,ratherthanthevalueof"PARAMETER"itself.
Thisisknownasindirectexpansion.
BashGuideforBeginnersChapter3.
TheBashenvironment47Youarecertainlyfamiliarwithstraightparameterexpansion,sinceithappensallthetime,eveninthesimplestofcases,suchastheoneaboveorthefollowing:franky~>echo$SHELL/bin/bashThefollowingisanexampleofindirectexpansion:franky~>echo${!
N*}NNTPPORTNNTPSERVERNPX_PLUGIN_PATHNotethatthisisnotthesameasecho$N*.
Thefollowingconstructallowsforcreationofthenamedvariableifitdoesnotyetexist:${VAR:=value}Example:franky~>echo$FRANKYfranky~>echo${FRANKY:=Franky}FrankySpecialparameters,amongothersthepositionalparameters,maynotbeassignedthisway,however.
WewillfurtherdiscusstheuseofthecurlybracesfortreatmentofvariablesinChapter10.
MoreinformationcanalsobefoundintheBashinfopages.
3.
4.
5.
CommandsubstitutionCommandsubstitutionallowstheoutputofacommandtoreplacethecommanditself.
Commandsubstitutionoccurswhenacommandisenclosedlikethis:$(command)orlikethisusingbackticks:`command`BashperformstheexpansionbyexecutingCOMMANDandreplacingthecommandsubstitutionwiththestandardoutputofthecommand,withanytrailingnewlinesdeleted.
Embeddednewlinesarenotdeleted,buttheymayberemovedduringwordsplitting.
franky~>echo`date`ThuFeb610:06:20CET2003Whentheold-stylebackquotedformofsubstitutionisused,backslashretainsitsliteralmeaningexceptwhenfollowedbyor"\".
Thefirstbackticksnotprecededbyabackslashterminatesthecommandsubstitution.
Whenusingthe"$(COMMAND)"form,allcharactersbetweentheparenthesesmakeupthecommand;nonearetreatedspecially.
Commandsubstitutionsmaybenested.
Tonestwhenusingthebackquotedform,escapetheinnerbacktickswithbackslashes.
BashGuideforBeginnersChapter3.
TheBashenvironment48Ifthesubstitutionappearswithindoublequotes,wordsplittingandfilenameexpansionarenotperformedontheresults.
3.
4.
6.
ArithmeticexpansionArithmeticexpansionallowstheevaluationofanarithmeticexpressionandthesubstitutionoftheresult.
Theformatforarithmeticexpansionis:$((EXPRESSION))Theexpressionistreatedasifitwerewithindoublequotes,butadoublequoteinsidetheparenthesesisnottreatedspecially.
Alltokensintheexpressionundergoparameterexpansion,commandsubstitution,andquoteremoval.
Arithmeticsubstitutionsmaybenested.
Evaluationofarithmeticexpressionsisdoneinfixed-widthintegerswithnocheckforoverflow-althoughdivisionbyzeroistrappedandrecognizedasanerror.
TheoperatorsareroughlythesameasintheCprogramminglanguage.
Inorderofdecreasingprecedence,thelistlookslikethis:Table3-4.
ArithmeticoperatorsOperatorMeaningVAR++andVAR--variablepost-incrementandpost-decrement++VARand--VARvariablepre-incrementandpre-decrement-and+unaryminusandplus!
and~logicalandbitwisenegation**exponentiation*,/and%multiplication,division,remainder+and-addition,subtraction>leftandrightbitwiseshiftsand>comparisonoperators==and!
=equalityandinequality&bitwiseAND^bitwiseexclusiveOR|bitwiseOR&&logicalAND||logicalORexprexpr:exprconditionalevaluationand|=assignments,separatorbetweenexpressionsShellvariablesareallowedasoperands;parameterexpansionisperformedbeforetheexpressionisevaluated.
Withinanexpression,shellvariablesmayalsobereferencedbynamewithoutusingtheparameterexpansionsyntax.
Thevalueofavariableisevaluatedasanarithmeticexpressionwhenitisreferenced.
Ashellvariableneednothaveitsintegerattributeturnedontobeusedinanexpression.
BashGuideforBeginnersChapter3.
TheBashenvironment49Constantswithaleading0(zero)areinterpretedasoctalnumbers.
Aleading"0x"or"0X"denoteshexadecimal.
Otherwise,numberstaketheform"[BASE'#']N",where"BASE"isadecimalnumberbetween2and64representingthearithmeticbase,andNisanumberinthatbase.
If"BASE'#'"isomitted,thenbase10isused.
Thedigitsgreaterthan9arerepresentedbythelowercaseletters,theuppercaseletters,"@",and"_",inthatorder.
If"BASE"islessthanorequalto36,lowercaseanduppercaselettersmaybeusedinterchangablytorepresentnumbersbetween10and35.
Operatorsareevaluatedinorderofprecedence.
Sub-expressionsinparenthesesareevaluatedfirstandmayoverridetheprecedencerulesabove.
Whereverpossible,Bashusersshouldtrytousethesyntaxwithsquarebrackets:$[EXPRESSION]However,thiswillonlycalculatetheresultofEXPRESSION,anddonotests:franky~>echo$[365*24]8760SeeSection7.
1.
2.
2,amongothers,forpracticalexamplesinscripts.
3.
4.
7.
ProcesssubstitutionProcesssubstitutionissupportedonsystemsthatsupportnamedpipes(FIFOs)orthe/dev/fdmethodofnamingopenfiles.
Ittakestheformof(LIST)TheprocessLISTisrunwithitsinputoroutputconnectedtoaFIFOorsomefilein/dev/fd.
Thenameofthisfileispassedasanargumenttothecurrentcommandastheresultoftheexpansion.
Ifthe">(LIST)"formisused,writingtothefilewillprovideinputforLIST.
Ifthe"signsandtheleftparenthesis,otherwisetheconstructwouldbeinterpretedasaredirection.
Whenavailable,processsubstitutionisperformedsimultaneouslywithparameterandvariableexpansion,commandsubstitution,andarithmeticexpansion.
MoreinformationinSection8.
2.
3.
3.
4.
8.
WordsplittingTheshellscanstheresultsofparameterexpansion,commandsubstitution,andarithmeticexpansionthatdidnotoccurwithindoublequotesforwordsplitting.
Theshelltreatseachcharacterof$IFSasadelimiter,andsplitstheresultsoftheotherexpansionsintowordsonthesecharacters.
IfIFSisunset,oritsvalueisexactly"''",thedefault,thenanysequenceofIFScharactersservestodelimitwords.
IfIFShasavalueotherthanthedefault,thensequencesBashGuideforBeginnersChapter3.
TheBashenvironment50ofthewhitespacecharacters"space"and"Tab"areignoredatthebeginningandendoftheword,aslongasthewhitespacecharacterisinthevalueofIFS(anIFSwhitespacecharacter).
AnycharacterinIFSthatisnotIFSwhitespace,alongwithanyadjacentIFwhitespacecharacters,delimitsafield.
AsequenceofIFSwhitespacecharactersisalsotreatedasadelimiter.
IfthevalueofIFSisnull,nowordsplittingoccurs.
Explicitnullarguments(""""or"''")areretained.
Unquotedimplicitnullarguments,resultingfromtheexpansionofparametersthathavenovalues,areremoved.
Ifaparameterwithnovalueisexpandedwithindoublequotes,anullargumentresultsandisretained.
ExpansionandwordsplittingIfnoexpansionoccurs,nosplittingisperformed.
3.
4.
9.
FilenameexpansionAfterwordsplitting,unlessthe-foptionhasbeenset(seeSection2.
3.
2),Bashscanseachwordforthecharactersand"[".
Ifoneofthesecharactersappears,thenthewordisregardedasaPATTERN,andreplacedwithanalphabeticallysortedlistoffilenamesmatchingthepattern.
Ifnomatchingfilenamesarefound,andtheshelloptionnullglobisdisabled,thewordisleftunchanged.
Ifthenullgloboptionisset,andnomatchesarefound,thewordisremoved.
Iftheshelloptionnocaseglobisenabled,thematchisperformedwithoutregardtothecaseofalphabeticcharacters.
Whenapatternisusedforfilenamegeneration,thecharacter".
"atthestartofafilenameorimmediatelyfollowingaslashmustbematchedexplicitly,unlesstheshelloptiondotglobisset.
Whenmatchingafilename,theslashcharactermustalwaysbematchedexplicitly.
Inothercases,the".
"characterisnottreatedspecially.
TheGLOBIGNOREshellvariablemaybeusedtorestrictthesetoffilenamesmatchingapattern.
IfGLOBIGNOREisset,eachmatchingfilenamethatalsomatchesoneofthepatternsinGLOBIGNOREisremovedfromthelistofmatches.
Thefilenames.
and.
.
arealwaysignored,evenwhenGLOBIGNOREisset.
However,settingGLOBIGNOREhastheeffectofenablingthedotglobshelloption,soallotherfilenamesbeginningwitha".
"willmatch.
Togettheoldbehaviorofignoringfilenamesbeginningwitha".
",make".
*"oneofthepatternsinGLOBIGNORE.
ThedotgloboptionisdisabledwhenGLOBIGNOREisunset.
3.
5.
Aliases3.
5.
1.
WhatarealiasesAnaliasallowsastringtobesubstitutedforawordwhenitisusedasthefirstwordofasimplecommand.
Theshellmaintainsalistofaliasesthatmaybesetandunsetwiththealiasandunaliasbuilt-incommands.
Issuethealiaswithoutoptionstodisplayalistofaliasesknowntothecurrentshell.
franky:~>aliasalias.
.
='cd.
.
'alias.
.
.
='cd.
.
/.
.
'alias.
.
.
.
='cdaliasPAGER='less-r'aliasTxterm='exportTERM=xterm'aliasXARGS='xargs-r'aliascdrecord='cdrecord-dev0,0,0-speed=8'BashGuideforBeginnersChapter3.
TheBashenvironment51aliase='vi'aliasegrep='grep-E'aliasewformat='fdformat-n/dev/fd0u1743;ewfsck'aliasfgrep='grep-F'aliasftp='ncftp-d15'aliash='history10'aliasfformat='fdformat/dev/fd0H1440'aliasj='jobs-l'aliasksane='setterm-reset'aliasls='ls-F--color=auto'aliasm='less'aliasmd='mkdir'aliasod='od-Ax-ta-txC'aliasp='pstree-p'aliasping='ping-vc1'aliassb='sshblubber'aliassl='ls'aliasss='sshoctarine'aliastar='gtar'aliastmp='cd/tmp'aliasunaliasall='unalias-a'aliasvi='eval`resize`;vi'aliasvt100='exportTERM=vt100'aliaswhich='type'aliasxt='xterm-bgblack-fgwhite&'franky~>Aliasesareusefulforspecifyingthedefaultversionofacommandthatexistsinseveralversionsonyoursystem,ortospecifydefaultoptionstoacommand.
Anotheruseforaliasesisforcorrectingincorrectspelling.
Thefirstwordofeachsimplecommand,ifunquoted,ischeckedtoseeifithasanalias.
Ifso,thatwordisreplacedbythetextofthealias.
Thealiasnameandthereplacementtextmaycontainanyvalidshellinput,includingshellmetacharacters,withtheexceptionthatthealiasnamemaynotcontain"=".
Thefirstwordofthereplacementtextistestedforaliases,butawordthatisidenticaltoanaliasbeingexpandedisnotexpandedasecondtime.
Thismeansthatonemayaliaslstols-F,forinstance,andBashwillnottrytorecursivelyexpandthereplacementtext.
Ifthelastcharacterofthealiasvalueisaspaceortabcharacter,thenthenextcommandwordfollowingthealiasisalsocheckedforaliasexpansion.
Aliasesarenotexpandedwhentheshellisnotinteractive,unlesstheexpand_aliasesoptionissetusingtheshoptshellbuilt-in.
3.
5.
2.
CreatingandremovingaliasesAliasesarecreatedusingthealiasshellbuilt-in.
Forpermanentuse,enterthealiasinoneofyourshellinitializationfiles;ifyoujustenterthealiasonthecommandline,itisonlyrecognizedwithinthecurrentshell.
franky~>aliasdh='df-h'franky~>dhFilesystemSizeUsedAvailUse%Mountedon/dev/hda71.
3G272M1018M22%//dev/hda1121M9.
4M105M9%/boot/dev/hda213G8.
7G3.
7G70%/home/dev/hda313G5.
3G7.
1G43%/optnone243M0243M0%/dev/shm/dev/hda63.
9G3.
2G572M85%/usrBashGuideforBeginnersChapter3.
TheBashenvironment52/dev/hda55.
2G4.
3G725M86%/varfranky~>unaliasdhfranky~>dhbash:dh:commandnotfoundfranky~>Bashalwaysreadsatleastonecompletelineofinputbeforeexecutinganyofthecommandsonthatline.
Aliasesareexpandedwhenacommandisread,notwhenitisexecuted.
Therefore,analiasdefinitionappearingonthesamelineasanothercommanddoesnottakeeffectuntilthenextlineofinputisread.
Thecommandsfollowingthealiasdefinitiononthatlinearenotaffectedbythenewalias.
Thisbehaviorisalsoanissuewhenfunctionsareexecuted.
Aliasesareexpandedwhenafunctiondefinitionisread,notwhenthefunctionisexecuted,becauseafunctiondefinitionisitselfacompoundcommand.
Asaconsequence,aliasesdefinedinafunctionarenotavailableuntilafterthatfunctionisexecuted.
Tobesafe,alwaysputaliasdefinitionsonaseparateline,anddonotusealiasincompoundcommands.
Aliasesarenotinheritedbychildprocesses.
Bourneshell(sh)doesnotrecognizealiases.
MoreaboutfunctionsisinChapter11.
FunctionsarefasterAliasesarelookedupafterfunctionsandthusresolvingisslower.
Whilealiasesareeasiertounderstand,shellfunctionsarepreferredoveraliasesforalmosteverypurpose.
3.
6.
MoreBashoptions3.
6.
1.
DisplayingoptionsWealreadydiscussedacoupleofBashoptionsthatareusefulfordebuggingyourscripts.
Inthissection,wewilltakeamorein-depthviewoftheBashoptions.
Usethe-ooptiontosettodisplayallshelloptions:willy:~>set-oallexportoffbraceexpandonemacsonerrexitoffhashallonhistexpandonhistoryonignoreeofoffinteractive-commentsonkeywordoffmonitoronnoclobberoffnoexecoffnogloboffnologoffnotifyoffnounsetoffonecmdoffphysicaloffBashGuideforBeginnersChapter3.
TheBashenvironment53posixoffprivilegedoffverboseoffvioffxtraceoffSeetheBashInfopages,sectionShellBuilt-inCommands->TheSetBuilt-inforadescriptionofeachoption.
Alotofoptionshaveone-charactershorthands:thextraceoption,forinstance,isequaltospecifyingset-x.
3.
6.
2.
ChangingoptionsShelloptionscaneitherbesetdifferentfromthedefaultuponcallingtheshell,orbesetduringshelloperation.
Theymayalsobeincludedintheshellresourceconfigurationfiles.
ThefollowingcommandexecutesascriptinPOSIX-compatiblemode:willy:~/scripts>bash--posixscript.
shForchangingthecurrentenvironmenttemporarily,orforuseinascript,wewouldratheruseset.
Use-(dash)forenablinganoption,+fordisabling:willy:~/test>set-onoclobberwilly:~/test>touchtestwilly:~/test>date>testbash:test:cannotoverwriteexistingfilewilly:~/test>set+onoclobberwilly:~/test>date>testTheaboveexampledemonstratesthenoclobberoption,whichpreventsexistingfilesfrombeingoverwrittenbyredirectionoperations.
Thesamegoesforone-characteroptions,forinstance-u,whichwilltreatunsetvariablesasanerrorwhenset,andexitsanon-interactiveshelluponencounteringsucherrors:willy:~>echo$VARwilly:~>set-uwilly:~>echo$VARbash:VAR:unboundvariableThisoptionisalsousefulfordetectingincorrectcontentassignmenttovariables:thesameerrorwillalsooccur,forinstance,whenassigningacharacterstringtoavariablethatwasdeclaredexplicitlyasoneholdingonlyintegervalues.
Onelastexamplefollows,demonstratingthenogloboption,whichpreventsspecialcharactersfrombeingexpanded:willy:~/testdir>set-onoglobwilly:~/testdir>touch*willy:~/testdir>ls-l*-rw-rw-r--1willywilly0Feb2713:37*BashGuideforBeginnersChapter3.
TheBashenvironment543.
7.
SummaryTheBashenvironmentcanbeconfiguredgloballyandonaperuserbasis.
Variousconfigurationfilesareusedtofine-tunethebehavioroftheshell.
Thesefilescontainshelloptions,settingsforvariables,functiondefinitionsandvariousotherbuildingblocksforcreatingourselvesacosyenvironment.
ExceptforthereservedBourneshell,Bashandspecialparameters,variablenamescanbechosenmoreorlessfreely.
Becausealotofcharactershavedoubleoreventriplemeanings,dependingontheenvironment,Bashusesasystemofquotingtotakeawayspecialmeaningfromoneormultiplecharacterswhenspecialtreatmentisnotwanted.
Bashusesvariousmethodsofexpandingcommandlineentriesinordertodeterminewhichcommandstoexecute.
3.
8.
ExercisesForthisexercise,youwillneedtoreadtheuseraddmanpages,becausewearegoingtousethe/etc/skeldirectorytoholddefaultshellconfigurationfiles,whicharecopiedtothehomedirectoryofeachnewlyaddeduser.
Firstwewilldosomegeneralexercisesonsettinganddisplayingvariables.
Create3variables,VAR1,VAR2andVAR3;initializethemtoholdthevalues"thirteen","13"and"HappyBirthday"respectively.
1.
Displaythevaluesofallthreevariables.
2.
Aretheselocalorglobalvariables3.
RemoveVAR3.
4.
Canyouseethetworemainingvariablesinanewterminalwindow5.
Edit/etc/profilesothatallusersaregreeteduponlogin(testthis).
6.
Fortherootaccount,settheprompttosomethinglike"Danger!
!
rootisdoingstuffin\w",preferablyinabrightcolorsuchasredorpinkorinreversevideomode.
7.
Makesurethatnewlycreatedusersalsogetanicepersonalizedpromptwhichinformsthemonwhichsysteminwhichdirectorytheyareworking.
Testyourchangesbyaddinganewuserandlogginginasthatuser.
8.
Writeascriptinwhichyouassigntwointegervaluestotwovariables.
Thescriptshouldcalculatethesurfaceofarectanglewhichhastheseproportions.
Itshouldbeairedwithcommentsandgenerateelegantoutput.
9.
Don'tforgettochmodyourscripts!
BashGuideforBeginnersChapter3.
TheBashenvironment55Chapter4.
RegularexpressionsInthischapterwediscuss:UsingregularexpressionsRegularexpressionmetacharactersFindingpatternsinfilesoroutputCharacterrangesandclassesinBash4.
1.
Regularexpressions4.
1.
1.
WhatareregularexpressionsAregularexpressionisapatternthatdescribesasetofstrings.
Regularexpressionsareconstructedanalogouslytoarithmeticexpressionsbyusingvariousoperatorstocombinesmallerexpressions.
Thefundamentalbuildingblocksaretheregularexpressionsthatmatchasinglecharacter.
Mostcharacters,includingalllettersanddigits,areregularexpressionsthatmatchthemselves.
Anymetacharacterwithspecialmeaningmaybequotedbyprecedingitwithabackslash.
4.
1.
2.
RegularexpressionmetacharactersAregularexpressionmaybefollowedbyoneofseveralrepetitionoperators(metacharacters):Table4-1.
RegularexpressionoperatorsOperatorEffect.
Matchesanysinglecharacter.
Theprecedingitemisoptionalandwillbematched,atmost,once.
*Theprecedingitemwillbematchedzeroormoretimes.
+Theprecedingitemwillbematchedoneormoretimes.
{N}TheprecedingitemismatchedexactlyNtimes.
{N,}TheprecedingitemismatchedNormoretimes.
{N,M}TheprecedingitemismatchedatleastNtimes,butnotmorethanMtimes.
-representstherangeifit'snotfirstorlastinalistortheendingpointofarangeinalist.
^Matchestheemptystringatthebeginningofaline;alsorepresentsthecharactersnotintherangeofalist.
$Matchestheemptystringattheendofaline.
\bMatchestheemptystringattheedgeofaword.
\BMatchestheemptystringprovidedit'snotattheedgeofaword.
\Matchtheemptystringattheendofword.
Chapter4.
Regularexpressions56Tworegularexpressionsmaybeconcatenated;theresultingregularexpressionmatchesanystringformedbyconcatenatingtwosubstringsthatrespectivelymatchtheconcatenatedsubexpressions.
Tworegularexpressionsmaybejoinedbytheinfixoperator"|";theresultingregularexpressionmatchesanystringmatchingeithersubexpression.
Repetitiontakesprecedenceoverconcatenation,whichinturntakesprecedenceoveralternation.
Awholesubexpressionmaybeenclosedinparenthesestooverridetheseprecedencerules.
4.
1.
3.
BasicversusextendedregularexpressionsInbasicregularexpressionsthemetacharactersand")"losetheirspecialmeaning;insteadusethebackslashedversionsand"\)".
Checkinyoursystemdocumentationwhethercommandsusingregularexpressionssupportextendedexpressions.
4.
2.
Examplesusinggrep4.
2.
1.
Whatisgrepgrepsearchestheinputfilesforlinescontainingamatchtoagivenpatternlist.
Whenitfindsamatchinaline,itcopiesthelinetostandardoutput(bydefault),orwhateverothersortofoutputyouhaverequestedwithoptions.
Thoughgrepexpectstodothematchingontext,ithasnolimitsoninputlinelengthotherthanavailablememory,anditcanmatcharbitrarycharacterswithinaline.
Ifthefinalbyteofaninputfileisnotanewline,grepsilentlysuppliesone.
Sincenewlineisalsoaseparatorforthelistofpatterns,thereisnowaytomatchnewlinecharactersinatext.
Someexamples:cathy~>greproot/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologincathy~>grep-nroot/etc/passwd1:root:x:0:0:root:/root:/bin/bash12:operator:x:11:0:operator:/root:/sbin/nologincathy~>grep-vbash/etc/passwd|grep-vnologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnews:x:9:13:news:/var/spool/news:mailnull:x:47:47::/var/spool/mqueue:/dev/nullxfs:x:43:43:XFontServer:/etc/X11/fs:/bin/falserpc:x:32:32:PortmapperRPCuser:/:/bin/falsenscd:x:28:28:NSCDDaemon:/:/bin/falsenamed:x:25:25:Named:/var/named:/bin/falsesquid:x:23:23::/var/spool/squid:/dev/nullldap:x:55:55:LDAPUser:/var/lib/ldap:/bin/falseapache:x:48:48:Apache:/var/www:/bin/falseBashGuideforBeginnersChapter4.
Regularexpressions57cathy~>grep-cfalse/etc/passwd7cathy~>grep-ips~/.
bash*|grep-vhistory/home/cathy/.
bashrc:PS1="\[\033[1;44m\]$USERisin\w\[\033[0m\]"Withthefirstcommand,usercathydisplaysthelinesfrom/etc/passwdcontainingthestringroot.
Thenshedisplaysthelinenumberscontainingthissearchstring.
Withthethirdcommandshecheckswhichusersarenotusingbash,butaccountswiththenologinshellarenotdisplayed.
Thenshecountsthenumberofaccountsthathave/bin/falseastheshell.
Thelastcommanddisplaysthelinesfromallthefilesinherhomedirectorystartingwith~/.
bash,excludingmatchescontainingthestringhistory,soastoexcludematchesfrom~/.
bash_historywhichmightcontainthesamestring,inupperorlowercases.
Notethatthesearchisforthestring"ps",andnotforthecommandps.
Nowlet'sseewhatelsewecandowithgrep,usingregularexpressions.
4.
2.
2.
GrepandregularexpressionsIfyouarenotonLinuxWeuseGNUgrepintheseexamples,whichsupportsextendedregularexpressions.
GNUgrepisthedefaultonLinuxsystems.
Ifyouareworkingonproprietarysystems,checkwiththe-Voptionwhichversionyouareusing.
GNUgrepcanbedownloadedfromhttp://gnu.
org/directory/.
4.
2.
2.
1.
LineandwordanchorsFromthepreviousexample,wenowexclusivelywanttodisplaylinesstartingwiththestring"root":cathy~>grep^root/etc/passwdroot:x:0:0:root:/root:/bin/bashIfwewanttoseewhichaccountshavenoshellassignedwhatsoever,wesearchforlinesendingin":":cathy~>grep:$/etc/passwdnews:x:9:13:news:/var/spool/news:TocheckthatPATHisexportedin~/.
bashrc,firstselect"export"linesandthensearchforlinesstartingwiththestring"PATH",soasnottodisplayMANPATHandotherpossiblepaths:cathy~>grepexport~/.
bashrc|grep'\matchestheendofaword.
Ifyouwanttofindastringthatisaseparateword(enclosedbyspaces),itisbetterusethe-w,asinthisexamplewherewearedisplayinginformationfortherootpartition:cathy~>grep-w//etc/fstabBashGuideforBeginnersChapter4.
Regularexpressions58LABEL=//ext3defaults11Ifthisoptionisnotused,allthelinesfromthefilesystemtablewillbedisplayed.
4.
2.
2.
2.
CharacterclassesAbracketexpressionisalistofcharactersenclosedby"["and"]".
Itmatchesanysinglecharacterinthatlist;ifthefirstcharacterofthelististhecaret,"^",thenitmatchesanycharacterNOTinthelist.
Forexample,theregularexpression"[0123456789]"matchesanysingledigit.
Withinabracketexpression,arangeexpressionconsistsoftwocharactersseparatedbyahyphen.
Itmatchesanysinglecharacterthatsortsbetweenthetwocharacters,inclusive,usingthelocale'scollatingsequenceandcharacterset.
Forexample,inthedefaultClocale,"[a-d]"isequivalentto"[abcd]".
Manylocalessortcharactersindictionaryorder,andintheselocales"[a-d]"istypicallynotequivalentto"[abcd]";itmightbeequivalentto"[aBbCcDd]",forexample.
Toobtainthetraditionalinterpretationofbracketexpressions,youcanusetheClocalebysettingtheLC_ALLenvironmentvariabletothevalue"C".
Finally,certainnamedclassesofcharactersarepredefinedwithinbracketexpressions.
Seethegrepmanorinfopagesformoreinformationaboutthesepredefinedexpressions.
cathy~>grep[yf]/etc/groupsys:x:3:root,bin,admtty:x:5:mail:x:12:mail,postfixftp:x:50:nobody:x:99:floppy:x:19:xfs:x:43:nfsnobody:x:65534:postfix:x:89:Intheexample,allthelinescontainingeithera"y"or"f"characteraredisplayed.
4.
2.
2.
3.
WildcardsUsethe".
"forasinglecharactermatch.
Ifyouwanttogetalistofallfive-characterEnglishdictionarywordsstartingwith"c"andendingin"h"(handyforsolvingcrosswords):cathy~>grep'\'/usr/share/dict/wordscatchclashclothcoachcouchcoughcrashcrushIfyouwanttodisplaylinescontainingtheliteraldotcharacter,usethe-Foptiontogrep.
Formatchingmultiplecharacters,usetheasterisk.
Thisexampleselectsallwordsstartingwith"c"andendingin"h"fromthesystem'sdictionary:cathy~>grep'\'/usr/share/dict/wordscaliphcashcatchBashGuideforBeginnersChapter4.
Regularexpressions59cheeseclothcheetah--outputomitted--Ifyouwanttofindtheliteralasteriskcharacterinafileoroutput,usesinglequotes.
Cathyintheexamplebelowfirsttriesfindingtheasteriskcharacterin/etc/profilewithoutusingquotes,whichdoesnotreturnanylines.
Usingquotes,outputisgenerated:cathy~>grep*/etc/profilecathy~>grep'*'/etc/profileforiin/etc/profile.
d/*.
sh;do4.
3.
PatternmatchingusingBashfeatures4.
3.
1.
CharacterrangesApartfromgrepandregularexpressions,there'sagooddealofpatternmatchingthatyoucandodirectlyintheshell,withouthavingtouseanexternalprogram.
Asyoualreadyknow,theasterisk(*)andthequestionmark()matchanystringoranysinglecharacter,respectively.
Quotethesespecialcharacterstomatchthemliterally:cathy~>touch"*"cathy~>ls"*"*Butyoucanalsousethesquarebracestomatchanyenclosedcharacterorrangeofcharacters,ifpairsofcharactersareseparatedbyahyphen.
Anexample:cathy~>ls-ld[a-cx-z]*drwxr-xr-x2cathycathy4096Jul202002app-defaults/drwxrwxr-x4cathycathy4096May252002arabic/drwxrwxr-x2cathycathy4096Mar418:30bin/drwxr-xr-x7cathycathy4096Sep22001crossover/drwxrwxr-x3cathycathy4096Mar222002xml/Thislistsallfilesincathy'shomedirectory,startingwith"a","b","c","x","y"or"z".
Ifthefirstcharacterwithinthebracesis"!
"or"^",anycharacternotenclosedwillbematched.
Tomatchthedash("-"),includeitasthefirstorlastcharacterintheset.
ThesortingdependsonthecurrentlocaleandofthevalueoftheLC_COLLATEvariable,ifitisset.
Mindthatotherlocalesmightinterpret"[a-cx-z]"as"[aBbCcXxYyZz]"ifsortingisdoneindictionaryorder.
Ifyouwanttobesuretohavethetraditionalinterpretationofranges,forcethisbehaviorbysettingLC_COLLATEorLC_ALLto"C".
4.
3.
2.
CharacterclassesCharacterclassescanbespecifiedwithinthesquarebraces,usingthesyntax[:CLASS:],whereCLASSisdefinedinthePOSIXstandardandhasoneofthevalues"alnum","alpha","ascii","blank","cntrl","digit","graph","lower","print","punct","space","upper","word"or"xdigit".
Someexamples:BashGuideforBeginnersChapter4.
Regularexpressions60cathy~>ls-ld[[:digit:]]*drwxrwxr-x2cathycathy4096Apr2013:452/cathy~>ls-ld[[:upper:]]*drwxrwxr--3cathycathy4096Sep302001Nautilus/drwxrwxr-x4cathycathy4096Jul112002OpenOffice.
org1.
0/-rw-rw-r--1cathycathy997376Apr1815:39Schedule.
sdcWhentheextglobshelloptionisenabled(usingtheshoptbuilt-in),severalextendedpatternmatchingoperatorsarerecognized.
ReadmoreintheBashinfopages,sectionBasicshellfeatures->ShellExpansions->FilenameExpansion->PatternMatching.
4.
4.
SummaryRegularexpressionsarepowerfultoolsforselectingparticularlinesfromfilesoroutput.
AlotofUNIXcommandsuseregularexpressions:vim,perl,thePostgreSQLdatabaseandsoon.
Theycanbemadeavailableinanylanguageorapplicationusingexternallibraries,andtheyevenfoundtheirwaytonon-UNIXsystems.
Forinstance,regularexpressionsareusedintheExcellspreadsheetthatcomeswiththeMicroSoftWindowsOfficesuite.
Inthischapterwegotthefeelofthegrepcommand,whichisindispensableinanyUNIXenvironment.
Thegrepcommandcandomuchmorethanthefewtaskswediscussedhere;weonlyuseditasanexampleforregularexpressions.
TheGNUgrepversioncomeswithplentyofdocumentation,whichyouarestronglyadvisedtoread!
Bashhasbuilt-infeaturesformatchingpatternsandcanrecognizecharacterclassesandranges.
4.
5.
ExercisesTheseexerciseswillhelpyoumasterregularexpressions.
DisplayalistofalltheusersonyoursystemwhologinwiththeBashshellasadefault.
1.
Fromthe/etc/groupdirectory,displayalllinesstartingwiththestring"daemon".
2.
Printallthelinesfromthesamefilethatdon'tcontainthestring.
3.
Displaylocalhostinformationfromthe/etc/hostsfile,displaythelinenumber(s)matchingthesearchstringandcountthenumberofoccurrencesofthestring.
4.
Displayalistof/usr/share/docsubdirectoriescontaininginformationaboutshells.
5.
HowmanyREADMEfilesdothesesubdirectoriescontainDon'tcountanythingintheformof"README.
a_string".
6.
Makealistoffilesinyourhomedirectorythatwerechangedlessthat10hoursago,usinggrep,butleaveoutdirectories.
7.
Putthesecommandsinashellscriptthatwillgeneratecomprehensibleoutput.
8.
Canyoufindanalternativeforwc-l,usinggrep9.
Usingthefilesystemtable(/etc/fstabforinstance),listlocaldiskdevices.
10.
Makeascriptthatcheckswhetherauserexistsin/etc/passwd.
Fornow,youcanspecifytheusernameinthescript,youdon'thavetoworkwithargumentsandconditionalsatthisstage.
11.
Displayconfigurationfilesin/etcthatcontainnumbersintheirnames.
12.
BashGuideforBeginnersChapter4.
Regularexpressions61Chapter5.
TheGNUsedstreameditorAttheendofthischapteryouwillknowaboutthefollowingtopics:WhatissedInteractiveuseofsedRegularexpressionsandstreameditingUsingsedcommandsinscriptsThisisanintroductionTheseexplanationsarefarfromcompleteandcertainlynotmeanttobeusedasthedefiniteusermanualforsed.
Thischapterisonlyincludedinordertoshowsomemoreinterestingtopicsinthenextchapters,andbecauseeverypowerusershouldhaveabasicknowledgeofthingsthatcanbedonewiththiseditor.
Fordetailedinformation,refertothesedinfoandmanpages.
5.
1.
Introduction5.
1.
1.
WhatissedAStreamEDitorisusedtoperformbasictransformationsontextreadfromafileorapipe.
Theresultissenttostandardoutput.
Thesyntaxforthesedcommandhasnooutputfilespecification,butresultscanbesavedtoafileusingoutputredirection.
Theeditordoesnotmodifytheoriginalinput.
Whatdistinguishessedfromothereditors,suchasvianded,isitsabilitytofiltertextthatitgetsfromapipelinefeed.
Youdonotneedtointeractwiththeeditorwhileitisrunning;thatiswhysedissometimescalledabatcheditor.
Thisfeatureallowsuseofeditingcommandsinscripts,greatlyeasingrepetitiveeditingtasks.
Whenfacingreplacementoftextinalargenumberoffiles,sedisagreathelp.
5.
1.
2.
sedcommandsThesedprogramcanperformtextpatternsubstitutionsanddeletionsusingregularexpressions,liketheonesusedwiththegrepcommand;seeSection4.
2.
Theeditingcommandsaresimilartotheonesusedinthevieditor:Table5-1.
SededitingcommandsCommandResulta\Appendtextbelowcurrentline.
c\Changetextinthecurrentlinewithnewtext.
dDeletetext.
i\Inserttextabovecurrentline.
Chapter5.
TheGNUsedstreameditor62pPrinttext.
rReadafile.
sSearchandreplacetext.
wWritetoafile.
Apartfromeditingcommands,youcangiveoptionstosed.
Anoverviewisinthetablebelow:Table5-2.
SedoptionsOptionEffect-eSCRIPTAddthecommandsinSCRIPTtothesetofcommandstoberunwhileprocessingtheinput.
-fAddthecommandscontainedinthefileSCRIPT-FILEtothesetofcommandstoberunwhileprocessingtheinput.
-nSilentmode.
-VPrintversioninformationandexit.
Thesedinfopagescontainmoreinformation;weonlylistthemostfrequentlyusedcommandsandoptionshere.
5.
2.
Interactiveediting5.
2.
1.
PrintinglinescontainingapatternThisissomethingyoucandowithgrep,ofcourse,butyoucan'tdoa"findandreplace"usingthatcommand.
Thisisjusttogetyoustarted.
Thisisourexampletextfile:sandy~>cat-nexample1Thisisthefirstlineofanexampletext.
2Itisatextwitherors.
3Lotsoferors.
4Somucherors,alltheseerorsaremakingmesick.
5Thisisalinenotcontaininganyerrors.
6Thisisthelastline.
sandy~>Wewantsedtofindallthelinescontainingoursearchpattern,inthiscase"erors".
Weusetheptoobtaintheresult:sandy~>sed'/erors/p'exampleThisisthefirstlineofanexampletext.
Itisatextwitherors.
Itisatextwitherors.
Lotsoferors.
Lotsoferors.
Somucherors,alltheseerorsaremakingmesick.
Somucherors,alltheseerorsaremakingmesick.
Thisisalinenotcontaininganyerrors.
Thisisthelastline.
BashGuideforBeginnersChapter5.
TheGNUsedstreameditor63sandy~>Asyounotice,sedprintstheentirefile,butthelinescontainingthesearchstringareprintedtwice.
Thisisnotwhatwewant.
Inordertoonlyprintthoselinesmatchingourpattern,usethe-noption:sandy~>sed-n'/erors/p'exampleItisatextwitherors.
Lotsoferors.
Somucherors,alltheseerorsaremakingmesick.
sandy~>5.
2.
2.
DeletinglinesofinputcontainingapatternWeusethesameexampletextfile.
Nowweonlywanttoseethelinesnotcontainingthesearchstring:sandy~>sed'/erors/d'exampleThisisthefirstlineofanexampletext.
Thisisalinenotcontaininganyerrors.
Thisisthelastline.
sandy~>Thedcommandresultsinexcludinglinesfrombeingdisplayed.
Matchinglinesstartingwithagivenpatternandendinginasecondpatternareshowedlikethis:sandy~>sed-n'/^This.
*errors.
$/p'exampleThisisalinenotcontaininganyerrors.
sandy~>Notethatthelastdotneedstobeescapedinordertoactuallymatch.
Inourexampletheexpressionjustmatchesanycharacter,includingthelastdot.
5.
2.
3.
RangesoflinesThistimewewanttotakeoutthelinescontainingtheerrors.
Intheexamplethesearelines2to4.
Specifythisrangetoaddress,togetherwiththedcommand:sandy~>sed'2,4d'exampleThisisthefirstlineofanexampletext.
Thisisalinenotcontaininganyerrors.
Thisisthelastline.
sandy~>Toprintthefilestartingfromacertainlineuntiltheendofthefile,useacommandsimilartothis:sandy~>sed'3,$d'exampleThisisthefirstlineofanexampletext.
Itisatextwitherors.
sandy~>Thisonlyprintsthefirsttwolinesoftheexamplefile.
Thefollowingcommandprintsthefirstlinecontainingthepattern"atext",uptoandincludingthenextlinecontainingthepattern"aline":BashGuideforBeginnersChapter5.
TheGNUsedstreameditor64sandy~>sed-n'/atext/,/This/p'exampleItisatextwitherors.
Lotsoferors.
Somucherors,alltheseerorsaremakingmesick.
Thisisalinenotcontaininganyerrors.
sandy~>5.
2.
4.
FindandreplacewithsedIntheexamplefile,wewillnowsearchandreplacetheerrorsinsteadofonly(de)selectingthelinescontainingthesearchstring.
sandy~>sed's/erors/errors/'exampleThisisthefirstlineofanexampletext.
Itisatextwitherrors.
Lotsoferrors.
Somucherrors,alltheseerorsaremakingmesick.
Thisisalinenotcontaininganyerrors.
Thisisthelastline.
sandy~>Asyoucansee,thisisnotexactlythedesiredeffect:inline4,onlythefirstoccurrenceofthesearchstringhasbeenreplaced,andthereisstillan'eror'left.
Usethegcommandtoindicatetosedthatitshouldexaminetheentirelineinsteadofstoppingatthefirstoccurrenceofyourstring:sandy~>sed's/erors/errors/g'exampleThisisthefirstlineofanexampletext.
Itisatextwitherrors.
Lotsoferrors.
Somucherrors,alltheseerrorsaremakingmesick.
Thisisalinenotcontaininganyerrors.
Thisisthelastline.
sandy~>Toinsertastringatthebeginningofeachlineofafile,forinstanceforquoting:sandy~>sed's/^/>/'example>Thisisthefirstlineofanexampletext.
>Itisatextwitherors.
>Lotsoferors.
>Somucherors,alltheseerorsaremakingmesick.
>Thisisalinenotcontaininganyerrors.
>Thisisthelastline.
sandy~>Insertsomestringattheendofeachline:sandy~>sed's/$/EOL/'exampleThisisthefirstlineofanexampletext.
EOLItisatextwitherors.
EOLLotsoferors.
EOLSomucherors,alltheseerorsaremakingmesick.
EOLThisisalinenotcontaininganyerrors.
EOLThisisthelastline.
EOLsandy~>BashGuideforBeginnersChapter5.
TheGNUsedstreameditor65Multiplefindandreplacecommandsareseparatedwithindividual-eoptions:sandy~>sed-e's/erors/errors/g'-e's/last/final/g'exampleThisisthefirstlineofanexampletext.
Itisatextwitherrors.
Lotsoferrors.
Somucherrors,alltheseerrorsaremakingmesick.
Thisisalinenotcontaininganyerrors.
Thisisthefinalline.
sandy~>Keepinmindthatbydefaultsedprintsitsresultstothestandardoutput,mostlikelyyourterminalwindow.
Ifyouwanttosavetheoutputtoafile,redirectit:sedoption'some/expression'file_to_process>sed_output_in_a_fileMoreexamplesPlentyofsedexamplescanbefoundinthestartupscriptsforyourmachine,whichareusuallyin/etc/init.
dor/etc/rc.
d/init.
d.
Changeintothedirectorycontainingtheinitscriptsonyoursystemandissuethefollowingcommand:grepsed*5.
3.
Non-interactiveediting5.
3.
1.
ReadingsedcommandsfromafileMultiplesedcommandscanbeputinafileandexecutedusingthe-foption.
Whencreatingsuchafile,makesurethat:Notrailingwhitespacesexistattheendoflines.
Noquotesareused.
Whenenteringtexttoaddorreplace,allexceptthelastlineendinabackslash.
5.
3.
2.
WritingoutputfilesWritingoutputisdoneusingtheoutputredirectionoperator>.
ThisisanexamplescriptusedtocreateverysimpleHTMLfilesfromplaintextfiles.
sandy~>catscript.
sed1i\\sedgeneratedhtml\\$a\\\sandy~>cattxt2html.
sh#!
/bin/bashBashGuideforBeginnersChapter5.
TheGNUsedstreameditor66#ThisisasimplescriptthatyoucanuseforconvertingtextintoHTML.
#Firstwetakeoutallnewlinecharacters,sothattheappendingonlyhappens#once,thenwereplacethenewlines.
echo"converting$1.
.
.
"SCRIPT="/home/sandy/scripts/script.
sed"NAME="$1"TEMPFILE="/var/tmp/sed.
$PID.
tmp"sed"s/\n/^M/"$1|sed-f$SCRIPT|sed"s/^M/\n/">$TEMPFILEmv$TEMPFILE$NAMEecho"done.
"sandy~>$1holdsthefirstargumenttoagivencommand,inthiscasethenameofthefiletoconvert:sandy~>cattestline1line2line3MoreonpositionalparametersinChapter7.
sandy~>txt2html.
shtestconvertingtest.
.
.
done.
sandy~>cattestsedgeneratedhtmlline1line2line3sandy~>Thisisnotreallyhowitisdone;thisexamplejustdemonstratessedcapabilities.
SeeSection6.
3foramoredecentsolutiontothisproblem,usingawkBEGINandENDconstructs.
EasysedAdvancededitors,supportingsyntaxhighlighting,canrecognizesedsyntax.
Thiscanbeagreathelpifyoutendtoforgetbackslashesandsuch.
5.
4.
SummaryThesedstreameditorisapowerfulcommandlinetool,whichcanhandlestreamsofdata:itcantakeinputlinesfromapipe.
Thismakesitfitfornon-interactiveuse.
Thesededitorusesvi-likecommandsandacceptsregularexpressions.
Thesedtoolcanreadcommandsfromthecommandlineorfromascript.
ItisoftenusedtoperformBashGuideforBeginnersChapter5.
TheGNUsedstreameditor67find-and-replaceactionsonlinescontainingapattern.
5.
5.
ExercisesTheseexercisesaremeanttofurtherdemonstratewhatsedcando.
Printalistoffilesinyourscriptsdirectory,endingin".
sh".
Mindthatyoumighthavetounaliasls.
Puttheresultinatemporaryfile.
1.
Makealistoffilesin/usr/binthathavetheletter"a"asthesecondcharacter.
Puttheresultinatemporaryfile.
2.
Deletethefirst3linesofeachtemporaryfile.
3.
Printtostandardoutputonlythelinescontainingthepattern"an".
4.
Createafileholdingsedcommandstoperformtheprevioustwotasks.
Addanextracommandtothisfilethataddsastringlike"***Thismighthavesomethingtodowithmanandmanpages***"inthelineprecedingeveryoccurenceofthestring"man".
Checktheresults.
5.
Alonglistingoftherootdirectory,/,isusedforinput.
Createafileholdingsedcommandsthatcheckforsymboliclinksandplainfiles.
Ifafileisasymboliclink,precedeitwithalinelike"--Thisisasymlink--".
Ifthefileisaplainfile,addastringonthesameline,addingacommentlike"ls-l|awk'{print$5$9}'160origBashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage70121script.
sed120temp_file126test120twolines441txt2html.
shkelly@octarine~/test>Thiscommandprintedthefifthcolumnofalongfilelisting,whichcontainsthefilesize,andthelastcolumn,thenameofthefile.
Thisoutputisnotveryreadableunlessyouusetheofficialwayofreferringtocolumns,whichistoseparatetheonesthatyouwanttoprintwithacomma.
Inthatcase,thedefaultoutputseparatercharacter,usuallyaspace,willbeputinbetweeneachoutputfield.
LocalconfigurationNotethattheconfigurationoftheoutputofthels-lcommandmightbedifferentonyoursystem.
Displayoftimeanddateisdependentonyourlocalesetting.
6.
2.
2.
FormattingfieldsWithoutformatting,usingonlytheoutputseparator,theoutputlooksratherpoor.
Insertingacoupleoftabsandastringtoindicatewhatoutputthisiswillmakeitlookalotbetter:kelly@octarine~/test>ls-ldh*|grep-vtotal|\awk'{print"Sizeis"$5"bytesfor"$9}'Sizeis160bytesfororigSizeis121bytesforscript.
sedSizeis120bytesfortemp_fileSizeis126bytesfortestSizeis120bytesfortwolinesSizeis441bytesfortxt2html.
shkelly@octarine~/test>Notetheuseofthebackslash,whichmakeslonginputcontinueonthenextlinewithouttheshellinterpretingthisasaseparatecommand.
Whileyourcommandlineinputcanbeofvirtuallyunlimitedlength,yourmonitorisnot,andprintedpapercertainlyisn't.
Usingthebackslashalsoallowsforcopyingandpastingoftheabovelinesintoaterminalwindow.
The-hoptiontolsisusedforsupplyinghumanlyreadablesizeformatsforbiggerfiles.
Theoutputofalonglistingdisplayingthetotalamountofblocksinthedirectoryisgivenwhenadirectoryistheargument.
Thislineisuselesstous,soweaddanasterisk.
Wealsoaddthe-doptionforthesamereason,incaseasteriskexpandstoadirectory.
Thebackslashinthisexamplemarksthecontinuationofaline.
SeeSection3.
3.
2.
Youcantakeoutanynumberofcolumnsandevenreversetheorder.
Intheexamplebelowthisisdemonstratedforshowingthemostcriticalpartitions:kelly@octarine~>df-h|sort-rnk5|head-3|\awk'{print"Partition"$6"\t:"$5"full!
"}'Partition/var:86%full!
Partition/usr:85%full!
Partition/home:70%full!
kelly@octarine~>BashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage71Thetablebelowgivesanoverviewofspecialformattingcharacters:Table6-1.
FormattingcharactersforgawkSequenceMeaning\aBellcharacter\nNewlinecharacter\tTabQuotes,dollarsignsandothermeta-charactersshouldbeescapedwithabackslash.
6.
2.
3.
TheprintcommandandregularexpressionsAregularexpressioncanbeusedasapatternbyenclosingitinslashes.
Theregularexpressionisthentestedagainsttheentiretextofeachrecord.
Thesyntaxisasfollows:awk'EXPRESSION{PROGRAM}'file(s)Thefollowingexampledisplaysonlylocaldiskdeviceinformation,networkedfilesystemsarenotshown:kellyisin~>df-h|awk'/dev\/hd/{print$6"\t:"$5}'/:46%/boot:10%/opt:84%/usr:97%/var:73%/.
vol1:8%kellyisin~>Slashesneedtobeescaped,becausetheyhaveaspecialmeaningtotheawkprogram.
Belowanotherexamplewherewesearchthe/etcdirectoryforfilesendingin".
conf"andstartingwitheither"a"or"x",usingextendedregularexpressions:kellyisin/etc>ls-l|awk'/\Thisexampleillustratesthespecialmeaningofthedotinregularexpressions:thefirstoneindicatesthatwewanttosearchforanycharacterafterthefirstsearchstring,thesecondisescapedbecauseitispartofastringtofind(theendofthefilename).
6.
2.
4.
SpecialpatternsInordertoprecedeoutputwithcomments,usetheBEGINstatement:kellyisin/etc>ls-l|\awk'BEGIN{print"Filesfound:\n"}/\TheENDstatementcanbeaddedforinsertingtextaftertheentireinputisprocessed:kellyisin/etc>ls-l|\awk'/\6.
2.
5.
GawkscriptsAscommandstendtogetalittlelonger,youmightwanttoputtheminascript,sotheyarereusable.
Anawkscriptcontainsawkstatementsdefiningpatternsandactions.
Asanillustration,wewillbuildareportthatdisplaysourmostloadedpartitions.
SeeSection6.
2.
2.
kellyisin~>catdiskrep.
awkBEGIN{print"***WARNINGWARNINGWARNING***"}/\df-h|awk-fdiskrep.
awk***WARNINGWARNINGWARNING***Partition/usr:97%full!
***GivemoneyfornewdisksURGENTLY!
***kellyisin~>awkfirstprintsabeginmessage,thenformatsallthelinesthatcontainaneightoranineatthebeginningofaword,followedbyoneothernumberandapercentagesign.
Anendmessageisadded.
SyntaxhighlightingAwkisaprogramminglanguage.
Itssyntaxisrecognizedbymosteditorsthatcandosyntaxhighlightingforotherlanguages,suchasC,Bash,HTML,etc.
6.
3.
GawkvariablesAsawkisprocessingtheinputfile,itusesseveralvariables.
Someareeditable,someareread-only.
6.
3.
1.
TheinputfieldseparatorThefieldseparator,whichiseitherasinglecharacteroraregularexpression,controlsthewayawksplitsupaninputrecordintofields.
Theinputrecordisscannedforcharactersequencesthatmatchtheseparatordefinition;thefieldsthemselvesarethetextbetweenthematches.
BashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage73Thefieldseparatorisrepresentedbythebuilt-invariableFS.
NotethatthisissomethingdifferentfromtheIFSvariableusedbyPOSIX-compliantshells.
Thevalueofthefieldseparatorvariablecanbechangedintheawkprogramwiththeassignmentoperator=.
Oftentherighttimetodothisisatthebeginningofexecutionbeforeanyinputhasbeenprocessed,sothattheveryfirstrecordisreadwiththeproperseparator.
Todothis,usethespecialBEGINpattern.
Intheexamplebelow,webuildacommandthatdisplaysalltheusersonyoursystemwithadescription:kellyisin~>awk'BEGIN{FS=print$1"\t"$5}'/etc/passwd--outputomitted--kellyKellySmithfrankyFrankyB.
eddyEddyWhitewillyWilliamBlackcathyCatherinetheGreatsandySandyLiWongkellyisin~>Inanawkscript,itwouldlooklikethis:kellyisin~>catprintnames.
awkBEGIN{FS=":"}{print$1"\t"$5}kellyisin~>awk-fprintnames.
awk/etc/passwd--outputomitted--Chooseinputfieldseparatorscarefullytopreventproblems.
Anexampletoillustratethis:sayyougetinputintheformoflinesthatlooklikethis:"SandyL.
Wong,64ZooSt.
,Antwerp,2000X"Youwriteacommandlineorascript,whichprintsoutthenameofthepersoninthatrecord:awk'BEGIN{FS=print$1,$2,$3}'inputfileButapersonmighthaveaPhD,anditmightbewrittenlikethis:"SandyL.
Wong,PhD,64ZooSt.
,Antwerp,2000X"Yourawkwillgivethewrongoutputforthisline.
Ifneeded,useanextraawkorsedtouniformdatainputformats.
Thedefaultinputfieldseparatorisoneormorewhitespacesortabs.
6.
3.
2.
Theoutputseparators6.
3.
2.
1.
TheoutputfieldseparatorFieldsarenormallyseparatedbyspacesintheoutput.
Thisbecomesapparentwhenyouusethecorrectsyntaxfortheprintcommand,whereargumentsareseparatedbycommas:kelly@octarine~/test>cattestBashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage74record1data1record2data2kelly@octarine~/test>awk'{print$1$2}'testrecord1data1record2data2kelly@octarine~/test>awk'{print$1,$2}'testrecord1data1record2data2kelly@octarine~/test>Ifyoudon'tputinthecommas,printwilltreattheitemstooutputasoneargument,thusomittingtheuseofthedefaultoutputseparator,OFS.
Anycharacterstringmaybeusedastheoutputfieldseparatorbysettingthisbuilt-invariable.
6.
3.
2.
2.
TheoutputrecordseparatorTheoutputfromanentireprintstatementiscalledanoutputrecord.
Eachprintcommandresultsinoneoutputrecord,andthenoutputsastringcalledtheoutputrecordseparator,ORS.
Thedefaultvalueforthisvariableis"\n",anewlinecharacter.
Thus,eachprintstatementgeneratesaseparateline.
Tochangethewayoutputfieldsandrecordsareseparated,assignnewvaluestoOFSandORS:kelly@octarine~/test>awk'BEGIN{OFS=";";ORS="\n-->\n"}\{print$1,$2}'testrecord1;data1-->record2;data2-->kelly@octarine~/test>IfthevalueofORSdoesnotcontainanewline,theprogram'soutputisruntogetheronasingleline.
6.
3.
3.
ThenumberofrecordsThebuilt-inNRholdsthenumberofrecordsthatareprocessed.
Itisincrementedafterreadinganewinputline.
Youcanuseitattheendtocountthetotalnumberofrecords,orineachoutputrecord:kelly@octarine~/test>catprocessed.
awkBEGIN{OFS="-";ORS="\n-->done\n"}{print"Recordnumber"NR":\t"$1,$2}END{print"Numberofrecordsprocessed:"NR}kelly@octarine~/test>awk-fprocessed.
awktestRecordnumber1:record1-data1-->doneRecordnumber2:record2-data2-->doneNumberofrecordsprocessed:2-->donekelly@octarine~/test>BashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage756.
3.
4.
UserdefinedvariablesApartfromthebuilt-invariables,youcandefineyourown.
Whenawkencountersareferencetoavariablewhichdoesnotexist(whichisnotpredefined),thevariableiscreatedandinitializedtoanullstring.
Forallsubsequentreferences,thevalueofthevariableiswhatevervaluewasassignedlast.
Variablescanbeastringoranumericvalue.
Contentofinputfieldscanalsobeassignedtovariables.
Valuescanbeassigneddirectlyusingthe=operator,oryoucanusethecurrentvalueofthevariableincombinationwithotheroperators:kelly@octarine~>catrevenues2002100920021013consultancyBigComp25002002101520021020trainingEduComp20002002111220021123appdevSmartComp100002002120420021215trainingEduComp5000kelly@octarine~>cattotal.
awk{total=total+$5}{print"Sendbillfor"$5"dollarto"$4}END{printnTotalrevenue:"total}kelly@octarine~>awk-ftotal.
awktestSendbillfor2500dollartoBigCompSendbillfor2000dollartoEduCompSendbillfor10000dollartoSmartCompSendbillfor5000dollartoEduCompTotalrevenue:19500kelly@octarine~>C-likeshorthandslikeVAR+=valuearealsoaccepted.
6.
3.
5.
MoreexamplesTheexamplefromSection5.
3.
2becomesmucheasierwhenweuseanawkscript:kelly@octarine~/html>catmake-html-from-text.
awkBEGIN{print"\nAwk-generatedHTML\n{print$0}END{print"\n\n"}Andthecommandtoexecuteisalsomuchmorestraightforwardwhenusingawkinsteadofsed:kelly@octarine~/html>awk-fmake-html-from-text.
awktestfile>file.
htmlAwkexamplesonyoursystemWereferagaintothedirectorycontainingtheinitscriptsonyoursystem.
Enteracommandsimilartothefollowingtoseemorepracticalexamplesofthewidelyspreadusageoftheawkcommand:grepawk/etc/init.
d/*BashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage766.
3.
6.
TheprintfprogramFormoreprecisecontrolovertheoutputformatthanwhatisnormallyprovidedbyprint,useprintf.
Theprintfcommandcanbeusedtospecifythefieldwidthtouseforeachitem,aswellasvariousformattingchoicesfornumbers(suchaswhatoutputbasetouse,whethertoprintanexponent,whethertoprintasign,andhowmanydigitstoprintafterthedecimalpoint).
Thisisdonebysupplyingastring,calledtheformatstring,thatcontrolshowandwheretoprinttheotherarguments.
ThesyntaxisthesameasfortheC-languageprintfstatement;seeyourCintroductionguide.
Thegawkinfopagescontainfullexplanations.
6.
4.
SummaryThegawkutilityinterpretsaspecial-purposeprogramminglanguage,handlingsimpledata-reformattingjobswithjustafewlinesofcode.
ItisthefreeversionofthegeneralUNIXawkcommand.
Thistoolsreadslinesofinputdataandcaneasilyrecognizecolumnedoutput.
Theprintprogramisthemostcommonforfilteringandformattingdefinedfields.
On-the-flyvariabledeclarationisstraightforwardandallowsforsimplecalculationofsums,statisticsandotheroperationsontheprocessedinputstream.
Variablesandcommandscanbeputinawkscriptsforbackgroundprocessing.
Otherthingsyoushouldknowaboutawk:Thelanguageremainswell-knownonUNIXandalikes,butforexecutingsimilartasks,Perlisnowmorecommonlyused.
However,awkhasamuchsteeperlearningcurve(meaningthatyoulearnalotinaveryshorttime).
Inotherwords,Perlismoredifficulttolearn.
BothPerlandawksharethereputationofbeingincomprehensible,eventotheactualauthorsoftheprogramsthatusetheselanguages.
Sodocumentyourcode!
6.
5.
ExercisesThesearesomepracticalexampleswhereawkcanbeuseful.
Forthefirstexercise,yourinputislinesinthefollowingform:Username:Firstname:Lastname:TelephonenumberMakeanawkscriptthatwillconvertsuchalinetoanLDAPrecordinthisformat:dn:uid=Username,dc=example,dc=comcn:FirstnameLastnamesn:LastnametelephoneNumber:TelephonenumberCreateafilecontainingacoupleoftestrecordsandcheck.
1.
CreateaBashscriptusingawkandstandardUNIXcommandsthatwillshowthetopthreeusersofdiskspaceinthe/homefilesystem(ifyoudon'thavethedirectoryholdingthehomesonaseparatepartition,makethescriptforthe/partition;thisispresentoneveryUNIXsystem).
First,executethecommandsfromthecommandline.
Thenputtheminascript.
Thescriptshouldcreatesensibleoutput(sensibleasinreadablebytheboss).
Ifeverythingprovestowork,havethescriptemailitsresultstoyou(useforinstancemail-sDiskspaceusageBashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage77Ifthequotadaemonisrunning,usethatinformation;ifnot,usefind.
CreateXML-styleoutputfromaTab-separatedlistinthefollowingform:MeaningverylonglinewithalotofdescriptionmeaninganotherlonglineothermeaningmorelonglinetestmeaninglooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonglinTheoutputshouldread:Meaningverylonglinemeaninglonglinemorelonglinelooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongline,butimeanreAdditionally,ifyouknowanythingaboutXML,writeaBEGINandENDscripttocompletethetable.
OrdoitinHTML.
3.
BashGuideforBeginnersChapter6.
TheGNUawkprogramminglanguage78Chapter7.
ConditionalstatementsInthischapterwewilldiscusstheuseofconditionalsinBashscripts.
Thisincludesthefollowingtopics:TheifstatementUsingtheexitstatusofacommandComparingandtestinginputandfilesif/then/elseconstructsif/then/elif/elseconstructsUsingandtestingthepositionalparametersNestedifstatementsBooleanexpressionsUsingcasestatements7.
1.
Introductiontoif7.
1.
1.
GeneralAttimesyouneedtospecifydifferentcoursesofactiontobetakeninashellscript,dependingonthesuccessorfailureofacommand.
Theifconstructionallowsyoutospecifysuchconditions.
Themostcompactsyntaxoftheifcommandis:ifTEST-COMMANDS;thenCONSEQUENT-COMMANDS;fiTheTEST-COMMANDlistisexecuted,andifitsreturnstatusiszero,theCONSEQUENT-COMMANDSlistisexecuted.
Thereturnstatusistheexitstatusofthelastcommandexecuted,orzeroifnoconditiontestedtrue.
TheTEST-COMMANDofteninvolvesnumericalorstringcomparisontests,butitcanalsobeanycommandthatreturnsastatusofzerowhenitsucceedsandsomeotherstatuswhenitfails.
Unaryexpressionsareoftenusedtoexaminethestatusofafile.
IftheFILEargumenttooneoftheprimariesisoftheform/dev/fd/N,thenfiledescriptor"N"ischecked.
stdin,stdoutandstderrandtheirrespectivefiledescriptorsmayalsobeusedfortests.
7.
1.
1.
1.
ExpressionsusedwithifThetablebelowcontainsanoverviewoftheso-called"primaries"thatmakeuptheTEST-COMMANDcommandorlistofcommands.
Theseprimariesareputbetweensquarebracketstoindicatethetestofaconditionalexpression.
Table7-1.
PrimaryexpressionsPrimaryMeaning[-aFILE]TrueifFILEexists.
Chapter7.
Conditionalstatements79[-bFILE]TrueifFILEexistsandisablock-specialfile.
[-cFILE]TrueifFILEexistsandisacharacter-specialfile.
[-dFILE]TrueifFILEexistsandisadirectory.
[-eFILE]TrueifFILEexists.
[-fFILE]TrueifFILEexistsandisaregularfile.
[-gFILE]TrueifFILEexistsanditsSGIDbitisset.
[-hFILE]TrueifFILEexistsandisasymboliclink.
[-kFILE]TrueifFILEexistsanditsstickybitisset.
[-pFILE]TrueifFILEexistsandisanamedpipe(FIFO).
[-rFILE]TrueifFILEexistsandisreadable.
[-sFILE]TrueifFILEexistsandhasasizegreaterthanzero.
[-tFD]TrueiffiledescriptorFDisopenandreferstoaterminal.
[-uFILE]TrueifFILEexistsanditsSUID(setuserID)bitisset.
[-wFILE]TrueifFILEexistsandiswritable.
[-xFILE]TrueifFILEexistsandisexecutable.
[-OFILE]TrueifFILEexistsandisownedbytheeffectiveuserID.
[-GFILE]TrueifFILEexistsandisownedbytheeffectivegroupID.
[-LFILE]TrueifFILEexistsandisasymboliclink.
[-NFILE]TrueifFILEexistsandhasbeenmodifiedsinceitwaslastread.
[-SFILE]TrueifFILEexistsandisasocket.
[FILE1-ntFILE2]TrueifFILE1hasbeenchangedmorerecentlythanFILE2,orifFILE1existsandFILE2doesnot.
[FILE1-otFILE2]TrueifFILE1isolderthanFILE2,orisFILE2existsandFILE1doesnot.
[FILE1-efFILE2]TrueifFILE1andFILE2refertothesamedeviceandinodenumbers.
[-oOPTIONNAME]Trueifshelloption"OPTIONNAME"isenabled.
[-zSTRING]Trueofthelengthif"STRING"iszero.
[-nSTRING]or[STRING]Trueifthelengthof"STRING"isnon-zero.
[STRING1==STRING2]Trueifthestringsareequal.
"="maybeusedinsteadof"=="forstrictPOSIXcompliance.
[STRING1!
=STRING2]Trueifthestringsarenotequal.
[STRING1STRING2]Trueif"STRING1"sortsafter"STRING2"lexicographicallyinthecurrentlocale.
[ARG1OPARG2]"OP"isoneof-eq,-ne,-lt,-le,-gtor-ge.
Thesearithmeticbinaryoperatorsreturntrueif"ARG1"isequalto,notequalto,lessthan,lessthanorequalto,greaterthan,orgreaterthanorequalto"ARG2",respectively.
"ARG1"and"ARG2"areintegers.
Expressionsmaybecombinedusingthefollowingoperators,listedindecreasingorderofprecedence:BashGuideforBeginnersChapter7.
Conditionalstatements80Table7-2.
CombiningexpressionsOperationEffect[!
EXPR]TrueifEXPRisfalse.
[(EXPR)]ReturnsthevalueofEXPR.
Thismaybeusedtooverridethenormalprecedenceofoperators.
[EXPR1-aEXPR2]TrueifbothEXPR1andEXPR2aretrue.
[EXPR1-oEXPR2]TrueifeitherEXPR1orEXPR2istrue.
The[(ortest)built-inevaluatesconditionalexpressionsusingasetofrulesbasedonthenumberofarguments.
MoreinformationaboutthissubjectcanbefoundintheBashdocumentation.
Justliketheifisclosedwithfi,theopeningsquarebracketshouldbeclosedaftertheconditionshavebeenlisted.
7.
1.
1.
2.
CommandsfollowingthethenstatementTheCONSEQUENT-COMMANDSlistthatfollowsthethenstatementcanbeanyvalidUNIXcommand,anyexecutableprogram,anyexecutableshellscriptoranyshellstatement,withtheexceptionoftheclosingfi.
Itisimportanttorememberthatthethenandfiareconsideredtobeseparatedstatementsintheshell.
Therefore,whenissuedonthecommandline,theyareseparatedbyasemi-colon.
Inascript,thedifferentpartsoftheifstatementareusuallywell-separated.
Below,acoupleofsimpleexamples.
7.
1.
1.
3.
CheckingfilesThefirstexamplechecksfortheexistenceofafile:anny~>catmsgcheck.
sh#!
/bin/bashecho"Thisscriptscheckstheexistenceofthemessagesfile.
"echo"Checking.
.
.
"if[-f/var/log/messages]thenecho"/var/log/messagesexists.
"fiechoecho".
.
.
done.
"anny~>.
/msgcheck.
shThisscriptscheckstheexistenceofthemessagesfile.
Checking.
.
.
/var/log/messagesexists.
.
.
.
done.
7.
1.
1.
4.
CheckingshelloptionsToaddinyourBashconfigurationfiles:#Theselineswillprintamessageifthenoclobberoptionisset:BashGuideforBeginnersChapter7.
Conditionalstatements81if[-onoclobber]thenecho"Yourfilesareprotectedagainstaccidentaloverwritingusingredirection.
"fiTheenvironmentTheaboveexamplewillworkwhenenteredonthecommandline:anny~>if[-onoclobber];thenecho;echo"yourfilesareprotectedagainstoverwriting.
";echo;fiyourfilesareprotectedagainstoverwriting.
anny~>However,ifyouusetestingofconditionsthatdependontheenvironment,youmightgetdifferentresultswhenyouenterthesamecommandinascript,becausethescriptwillopenanewshell,inwhichexpectedvariablesandoptionsmightnotbesetautomatically.
7.
1.
2.
Simpleapplicationsofif7.
1.
2.
1.
TestingexitstatusThevariableholdstheexitstatusofthepreviouslyexecutedcommand(themostrecentlycompletedforegroundprocess).
Thefollowingexampleshowsasimpletest:anny~>if[$-eq0]Moreinput>thenecho'Thatwasagoodjob!
'Moreinput>fiThatwasagoodjob!
anny~>ThefollowingexampledemonstratesthatTEST-COMMANDSmightbeanyUNIXcommandthatreturnsanexitstatus,andthatifagainreturnsanexitstatusofzero:anny~>if!
grep$USER/etc/passwdMoreinput>thenecho"youruseraccountisnotmanagedlocally";fiyouruseraccountisnotmanagedlocallyanny>echo$0anny>Thesameresultcanbeobtainedasfollows:anny>grep$USER/etc/passwdanny>if[$-ne0];thenecho"notalocalaccount";finotalocalaccountanny>BashGuideforBeginnersChapter7.
Conditionalstatements827.
1.
2.
2.
NumericcomparisonsTheexamplesbelowusenumericalcomparisons:anny>num=`wc-lwork.
txt`anny>echo$num201anny>if["$num"-gt"150"]Moreinput>thenecho;echo"you'veworkedhardenoughfortoday.
"Moreinput>echo;fiyou'veworkedhardenoughfortoday.
anny>ThisscriptisexecutedbycroneverySunday.
Iftheweeknumberiseven,itremindsyoutoputoutthegarbagecans:#!
/bin/bash#Calculatetheweeknumberusingthedatecommand:WEEKOFFSET=$[$(date+"%V")%2]#Testifwehavearemainder.
Ifnot,thisisanevenweeksosendamessage.
#Else,donothing.
if[$WEEKOFFSET-eq"0"];thenecho"Sundayevening,putoutthegarbagecans.
"|mail-s"Garbagecansout"your@your_domain.
fi7.
1.
2.
3.
StringcomparisonsAnexampleofcomparingstringsfortestingtheuserID:if["$(whoami)"!
='root'];thenecho"Youhavenopermissiontorun$0asnon-rootuser.
"exit1;fiWithBash,youcanshortenthistypeofconstruct.
Thecompactequivalentoftheabovetestisasfollows:["$(whoami)"!
='root'echoyouareusinganon-privilegedaccount;exit1)Similartothe"&&"expressionwhichindicateswhattodoifthetestprovestrue,"||"specifieswhattodoifthetestisfalse.
Regularexpressionsmayalsobeusedincomparisons:anny>gender="female"anny>if[["$gender"==f*]]Moreinput>thenecho"Pleasuretomeetyou,Madame.
";fiPleasuretomeetyou,Madame.
anny>BashGuideforBeginnersChapter7.
Conditionalstatements83RealProgrammersMostprogrammerswillprefertousethetestbuilt-incommand,whichisequivalenttousingsquarebracketsforcomparison,likethis:test"$(whoami)"!
='root'&&(echoyouareusinganon-privilegedaccount;exit1)NoexitIfyouinvoketheexitinasubshell,itwillnotpassvariablestotheparent.
Use{and}insteadof(and)ifyoudonotwantBashtoforkasubshell.
SeetheinfopagesforBashformoreinformationonpatternmatchingwiththe"((EXPRESSION))"and"[[EXPRESSION]]"constructs.
7.
2.
Moreadvancedifusage7.
2.
1.
if/then/elseconstructs7.
2.
1.
1.
DummyexampleThisistheconstructtousetotakeonecourseofactioniftheifcommandstesttrue,andanotherifittestsfalse.
Anexample:freddyscripts>gender="male"freddyscripts>if[["$gender"=="f*"]]Moreinput>thenecho"Pleasuretomeetyou,Madame.
"Moreinput>elseecho"Howcometheladyhasn'tgotadrinkyet"Moreinput>fiHowcometheladyhasn'tgotadrinkyetfreddyscripts>[]vs.
[[]]Contraryto[,[[preventswordsplittingofvariablevalues.
So,ifVAR="varwithspaces",youdonotneedtodoublequote$VARinatest-eventhoughusingquotesremainsagoodhabit.
Also,[[preventspathnameexpansion,soliteralstringswithwildcardsdonottrytoexpandtofilenames.
Using[[,==and!
=interpretstringstotherightasshellglobpatternstobematchedagainstthevaluetotheleft,forinstance:[["value"==val*]].
LiketheCONSEQUENT-COMMANDSlistfollowingthethenstatement,theALTERNATE-CONSEQUENT-COMMANDSlistfollowingtheelsestatementcanholdanyUNIX-stylecommandthatreturnsanexitstatus.
Anotherexample,extendingtheonefromSection7.
1.
2.
1:anny~>su-Password:[root@eleganceroot]#if!
grep^$USER/etc/passwd1>/dev/null>thenecho"youruseraccountisnotmanagedlocally">elseecho"youraccountismanagedfromthelocal/etc/passwdfile">fiyouraccountismanagedfromthelocal/etc/passwdfileBashGuideforBeginnersChapter7.
Conditionalstatements84[root@eleganceroot]#Weswitchtotherootaccounttodemonstratetheeffectoftheelsestatement-yourrootisusuallyalocalaccountwhileyourownuseraccountmightbemanagedbyacentralsystem,suchasanLDAPserver.
7.
2.
1.
2.
CheckingcommandlineargumentsInsteadofsettingavariableandthenexecutingascript,itisfrequentlymoreeleganttoputthevaluesforthevariablesonthecommandline.
Weusethepositionalparameters$1,$2,.
.
.
,$Nforthispurpose.
$#referstothenumberofcommandlinearguments.
$0referstothenameofthescript.
Thefollowingisasimpleexample:Figure7-1.
TestingofacommandlineargumentwithifHere'sanotherexample,usingtwoarguments:anny~>catweight.
sh#!
/bin/bash#Thisscriptprintsamessageaboutyourweightifyougiveityour#weightinkilosandheightincentimeters.
weight="$1"height="$2"idealweight=$[$height-110]if[$weight-le$idealweight];thenecho"Youshouldeatabitmorefat.
"elseBashGuideforBeginnersChapter7.
Conditionalstatements85echo"Youshouldeatabitmorefruit.
"fianny~>bash-xweight.
sh55169+weight=55+height=169+idealweight=59+'['55-le59']'+echo'Youshouldeatabitmorefat.
'Youshouldeatabitmorefat.
7.
2.
1.
3.
TestingthenumberofargumentsThefollowingexampleshowshowtochangethepreviousscriptsothatitprintsamessageifmoreorlessthan2argumentsaregiven:anny~>catweight.
sh#!
/bin/bash#Thisscriptprintsamessageaboutyourweightifyougiveityour#weightinkilosandheightincentimeters.
if2];thenecho"Usage:$0weight_in_kiloslength_in_centimeters"exitfiweight="$1"height="$2"idealweight=$[$height-110]if[$weight-le$idealweight];thenecho"Youshouldeatabitmorefat.
"elseecho"Youshouldeatabitmorefruit.
"fianny~>weight.
sh70150Youshouldeatabitmorefruit.
anny~>weight.
sh7015033Usage:.
/weight.
shweight_in_kiloslength_in_centimetersThefirstargumentisreferredtoas$1,thesecondas$2andsoon.
Thetotalnumberofargumentsisstoredin$#.
CheckoutSection7.
2.
5foramoreelegantwaytoprintusagemessages.
7.
2.
1.
4.
TestingthatafileexistsThistestisdoneinalotofscripts,becausethere'snouseinstartingalotofprogramsifyouknowthey'renotgoingtowork:#!
/bin/bash#Thisscriptgivesinformationaboutafile.
FILENAME="$1"BashGuideforBeginnersChapter7.
Conditionalstatements86echo"Propertiesfor$FILENAME:"if[-f$FILENAME];thenecho"Sizeis$(ls-lh$FILENAME|awk'{print$5}')"echo"Typeis$(file$FILENAME|cut-d":"-f2-)"echo"Inodenumberis$(ls-i$FILENAME|cut-d""-f1-)"echo"$(df-h$FILENAME|grep-vMounted|awk'{print"On",$1",\whichismountedasthe",$6,"partition.
"}')"elseecho"Filedoesnotexist.
"fiNotethatthefileisreferredtousingavariable;inthiscaseitisthefirstargumenttothescript.
Alternatively,whennoargumentsaregiven,filelocationsareusuallystoredinvariablesatthebeginningofascript,andtheircontentisreferredtousingthesevariables.
Thus,whenyouwanttochangeafilenameinascript,youonlyneedtodoitonce.
FilenameswithspacesTheaboveexamplewillfailifthevalueof$1canbeparsedasmultiplewords.
Inthatcase,theifcommandcanbefixedeitherusingdoublequotesaroundthefilename,orbyusing[[insteadof[.
7.
2.
2.
if/then/elif/elseconstructs7.
2.
2.
1.
GeneralThisisthefullformoftheifstatement:ifTEST-COMMANDS;thenCONSEQUENT-COMMANDS;elifMORE-TEST-COMMANDS;thenMORE-CONSEQUENT-COMMANDS;elseALTERNATE-CONSEQUENT-COMMANDS;fiTheTEST-COMMANDSlistisexecuted,andifitsreturnstatusiszero,theCONSEQUENT-COMMANDSlistisexecuted.
IfTEST-COMMANDSreturnsanon-zerostatus,eacheliflistisexecutedinturn,andifitsexitstatusiszero,thecorrespondingMORE-CONSEQUENT-COMMANDSisexecutedandthecommandcompletes.
IfelseisfollowedbyanALTERNATE-CONSEQUENT-COMMANDSlist,andthefinalcommandinthefinaliforelifclausehasanon-zeroexitstatus,thenALTERNATE-CONSEQUENT-COMMANDSisexecuted.
Thereturnstatusistheexitstatusofthelastcommandexecuted,orzeroifnoconditiontestedtrue.
7.
2.
2.
2.
ExampleThisisanexamplethatyoucanputinyourcrontabfordailyexecution:anny/etc/cron.
daily>catdisktest.
shBashGuideforBeginnersChapter7.
Conditionalstatements87#!
/bin/bash#Thisscriptdoesaverysimpletestforcheckingdiskspace.
space=`df-h|awk'{print$5}'|grep%|grep-vUse|sort-n|tail-1|cut-d"%"-f1-`alertvalue="80"if["$space"-ge"$alertvalue"];thenecho"Atleastoneofmydisksisnearlyfull!
"|mail-s"dailydiskcheck"rootelseecho"Diskspacenormal"|mail-s"dailydiskcheck"rootfi7.
2.
3.
NestedifstatementsInsidetheifstatement,youcanuseanotherifstatement.
Youmayuseasmanylevelsofnestedifsasyoucanlogicallymanage.
Thisisanexampletestingleapyears:anny~/testdir>cattestleap.
sh#!
/bin/bash#Thisscriptwilltestifwe'reinaleapyearornot.
year=`date+%Y`if[$[$year%400]-eq"0"];thenecho"Thisisaleapyear.
Februaryhas29days.
"elif[$[$year%4]-eq0];thenif[$[$year%100]-ne0];thenecho"Thisisaleapyear,Februaryhas29days.
"elseecho"Thisisnotaleapyear.
Februaryhas28days.
"fielseecho"Thisisnotaleapyear.
Februaryhas28days.
"fianny~/testdir>dateTueJan1420:37:55CET2003anny~/testdir>testleap.
shThisisnotaleapyear.
7.
2.
4.
BooleanoperationsTheabovescriptcanbeshortenedusingtheBooleanoperators"AND"(&&)and"OR"(||).
Figure7-2.
ExampleusingBooleanoperatorsBashGuideforBeginnersChapter7.
Conditionalstatements88Weusethedoublebracketsfortestinganarithmeticexpression,seeSection3.
4.
6.
Thisisequivalenttotheletstatement.
Youwillgetstuckusingsquarebracketshere,ifyoutrysomethinglike$[$year%400],becausehere,thesquarebracketsdon'trepresentanactualcommandbythemselves.
Amongothereditors,gvimisoneofthosesupportingcolourschemesaccordingtothefileformat;sucheditorsareusefulfordetectingerrorsinyourcode.
7.
2.
5.
UsingtheexitstatementandifWealreadybrieflymettheexitstatementinSection7.
2.
1.
3.
Itterminatesexecutionoftheentirescript.
Itismostoftenusediftheinputrequestedfromtheuserisincorrect,ifastatementdidnotrunsuccessfullyorifsomeothererroroccurred.
Theexitstatementtakesanoptionalargument.
Thisargumentistheintegerexitstatuscode,whichispassedbacktotheparentandstoredinthe$variable.
Azeroargumentmeansthatthescriptransuccessfully.
Anyothervaluemaybeusedbyprogrammerstopassbackdifferentmessagestotheparent,sothatdifferentactionscanbetakenaccordingtofailureorsuccessofthechildprocess.
Ifnoargumentisgiventotheexitcommand,theparentshellusesthecurrentvalueofthe$variable.
Belowisanexamplewithaslightlyadaptedpenguin.
shscript,whichsendsitsexitstatusbacktotheparent,feed.
sh:anny~/testdir>catpenguin.
sh#!
/bin/bash#ThisscriptletsyoupresentdifferentmenustoTux.
Hewillonlybehappy#whengivenafish.
We'vealsoaddedadolphinand(presumably)acamel.
if["$menu"=="fish"];thenif["$animal"=="penguin"];thenecho"Hmmmmmmfish.
.
.
Tuxhappy!
"elif["$animal"=="dolphin"];thenecho"Pweetpeettreetppeterdepweet!
"elseecho"*prrrrrrrt*"BashGuideforBeginnersChapter7.
Conditionalstatements89fielseif["$animal"=="penguin"];thenecho"Tuxdon'tlikethat.
Tuxwantsfish!
"exit1elif["$animal"=="dolphin"];thenecho"Pweepwishpeeterdepweet!
"exit2elseecho"Willyoureadthissign!
"exit3fifiThisscriptiscalleduponinthenextone,whichthereforeexportsitsvariablesmenuandanimal:anny~/testdir>catfeed.
sh#!
/bin/bash#Thisscriptactsupontheexitstatusgivenbypenguin.
shexportmenu="$1"exportanimal="$2"feed="/nethome/anny/testdir/penguin.
sh"$feed$menu$animalcase$in1)echo"Guard:You'dbettergive'mafish,lesstheygetviolent.
.
.
";;2)echo"Guard:It'sbecauseofpeoplelikeyouthattheyareleavingearthallthetime.
.
.
";;3)echo"Guard:BuythefoodthattheZooprovidesfortheanimals,you***,howdoyouthinkwesurvive";;*)echo"Guard:Don'tforgettheguide!
";;esacanny~/testdir>.
/feed.
shapplepenguinTuxdon'tlikethat.
Tuxwantsfish!
Guard:You'dbettergive'mafish,lesstheygetviolent.
.
.
Asyoucansee,exitstatuscodescanbechosenfreely.
Existingcommandsusuallyhaveaseriesofdefinedcodes;seetheprogrammer'smanualforeachcommandformoreinformation.
7.
3.
Usingcasestatements7.
3.
1.
SimplifiedconditionsNestedifstatementsmightbenice,butassoonasyouareconfrontedwithacoupleofdifferentpossibleactionstotake,theytendtoconfuse.
Forthemorecomplexconditionals,usethecasesyntax:BashGuideforBeginnersChapter7.
Conditionalstatements90caseEXPRESSIONinCASE1)COMMAND-LIST;;CASE2)COMMAND-LIST;;.
.
.
CASEN)COMMAND-LIST;;esacEachcaseisanexpressionmatchingapattern.
ThecommandsintheCOMMAND-LISTforthefirstmatchareexecuted.
The"|"symbolisusedforseparatingmultiplepatterns,andthe")"operatorterminatesapatternlist.
Eachcaseplusitsaccordingcommandsarecalledaclause.
Eachclausemustbeterminatedwith";;".
Eachcasestatementisendedwiththeesacstatement.
Intheexample,wedemonstrateuseofcasesforsendingamoreselectivewarningmessagewiththedisktest.
shscript:anny~/testdir>catdisktest.
sh#!
/bin/bash#Thisscriptdoesaverysimpletestforcheckingdiskspace.
space=`df-h|awk'{print$5}'|grep%|grep-vUse|sort-n|tail-1|cut-d"%"-f1-`case$spacein[1-6]*)Message="Allisquiet.
";;[7-8]*)Message="Startthinkingaboutcleaningoutsomestuff.
There'sapartitionthatis$space%fu;;9[1-8])Message="Betterhurrywiththatnewdisk.
.
.
Onepartitionis$space%full.
";;99)Message="I'mdrowninghere!
There'sapartitionat$space%!
";;*)Message="Iseemtoberunningwithannonexistentamountofdiskspace.
.
.
";;esacecho$Message|mail-s"diskreport`date`"annyanny~/testdir>Youhavenewmail.
anny~/testdir>tail-16/var/spool/mail/annyFromanny@octarineTueJan1422:10:472003Return-Path:Received:fromoctarine(localhost[127.
0.
0.
1])byoctarine(8.
12.
5/8.
12.
5)withESMTPidh0ELAlBG020414for;Tue,14Jan200322:10:47+0100Received:(fromanny@localhost)byoctarine(8.
12.
5/8.
12.
5/Submit)idh0ELAltn020413foranny;Tue,14Jan200322:10:47+0100Date:Tue,14Jan200322:10:47+0100From:AnnyMessage-Id:To:anny@octarineSubject:diskreportTueJan1422:10:47CET2003Startthinkingaboutcleaningoutsomestuff.
There'sapartitionthatis87%full.
anny~/testdir>BashGuideforBeginnersChapter7.
Conditionalstatements91Ofcourseyoucouldhaveopenedyourmailprogramtochecktheresults;thisisjusttodemonstratethatthescriptsendsadecentmailwith"To:","Subject:"and"From:"headerlines.
Manymoreexamplesusingcasestatementscanbefoundinyoursystem'sinitscriptdirectory.
Thestartupscriptsusestartandstopcasestorunorstopsystemprocesses.
Atheoreticalexamplecanbefoundinthenextsection.
7.
3.
2.
InitscriptexampleInitscriptsoftenmakeuseofcasestatementsforstarting,stoppingandqueryingsystemservices.
ThisisanexcerptofthescriptthatstartsAnacron,adaemonthatrunscommandsperiodicallywithafrequencyspecifiedindays.
case"$1"instart)start;;stop)stop;;status)statusanacron;;restart)stopstart;;condrestart)iftest"x`pidofanacron`"!
=x;thenstopstartfi;;*)echo$"Usage:$0{start|stop|restart|condrestart|status}"exit1esacThetaskstoexecuteineachcase,suchasstoppingandstartingthedaemon,aredefinedinfunctions,whicharepartiallysourcedfromthe/etc/rc.
d/init.
d/functionsfile.
SeeChapter11formoreexplanation.
7.
4.
SummaryInthischapterwelearnedhowtobuildconditionsintoourscriptssothatdifferentactionscanbeundertakenuponsuccessorfailureofacommand.
Theactionscanbedeterminedusingtheifstatement.
Thisallowsyoutoperformarithmeticandstringcomparisons,andtestingofexitcode,inputandfilesneededbythescript.
Asimpleif/then/fitestoftenpreceedscommandsinashellscriptinordertopreventoutputgeneration,sothatthescriptcaneasilyberuninthebackgroundorthroughthecronfacility.
Morecomplexdefinitionsofconditionsareusuallyputinacasestatement.
BashGuideforBeginnersChapter7.
Conditionalstatements92Uponsuccessfulconditiontesting,thescriptcanexplicitlyinformtheparentusingtheexit0status.
Uponfailure,anyothernumbermaybereturned.
Basedonthereturncode,theparentprogramcantakeappropriateaction.
7.
5.
ExercisesHerearesomeideastogetyoustartedusingifinscripts:Useanif/then/elif/elseconstructthatprintsinformationaboutthecurrentmonth.
Thescriptshouldprintthenumberofdaysinthismonth,andgiveinformationaboutleapyearsifthecurrentmonthisFebruary.
1.
Dothesame,usingacasestatementandanalternativeuseofthedatecommand.
2.
Modify/etc/profilesothatyougetaspecialgreetingmessagewhenyouconnecttoyoursystemasroot.
3.
Edittheleaptest.
shscriptfromSection7.
2.
4sothatitrequiresoneargument,theyear.
Testthatexactlyoneargumentissupplied.
4.
Writeascriptcalledwhichdaemon.
shthatchecksifthehttpdandinitdaemonsarerunningonyoursystem.
Ifanhttpdisrunning,thescriptshouldprintamessagelike,"Thismachineisrunningawebserver.
"Usepstocheckonprocesses.
5.
Writeascriptthatmakesabackupofyourhomedirectoryonaremotemachineusingscp.
Thescriptshouldreportinalogfile,forinstance~/log/homebackup.
log.
Ifyoudon'thaveasecondmachinetocopythebackupto,usescptotestcopyingittothelocalhost.
ThisrequiresSSHkeysbetweenthetwohosts,orelseyouhavetosupplyapassword.
ThecreationofSSHkeysisexplainedinmanssh-keygen.
6.
AdaptthescriptfromthefirstexampleinSection7.
3.
1toincludethecaseofexactly90%diskspaceusage,andlowerthan10%diskspaceusage.
7.
Thescriptshouldusetarcfforthecreationofthebackupandgziporbzip2forcompressingthe.
tarfile.
Putallfilenamesinvariables.
Putthenameoftheremoteserverandtheremotedirectoryinavariable.
Thiswillmakeiteasiertore-usethescriptortomakechangestoitinthefuture.
Thescriptshouldcheckfortheexistenceofacompressedarchive.
Ifthisexists,removeitfirstinordertopreventoutputgeneration.
Thescriptshouldalsocheckforavailablediskspace.
Keepinmindthatatanygivenmomentyoucouldhavethedatainyourhomedirectory,thedatainthe.
tarfileandthedatainthecompressedarchivealltogetheronyourdisk.
Ifthereisnotenoughdiskspace,exitwithanerrormessageinthelogfile.
Thescriptshouldcleanupthecompressedarchivebeforeitexits.
BashGuideforBeginnersChapter7.
Conditionalstatements93Chapter8.
WritinginteractivescriptsInthischapterwewilldiscusshowtointeractwiththeusersofourscripts:PrintinguserfriendlymessagesandexplanationsCatchinguserinputPromptingforuserinputUsingthefiledescriptorstoreadfromandwritetomultiplefiles8.
1.
Displayingusermessages8.
1.
1.
InteractiveornotSomescriptsrunwithoutanyinteractionfromtheuseratall.
Advantagesofnon-interactivescriptsinclude:Thescriptrunsinapredictablewayeverytime.
Thescriptcanruninthebackground.
Manyscripts,however,requireinputfromtheuser,orgiveoutputtotheuserasthescriptisrunning.
Theadvantagesofinteractivescriptsare,amongothers:Moreflexiblescriptscanbebuilt.
Userscancustomizethescriptasitrunsormakeitbehaveindifferentways.
Thescriptcanreportitsprogressasitruns.
Whenwritinginteractivescripts,neverholdbackoncomments.
Ascriptthatprintsappropriatemessagesismuchmoreuser-friendlyandcanbemoreeasilydebugged.
Ascriptmightdoaperfectjob,butyouwillgetawholelotofsupportcallsifitdoesnotinformtheuseraboutwhatitisdoing.
Soincludemessagesthattelltheusertowaitforoutputbecauseacalculationisbeingdone.
Ifpossible,trytogiveanindicationofhowlongtheuserwillhavetowait.
Ifthewaitingshouldregularlytakealongtimewhenexecutingacertaintask,youmightwanttoconsiderintegratingsomeprocessingindicationintheoutputofyourscript.
Whenpromptingtheuserforinput,itisalsobettertogivetoomuchthantoolittleinformationaboutthekindofdatatobeentered.
Thisappliestothecheckingofargumentsandtheaccompanyingusagemessageaswell.
Bashhastheechoandprintfcommandstoprovidecommentsforusers,andalthoughyoushouldbefamiliarwithatleasttheuseofechobynow,wewilldiscusssomemoreexamplesinthenextsections.
8.
1.
2.
Usingtheechobuilt-incommandTheechobuilt-incommandoutputsitsarguments,separatedbyspacesandterminatedwithanewlinecharacter.
Thereturnstatusisalwayszero.
echotakesacoupleofoptions:-e:interpretsbackslash-escapedcharacters.
-n:suppressesthetrailingnewline.
Asanexampleofaddingcomments,wewillmakethefeed.
shandpenguin.
shfromSection7.
2.
1.
2aChapter8.
Writinginteractivescripts94bitbetter:michel~/test>catpenguin.
sh#!
/bin/bash#ThisscriptletsyoupresentdifferentmenustoTux.
Hewillonlybehappy#whengivenafish.
Tomakeitmorefun,weaddedacouplemoreanimals.
if["$menu"=="fish"];thenif["$animal"=="penguin"];thenecho-e"Hmmmmmmfish.
.
.
Tuxhappy!
\n"elif["$animal"=="dolphin"];thenecho-e"\a\a\aPweetpeettreetppeterdepweet!
\a\a\a\n"elseecho-e"*prrrrrrrt*\n"fielseif["$animal"=="penguin"];thenecho-e"Tuxdon'tlikethat.
Tuxwantsfish!
\n"exit1elif["$animal"=="dolphin"];thenecho-e"\a\a\a\a\a\aPweepwishpeeterdepweet!
\a\a\a"exit2elseecho-e"Willyoureadthissign!
Don'tfeedthe"$animal"s!
\n"exit3fifimichel~/test>catfeed.
sh#!
/bin/bash#Thisscriptactsupontheexitstatusgivenbypenguin.
shif2"];thenecho-e"Usageofthefeedscript:\t$0food-on-menuanimal-name\n"exit1elseexportmenu="$1"exportanimal="$2"echo-e"Feeding$menuto$animal.
.
.
\n"feed="/nethome/anny/testdir/penguin.
sh"$feed$menu$animalresult="$"echo-e"Donefeeding.
\n"case"$result"in1)echo-e"Guard:\"You'dbettergive'mafish,lesstheygetviolent.
.
.
\"\n";;2)echo-e"Guard:\"Nowondertheyfleeourplanet.
.
.
\"\n";;3)echo-e"Guard:\"BuythefoodthattheZooprovidesattheentry,you***\"\n"echo-e"Guard:\"Youwanttopoisonthem,doyou\"\n"BashGuideforBeginnersChapter8.
Writinginteractivescripts95;;*)echo-e"Guard:\"Don'tforgettheguide!
\"\n";;esacfiecho"Leaving.
.
.
"echo-e"\a\a\aThanksforvisitingtheZoo,hopetoseeyouagainsoon!
\n"michel~/test>feed.
shapplecamelFeedingappletocamel.
.
.
Willyoureadthissign!
Don'tfeedthecamels!
Donefeeding.
Guard:"BuythefoodthattheZooprovidesattheentry,you***"Guard:"Youwanttopoisonthem,doyou"Leaving.
.
.
ThanksforvisitingtheZoo,hopetoseeyouagainsoon!
michel~/test>feed.
shappleUsageofthefeedscript:.
/feed.
shfood-on-menuanimal-nameMoreaboutescapecharacterscanbefoundinSection3.
3.
2.
Thefollowingtablegivesanoverviewofsequencesrecognizedbytheechocommand:Table8-1.
EscapesequencesusedbytheechocommandSequenceMeaning\aAlert(bell).
\bBackspace.
\cSuppresstrailingnewline.
\eEscape.
\fFormfeed.
\nNewline.
\rCarriagereturn.
\tHorizontaltab.
\vVerticaltab.
\\Backslash.
\0NNNTheeight-bitcharacterwhosevalueistheoctalvalueNNN(zerotothreeoctaldigits).
\NNNTheeight-bitcharacterwhosevalueistheoctalvalueNNN(onetothreeoctaldigits).
\xHHTheeight-bitcharacterwhosevalueisthehexadecimalvalue(oneortwohexadecimaldigits).
Formoreinformationabouttheprintfcommandandthewayitallowsyoutoformatoutput,seetheBashinfopages.
KeepinmindthattheremightbedifferencesbetweendifferentversionsofBash.
BashGuideforBeginnersChapter8.
Writinginteractivescripts968.
2.
Catchinguserinput8.
2.
1.
Usingthereadbuilt-incommandThereadbuilt-incommandisthecounterpartoftheechoandprintfcommands.
Thesyntaxofthereadcommandisasfollows:read[options]NAME1NAME2.
.
.
NAMENOnelineisreadfromthestandardinput,orfromthefiledescriptorsuppliedasanargumenttothe-uoption.
Thefirstwordofthelineisassignedtothefirstname,NAME1,thesecondwordtothesecondname,andsoon,withleftoverwordsandtheirinterveningseparatorsassignedtothelastname,NAMEN.
Iftherearefewerwordsreadfromtheinputstreamthantherearenames,theremainingnamesareassignedemptyvalues.
ThecharactersinthevalueoftheIFSvariableareusedtosplittheinputlineintowordsortokens;seeSection3.
4.
8.
Thebackslashcharactermaybeusedtoremoveanyspecialmeaningforthenextcharacterreadandforlinecontinuation.
Ifnonamesaresupplied,thelinereadisassignedtothevariableREPLY.
Thereturncodeofthereadcommandiszero,unlessanend-of-filecharacterisencountered,ifreadtimesoutorifaninvalidfiledescriptorissuppliedastheargumenttothe-uoption.
ThefollowingoptionsaresupportedbytheBashreadbuilt-in:Table8-2.
Optionstothereadbuilt-inOptionMeaning-aANAMEThewordsareassignedtosequentialindexesofthearrayvariableANAME,startingat0.
AllelementsareremovedfromANAMEbeforetheassignment.
OtherNAMEargumentsareignored.
-dDELIMThefirstcharacterofDELIMisusedtoterminatetheinputline,ratherthannewline.
-ereadlineisusedtoobtaintheline.
-nNCHARSreadreturnsafterreadingNCHARScharactersratherthanwaitingforacompletelineofinput.
-pPROMPTDisplayPROMPT,withoutatrailingnewline,beforeattemptingtoreadanyinput.
Thepromptisdisplayedonlyifinputiscomingfromaterminal.
-rIfthisoptionisgiven,backslashdoesnotactasanescapecharacter.
Thebackslashisconsideredtobepartoftheline.
Inparticular,abackslash-newlinepairmaynotbeusedasalinecontinuation.
-sSilentmode.
Ifinputiscomingfromaterminal,charactersarenotechoed.
-tTIMEOUTCausereadtotimeoutandreturnfailureifacompletelineofinputisnotreadwithinTIMEOUTseconds.
Thisoptionhasnoeffectifreadisnotreadinginputfromtheterminalorfromapipe.
-uFDReadinputfromfiledescriptorFD.
Thisisastraightforwardexample,improvingontheleaptest.
shscriptfromthepreviouschapter:BashGuideforBeginnersChapter8.
Writinginteractivescripts97michel~/test>catleaptest.
sh#!
/bin/bash#Thisscriptwilltestifyouhavegivenaleapyearornot.
echo"Typetheyearthatyouwanttocheck(4digits),followedby[ENTER]:"readyearif((("$year"%400)=="0"year"%4=="0"year"%100!
="0")));thenecho"$yearisaleapyear.
"elseecho"Thisisnotaleapyear.
"fimichel~/test>leaptest.
shTypetheyearthatyouwanttocheck(4digits),followedby[ENTER]:20002000isaleapyear.
8.
2.
2.
PromptingforuserinputThefollowingexampleshowshowyoucanusepromptstoexplainwhattheusershouldenter.
michel~/test>catfriends.
sh#!
/bin/bash#Thisisaprogramthatkeepsyouraddressbookuptodate.
friends="/var/tmp/michel/friends"echo"Hello,"$USER".
ThisscriptwillregisteryouinMichel'sfriendsdatabase.
"echo-n"Enteryournameandpress[ENTER]:"readnameecho-n"Enteryourgenderandpress[ENTER]:"read-n1genderechogrep-i"$name""$friends"if[$==0];thenecho"Youarealreadyregistered,quitting.
"exit1elif["$gender"=="m"];thenecho"YouareaddedtoMichel'sfriendslist.
"exit1elseecho-n"Howoldareyou"readageif[$age-lt25];thenecho-n"Whichcolourofhairdoyouhave"readcolourecho"$name$age$colour">>"$friends"echo"YouareaddedtoMichel'sfriendslist.
Thankyousomuch!
"elseecho"YouareaddedtoMichel'sfriendslist.
"exit1fifiBashGuideforBeginnersChapter8.
Writinginteractivescripts98michel~/test>cpfriends.
sh/var/tmp;cd/var/tmpmichel~/test>touchfriends;chmoda+wfriendsmichel~/test>friends.
shHello,michel.
ThisscriptwillregisteryouinMichel'sfriendsdatabase.
Enteryournameandpress[ENTER]:michelEnteryourgenderandpress[ENTER]:mYouareaddedtoMichel'sfriendslist.
michel~/test>catfriendsNotethatnooutputisomittedhere.
ThescriptonlystoresinformationaboutthepeopleMichelisinterestedin,butitwillalwayssayyouareaddedtothelist,unlessyouarealreadyinit.
Otherpeoplecannowstartexecutingthescript:[anny@octarinetmp]$friends.
shHello,anny.
ThisscriptwillregisteryouinMichel'sfriendsdatabase.
Enteryournameandpress[ENTER]:annyEnteryourgenderandpress[ENTER]:fHowoldareyou22WhichcolourofhairdoyouhaveblackYouareaddedtoMichel'sfriendslist.
Afterawhile,thefriendslistbeginstolooklikethis:tille24blackanny22blackkatya22blondemaria21black--outputomitted--Ofcourse,thissituationisnotideal,sinceeverybodycanedit(butnotdelete)Michel'sfiles.
Youcansolvethisproblemusingspecialaccessmodesonthescriptfile,seeSUIDandSGIDintheIntroductiontoLinuxguide.
8.
2.
3.
Redirectionandfiledescriptors8.
2.
3.
1.
GeneralAsyouknowfrombasicshellusage,inputandoutputofacommandmayberedirectedbeforeitisexecuted,usingaspecialnotation-theredirectionoperators-interpretedbytheshell.
Redirectionmayalsobeusedtoopenandclosefilesforthecurrentshellexecutionenvironment.
Redirectioncanalsooccurinascript,sothatitcanreceiveinputfromafile,forinstance,orsendoutputtoafile.
Later,theusercanreviewtheoutputfile,oritmaybeusedbyanotherscriptasinput.
Fileinputandoutputareaccomplishedbyintegerhandlesthattrackallopenfilesforagivenprocess.
Thesenumericvaluesareknownasfiledescriptors.
Thebestknownfiledescriptorsarestdin,stdoutandstderr,withfiledescriptornumbers0,1and2,respectively.
Thesenumbersandrespectivedevicesarereserved.
BashcantakeTCPorUDPportsonnetworkedhostsasfiledescriptorsaswell.
Theoutputbelowshowshowthereservedfiledescriptorspointtoactualdevices:BashGuideforBeginnersChapter8.
Writinginteractivescripts99michel~>ls-l/dev/std*lrwxrwxrwx1rootroot17Oct207:46/dev/stderr->.
.
/proc/self/fd/2lrwxrwxrwx1rootroot17Oct207:46/dev/stdin->.
.
/proc/self/fd/0lrwxrwxrwx1rootroot17Oct207:46/dev/stdout->.
.
/proc/self/fd/1michel~>ls-l/proc/self/fd/[0-2]lrwx------1michelmichel64Jan2312:11/proc/self/fd/0->/dev/pts/6lrwx------1michelmichel64Jan2312:11/proc/self/fd/1->/dev/pts/6lrwx------1michelmichel64Jan2312:11/proc/self/fd/2->/dev/pts/6Notethateachprocesshasitsownviewofthefilesunder/proc/self,asitisactuallyasymboliclinkto/proc/.
YoumightwanttocheckinfoMAKEDEVandinfoprocformoreinformationabout/procsubdirectoriesandthewayyoursystemhandlesstandardfiledescriptorsforeachrunningprocess.
Whenexcutingagivencommand,thefollowingstepsareexcuted,inorder:Ifthestandardoutputofapreviouscommandisbeingpipedtothestandardinputofthecurrentcommand,then/proc//fd/0isupdatedtotargetthesameanonymouspipeas/proc//fd/1isupdatedtotargetanotheranonymouspipe.
Redirectionforthecurrentcommandisprocessedfromlefttoright.
Redirection"N>&M"or"Nfile"and"N&-"hastheeffectofdeletingthesymboliclink/proc/self/fd/N.
Onlynowisthecurrentcommandexecuted.
Whenyourunascriptfromthecommandline,nothingmuchchangesbecausethechildshellprocesswillusethesamefiledescriptorsastheparent.
Whennosuchparentisavailable,forinstancewhenyourunascriptusingthecronfacility,thestandardfiledescriptorsarepipesorother(temporary)files,unlesssomeformofredirectionisused.
Thisisdemonstratedintheexamplebelow,whichshowsoutputfromasimpleatscript:michel~>dateFriJan2411:05:50CET2003michel~>at1107warning:commandswillbeexecutedusing(inorder)a)$SHELLb)loginshellc)/bin/shat>ls-l/proc/self/fd/>/var/tmp/fdtest.
atat>job10at2003-01-2411:07michel~>cat/var/tmp/fdtest.
attotal0lr-x------1michelmichel64Jan2411:070->/var/spool/at/!
0000c010959eb(deleted)l-wx------1michelmichel64Jan2411:071->/var/tmp/fdtest.
atl-wx------1michelmichel64Jan2411:072->/var/spool/at/spool/a0000c010959eblr-x------1michelmichel64Jan2411:073->/proc/21949/fdAndonewithcron:michel~>crontab-lBashGuideforBeginnersChapter8.
Writinginteractivescripts100#DONOTEDITTHISFILE-editthemasterandreinstall.
#(/tmp/crontab.
21968installedonFriJan2411:30:412003)#(Cronversion--$Id:chap8.
xml,v1.
92006/09/2809:42:45tilleExp$)3211***ls-l/proc/self/fd/>/var/tmp/fdtest.
cronmichel~>cat/var/tmp/fdtest.
crontotal0lr-x------1michelmichel64Jan2411:320->pipe:[124440]l-wx------1michelmichel64Jan2411:321->/var/tmp/fdtest.
cronl-wx------1michelmichel64Jan2411:322->pipe:[124441]lr-x------1michelmichel64Jan2411:323->/proc/21974/fd8.
2.
3.
2.
RedirectionoferrorsFromthepreviousexamples,itisclearthatyoucanprovideinputandoutputfilesforascript(seeSection8.
2.
4formore),butsometendtoforgetaboutredirectingerrors-outputwhichmightbedependeduponlateron.
Also,ifyouarelucky,errorswillbemailedtoyouandeventualcausesoffailuremightgetrevealed.
Ifyouarenotaslucky,errorswillcauseyourscripttofailandwon'tbecaughtorsentanywhere,sothatyoucan'tstarttodoanyworthwhiledebugging.
Whenredirectingerrors,notethattheorderofprecedenceissignificant.
Forexample,thiscommand,issuedin/var/spoolls-l*2>/var/tmp/unaccessible-in-spoolwillredirectstandardoutputofthelscommandtothefileunaccessible-in-spoolin/var/tmp.
Thecommandls-l*>/var/tmp/spoollist2>&1willdirectbothstandardinputandstandarderrortothefilespoollist.
Thecommandls-l*2>&1>/var/tmp/spoollistdirectsonlythestandardoutputtothedestinationfile,becausethestandarderroriscopiedtostandardoutputbeforethestandardoutputisredirected.
Forconvenience,errorsareoftenredirectedto/dev/null,ifitissuretheywillnotbeneeded.
Hundredsofexamplescanbefoundinthestartupscriptsforyoursystem.
BashallowsforbothstandardoutputandstandarderrortoberedirectedtothefilewhosenameistheresultoftheexpansionofFILEwiththisconstruct:&>FILEThisistheequivalentof>FILE2>&1,theconstructusedintheprevioussetofexamples.
Itisalsooftencombinedwithredirectionto/dev/null,forinstancewhenyoujustwantacommandtoexecute,nomatterwhatoutputorerrorsitgives.
8.
2.
4.
Fileinputandoutput8.
2.
4.
1.
Using/dev/fdThe/dev/fddirectorycontainsentriesnamed0,1,2,andsoon.
Openingthefile/dev/fd/NisequivalenttoduplicatingfiledescriptorN.
Ifyoursystemprovides/dev/stdin,/dev/stdoutandBashGuideforBeginnersChapter8.
Writinginteractivescripts101/dev/stderr,youwillseethattheseareequivalentto/dev/fd/0,/dev/fd/1and/dev/fd/2,respectively.
Themainuseofthe/dev/fdfilesisfromtheshell.
Thismechanismallowsforprogramsthatusepathnameargumentstohandlestandardinputandstandardoutputinthesamewayasotherpathnames.
If/dev/fdisnotavailableonasystem,you'llhavetofindawaytobypasstheproblem.
Thiscanbedoneforinstanceusingahyphen(-)toindicatethataprogramshouldreadfromapipe.
Anexample:michel~>filterbody.
txt.
gz|catheader.
txt-footer.
txtThistextisprintedatthebeginningofeachprintjobandthanksthesysadminforsettingusupsuchagreatprintinginfrastructure.
Texttobefiltered.
Thistextisprintedattheendofeachprintjob.
Thecatcommandfirstreadsthefileheader.
txt,nextitsstandardinputwhichistheoutputofthefiltercommand,andlastthefooter.
txtfile.
Thespecialmeaningofthehyphenasacommand-lineargumenttorefertothestandardinputorstandardoutputisamisconceptionthathascreptintomanyprograms.
Theremightalsobeproblemswhenspecifyinghyphenasthefirstargument,sinceitmightbeinterpretedasanoptiontotheprecedingcommand.
Using/dev/fdallowsforuniformityandpreventsconfusion:michel~>filterbody.
txt|catheader.
txt/dev/fd/0footer.
txt|lpInthiscleanexample,alloutputisadditionallypipedthroughlptosendittothedefaultprinter.
8.
2.
4.
2.
Readandexec8.
2.
4.
2.
1.
AssigningfiledescriptorstofilesAnotherwayoflookingatfiledescriptorsisthinkingofthemasawaytoassignanumericvaluetoafile.
Insteadofusingthefilename,youcanusethefiledescriptornumber.
Theexecbuilt-incommandcanbeusedtoreplacetheshellofthecurrentprocessortoalterthefiledescriptorsofthecurrentshell.
Forexample,itcanbeusedtoassignafiledescriptortoafile.
UseexecfdN>fileforassigningfiledescriptorNtofileforoutput,andexecfdNexec4>result.
txtmichel~>filterbody.
txt|catheader.
txt/dev/fd/0footer.
txt>&4michel~>catresult.
txtThistextisprintedatthebeginningofeachprintjobandthanksthesysadminforsettingusupsuchagreatprintinginfrastructure.
Texttobefiltered.
Thistextisprintedattheendofeachprintjob.
BashGuideforBeginnersChapter8.
Writinginteractivescripts102Filedescriptor5Usingthisfiledescriptormightcauseproblems,seetheAdvancedBash-ScriptingGuide,chapter16.
Youarestronglyadvisednottouseit.
8.
2.
4.
2.
2.
ReadinscriptsThefollowingisanexamplethatshowshowyoucanalternatebetweenfileinputandcommandlineinput:michel~/testdir>catsysnotes.
sh#!
/bin/bash#Thisscriptmakesanindexofimportantconfigfiles,putsthemtogetherin#abackupfileandallowsforaddingcommentforeachfile.
CONFIG=/var/tmp/sysconfig.
outrm"$CONFIG"2>/dev/nullecho"Outputwillbesavedin$CONFIG.
"#createfd7withsametargetasfd0(savestdin"value")exec7>"$CONFIG"echo$rootpasswd>>"$CONFIG"#updatefd0totargetfd7target(oldfd0target);deletefd7exec0>"$CONFIG"echo"Savinghostsinformation.
.
.
"#firstprepareahostsfilenotcontaininganycommentsTEMP="/var/tmp/hosts.
tmp"cat/etc/hosts|grep-vTEMP"exec7>"$CONFIG"echo"$ip1$name1$alias1">>"$CONFIG"echo"$ip2$name2$alias2">>"$CONFIG"exec0>"$CONFIG"BashGuideforBeginnersChapter8.
Writinginteractivescripts103rm"$TEMP"michel~/testdir>sysnotes.
shOutputwillbesavedin/var/tmp/sysconfig.
out.
Savingrootaccountinfo.
.
.
Entercommentor[ENTER]fornocomment:hintforpassword:bluelagoonSavinghostsinformation.
.
.
Entercommentor[ENTER]fornocomment:incentralDNSmichel~/testdir>cat/var/tmp/sysconfig.
outYourrootaccountinfo:root:x:0:0:root:/root:/bin/bashhintforpassword:bluelagoonYourlocalhostconfiguration:127.
0.
0.
1localhost.
localdomainlocalhost192.
168.
42.
1tintagel.
kingarthur.
comtintagelincentralDNS8.
2.
4.
3.
ClosingfiledescriptorsSincechildprocessesinheritopenfiledescriptors,itisgoodpracticetocloseafiledescriptorwhenitisnolongerneeded.
Thisisdoneusingtheexecfdcatlistdirs.
sh#!
/bin/bash#Thisscriptprintsstandardoutputunchanged,whilestandarderroris#redirectedforprocessingbyawk.
INPUTDIR="$1"#fd6targetsfd1target(consoleout)incurrentshellexec6>&1#fd1targetspipe,fd2targetsfd1target(pipe),#fd1targetsfd6target(consoleout),fd6closed,executelsls"$INPUTDIR"/*2>&1>&66>&-\#Closesfd6forawk,butnotforls.
|awk'BEGIN{FS=print"YOUHAVENOACCESSTO"$2}'6>&-#fd6closedforcurrentshellexec6>&-8.
2.
4.
4.
HeredocumentsFrequently,yourscriptmightcallonanotherprogramorscriptthatrequiresinput.
Theheredocumentprovidesawayofinstructingtheshelltoreadinputfromthecurrentsourceuntilalinecontainingonlythesearchstringisfound(notrailingblanks).
Allofthelinesreaduptothatpointarethenusedasthestandardinputforacommand.
BashGuideforBeginnersChapter8.
Writinginteractivescripts104Theresultisthatyoudon'tneedtocallonseparatefiles;youcanuseshell-specialcharacters,anditlooksnicerthanabunchofecho's:michel~>catstartsurf.
sh#!
/bin/bash#Thisscriptprovidesaneasywayforuserstochoosebetweenbrowsers.
echo"Thesearethewebbrowsersonthissystem:"#Startheredocumentcatstartsurf.
shThesearethewebbrowsersonthissystem:mozillalinkslynxkonqueroroperanetscapeWhichisyourfavoriteoperaStartingopera,pleasewait.
.
.
Althoughwetalkaboutaheredocument,itissupposedtobeaconstructwithinthesamescript.
Thisisanexamplethatinstallsapackageautomatically,eventhoughyoushouldnormallyconfirm:#!
/bin/bash#Thisscriptinstallspackagesautomatically,usingyum.
if[$#-lt1];thenecho"Usage:$0package.
"exit1fiyuminstall$1BashGuideforBeginnersChapter8.
Writinginteractivescripts105Server:JPackage1.
5forFedoraCore2Server:JPackage1.
5,genericServer:FedoraLinux2-i386-updatesFindingupdatedpackagesDownloadingneededheadersResolvingdependenciesDependenciesresolvedIwilldothefollowing:[install:tuxracer0.
61-26.
i386]Isthisok[y/N]:EnterDownloadingPackagesRunningtesttransaction:Testtransactioncomplete,Success!
tuxracer100%done1/1Installed:tuxracer0.
61-26.
i386Transaction(s)Complete8.
3.
SummaryInthischapter,welearnedhowtoprovideusercommentsandhowtopromptforuserinput.
Thisisusuallydoneusingtheecho/readcombination.
Wealsodiscussedhowfilescanbeusedasinputandoutputusingfiledescriptorsandredirection,andhowthiscanbecombinedwithgettinginputfromtheuser.
Westressedtheimportanceofprovidingamplemessagefortheusersofourscripts.
Asalwayswhenothersuseyourscripts,itisbettertogivetoomuchinformationthannotenough.
Heredocumentsisatypeofshellconstructthatallowscreationoflists,holdingchoicesfortheusers.
Thisconstructcanalsobeusedtoexecuteotherwiseinteractivetasksinthebackground,withoutintervention.
8.
4.
ExercisesTheseexercisesarepracticalapplicationsoftheconstructsdiscussedinthischapter.
Whenwritingthescripts,youmaytestbyusingatestdirectorythatdoesnotcontaintoomuchdata.
Writeeachstep,thentestthatportionofcode,ratherthanwritingeverythingatonce.
Writeascriptthatasksfortheuser'sage.
Ifitisequaltoorhigherthan16,printamessagesayingthatthisuserisallowedtodrinkalcohol.
Iftheuser'sageisbelow16,printamessagetellingtheuserhowmanyyearsheorshehastowaitbeforelegallybeingallowedtodrink.
Asanextra,calculatehowmuchbeeran18+userhasdrunkstatistically(100liters/year)andprintthisinformationfortheuser.
1.
Writeascriptthattakesonefileasanargument.
Useaheredocumentthatpresentstheuserwithacoupleofchoicesforcompressingthefile.
Possiblechoicescouldbegzip,bzip2,compressandzip.
2.
Writeascriptcalledhomebackupthatautomatestarsothepersonexecutingthescriptalwaysusesthedesiredoptions(cvp)andbackupdestinationdirectory(/var/backups)tomakeabackupofhisorherhomedirectory.
Implementthefollowingfeatures:Testforthenumberofarguments.
Thescriptshouldrunwithoutarguments.
Ifanyargumentsarepresent,exitafterprintingausagemessage.
Determinewhetherthebackupsdirectoryhasenoughfreespacetoholdthebackup.
Asktheuserwhetherafulloranincrementalbackupiswanted.
Iftheuserdoesnothaveafullbackupfileyet,printamessagethatafullbackupwillbetaken.
Incaseofanincrementalbackup,onlydothisifthefullbackupisnotolderthanaweek.
3.
BashGuideforBeginnersChapter8.
Writinginteractivescripts106Compressthebackupusinganycompressiontool.
Informtheuserthatthescriptisdoingthis,becauseitmighttakesometime,duringwhichtheusermightstartworryingifnooutputappearsonthescreen.
Printamessageinformingtheuseraboutthesizeofthecompressedbackup.
SeeinfotarorIntroductiontoLinux,chapter9:"Preparingyourdata"forbackgroundinformation.
Writeascriptcalledsimple-useradd.
shthataddsalocalusertothesystem.
Thisscriptshould:Takeonlyoneargument,orelseexitafterprintingausagemessage.
Check/etc/passwdanddecideonthefirstfreeuserID.
PrintamessagecontainingthisID.
Createaprivategroupforthisuser,checkingthe/etc/groupfile.
PrintamessagecontainingthegroupID.
Gatherinformationfromtheoperatoruser:acommentdescribingthisuser,choicefromalistofshells(testforacceptability,elseexitprintingamessage),expirationdateforthisaccount,extragroupsofwhichthenewusershouldbeamember.
Withtheobtainedinformation,addalineto/etc/passwd,/etc/groupand/etc/shadow;createtheuser'shomedirectory(withcorrectpermissions!
);addtheusertothedesiredsecondarygroups.
Setthepasswordforthisusertoadefaultknownstring.
4.
RewritethescriptfromSection7.
2.
1.
4sothatitreadsinputfromtheuserinsteadoftakingitfromthefirstargument.
5.
BashGuideforBeginnersChapter8.
Writinginteractivescripts107Chapter9.
RepetitivetasksUponcompletionofthischapter,youwillbeabletoUsefor,whileanduntilloops,anddecidewhichloopfitswhichoccasion.
UsethebreakandcontinueBashbuilt-ins.
Writescriptsusingtheselectstatement.
Writescriptsthattakeavariablenumberofarguments.
9.
1.
Theforloop9.
1.
1.
HowdoesitworkTheforloopisthefirstofthethreeshellloopingconstructs.
Thisloopallowsforspecificationofalistofvalues.
Alistofcommandsisexecutedforeachvalueinthelist.
Thesyntaxforthisloopis:forNAME[inLIST];doCOMMANDS;doneIf[inLIST]isnotpresent,itisreplacedwithin$@andforexecutestheCOMMANDSonceforeachpositionalparameterthatisset(seeSection3.
2.
5andSection7.
2.
1.
2).
Thereturnstatusistheexitstatusofthelastcommandthatexecutes.
IfnocommandsareexecutedbecauseLISTdoesnotexpandtoanyitems,thereturnstatusiszero.
NAMEcanbeanyvariablename,althoughiisusedveryoften.
LISTcanbeanylistofwords,stringsornumbers,whichcanbeliteralorgeneratedbyanycommand.
TheCOMMANDStoexecutecanalsobeanyoperatingsystemcommands,script,programorshellstatement.
Thefirsttimethroughtheloop,NAMEissettothefirstiteminLIST.
Thesecondtime,itsvalueissettotheseconditeminthelist,andsoon.
TheloopterminateswhenNAMEhastakenoneachofthevaluesfromLISTandnoitemsareleftinLIST.
9.
1.
2.
Examples9.
1.
2.
1.
UsingcommandsubstitutionforspecifyingLISTitemsThefirstisacommandlineexample,demonstratingtheuseofaforloopthatmakesabackupcopyofeach.
xmlfile.
Afterissuingthecommand,itissafetostartworkingonyoursources:[carol@octarine~/articles]ls*.
xmlfile1.
xmlfile2.
xmlfile3.
xml[carol@octarine~/articles]ls*.
xml>list[carol@octarine~/articles]foriin`catlist`;docp"$i""$i".
bak;done[carol@octarine~/articles]ls*.
xml*file1.
xmlfile1.
xml.
bakfile2.
xmlfile2.
xml.
bakfile3.
xmlfile3.
xml.
bakThisoneliststhefilesin/sbinthatarejustplaintextfiles,andpossiblyscripts:Chapter9.
Repetitivetasks108foriin`ls/sbin`;dofile/sbin/$i|grepASCII;done9.
1.
2.
2.
UsingthecontentofavariabletospecifyLISTitemsThefollowingisaspecificapplicationscriptforconvertingHTMLfiles,compliantwithacertainscheme,toPHPfiles.
Theconversionisdonebytakingoutthefirst25andthelast21lines,replacingthesewithtwoPHPtagsthatprovideheaderandfooterlines:[carol@octarine~/html]cathtml2php.
sh#!
/bin/bash#specificconversionscriptformyhtmlfilestophpLIST="$(ls*.
html)"foriin"$LIST";doNEWNAME=$(ls"$i"|sed-e's/html/php/')catbeginfile>"$NEWNAME"cat"$i"|sed-e'1,25d'|tac|sed-e'1,21d'|tac>>"$NEWNAME"catendfile>>"$NEWNAME"doneSincewedon'tdoalinecounthere,thereisnowayofknowingthelinenumberfromwhichtostartdeletinglinesuntilreachingtheend.
Theproblemissolvedusingtac,whichreversesthelinesinafile.
ThebasenamecommandInsteadofusingsedtoreplacethehtmlsuffixwithphp,itwouldbecleanertousethebasenamecommand.
Readthemanpageformoreinfo.
OddcharactersYouwillrunintoproblemsifthelistexpandstofilenamescontainingspacesandotherirregularcharacters.
Amoreidealconstructtoobtainthelistwouldbetousetheshell'sglobbingfeature,likethis:foriin$PATHNAME/*;docommandsdone9.
2.
Thewhileloop9.
2.
1.
WhatisitThewhileconstructallowsforrepetitiveexecutionofalistofcommands,aslongasthecommandcontrollingthewhileloopexecutessuccessfully(exitstatusofzero).
Thesyntaxis:whileCONTROL-COMMAND;doCONSEQUENT-COMMANDS;doneCONTROL-COMMANDcanbeanycommand(s)thatcanexitwithasuccessorfailurestatus.
TheCONSEQUENT-COMMANDScanbeanyprogram,scriptorshellconstruct.
AssoonastheCONTROL-COMMANDfails,theloopexits.
Inascript,thecommandfollowingthedonestatementisexecuted.
ThereturnstatusistheexitstatusofthelastCONSEQUENT-COMMANDScommand,orzeroifnonewasexecuted.
BashGuideforBeginnersChapter9.
Repetitivetasks1099.
2.
2.
Examples9.
2.
2.
1.
SimpleexampleusingwhileHereisanexamplefortheimpatient:#!
/bin/bash#Thisscriptopens4terminalwindows.
i="0"while[$i-lt4]doxterm&i=$[$i+1]done9.
2.
2.
2.
NestedwhileloopsTheexamplebelowwaswrittentocopypicturesthataremadewithawebcamtoawebdirectory.
Everyfiveminutesapictureistaken.
Everyhour,anewdirectoryiscreated,holdingtheimagesforthathour.
Everyday,anewdirectoryiscreatedcontaining24subdirectories.
Thescriptrunsinthebackground.
#!
/bin/bash#Thisscriptcopiesfilesfrommyhomedirectoryintothewebserverdirectory.
#(usescpandSSHkeysforaremotedirectory)#Anewdirectoryiscreatedeveryhour.
PICSDIR=/home/carol/picsWEBDIR=/var/www/carol/webcamwhiletrue;doDATE=`date+%Y%m%d`HOUR=`date+%H`mkdir$WEBDIR/"$DATE"while[$HOUR-ne"00"];doDESTDIR=$WEBDIR/"$DATE"/"$HOUR"mkdir"$DESTDIR"mv$PICDIR/*.
jpg"$DESTDIR"/sleep3600HOUR=`date+%H`donedoneNotetheuseofthetruestatement.
Thismeans:continueexecutionuntilweareforciblyinterrupted(withkillorCtrl+C).
Thissmallscriptcanbeusedforsimulationtesting;itgeneratesfiles:#!
/bin/bash#Thisgeneratesafileevery5minuteswhiletrue;doBashGuideforBeginnersChapter9.
Repetitivetasks110touchpic-`date+%s`.
jpgsleep300doneNotetheuseofthedatecommandtogenerateallkindsoffileanddirectorynames.
Seethemanpageformore.
UsethesystemThepreviousexampleisforthesakeofdemonstration.
Regularcheckscaneasilybeachievedusingthesystem'scronfacility.
Donotforgettoredirectoutputanderrorswhenusingscriptsthatareexecutedfromyourcrontab!
9.
2.
2.
3.
UsingkeyboardinputtocontrolthewhileloopThisscriptcanbeinterruptedbytheuserwhenaCtrl+Csequenceisentered:#!
/bin/bash#ThisscriptprovideswisdomFORTUNE=/usr/games/fortunewhiletrue;doecho"Onwhichtopicdoyouwantadvice"catbashBashGuideforBeginnersChapter9.
Repetitivetasks111#Calculatetheaverageofaseriesofnumbers.
SCORE="0"AVERAGE="0"SUM="0"NUM="0"whiletrue;doecho-n"Enteryourscore[0-100%]('q'forquit):";readSCORE;if(("$SCORE""100"));thenecho"Beserious.
Common,tryagain:"elif["$SCORE"=="q"];thenecho"Averagerating:$AVERAGE%.
"breakelseSUM=$[$SUM+$SCORE]NUM=$[$NUM+1]AVERAGE=$[$SUM/$NUM]fidoneecho"Exiting.
"Notehowthevariablesinthelastlinesareleftunquotedinordertodoarithmetic.
9.
3.
Theuntilloop9.
3.
1.
WhatisitTheuntilloopisverysimilartothewhileloop,exceptthattheloopexecutesuntiltheTEST-COMMANDexecutessuccessfully.
Aslongasthiscommandfails,theloopcontinues.
Thesyntaxisthesameasforthewhileloop:untilTEST-COMMAND;doCONSEQUENT-COMMANDS;doneThereturnstatusistheexitstatusofthelastcommandexecutedintheCONSEQUENT-COMMANDSlist,orzeroifnonewasexecuted.
TEST-COMMANDcan,again,beanycommandthatcanexitwithasuccessorfailurestatus,andCONSEQUENT-COMMANDScanbeanyUNIXcommand,scriptorshellconstruct.
Aswealreadyexplainedpreviously,the";"maybereplacedwithoneormorenewlineswhereveritappears.
9.
3.
2.
ExampleAnimprovedpicturesort.
shscript(seeSection9.
2.
2.
2),whichtestsforavailablediskspace.
Ifnotenoughdiskspaceisavailable,removepicturesfromthepreviousmonths:#!
/bin/bash#Thisscriptcopiesfilesfrommyhomedirectoryintothewebserverdirectory.
#Anewdirectoryiscreatedeveryhour.
#Ifthepicsaretakinguptoomuchspace,theoldestareremoved.
whiletrue;doBashGuideforBeginnersChapter9.
Repetitivetasks112DISKFUL=$(df-h$WEBDIR|grep-vFile|awk'{print$5}'|cut-d"%"-f1-)until[$DISKFUL-ge"90"];doDATE=`date+%Y%m%d`HOUR=`date+%H`mkdir$WEBDIR/"$DATE"while[$HOUR-ne"00"];doDESTDIR=$WEBDIR/"$DATE"/"$HOUR"mkdir"$DESTDIR"mv$PICDIR/*.
jpg"$DESTDIR"/sleep3600HOUR=`date+%H`doneDISKFULL=$(df-h$WEBDIR|grep-vFile|awk'{print$5}'|cut-d"%"-f1-)doneTOREMOVE=$(find$WEBDIR-typed-a-mtime+30)foriin$TOREMOVE;dorm-rf"$i";donedoneNotetheinitializationoftheHOURandDISKFULLvariablesandtheuseofoptionswithlsanddateinordertoobtainacorrectlistingforTOREMOVE.
9.
4.
I/Oredirectionandloops9.
4.
1.
InputredirectionInsteadofcontrollingaloopbytestingtheresultofacommandorbyuserinput,youcanspecifyafilefromwhichtoreadinputthatcontrolstheloop.
Insuchcases,readisoftenthecontrollingcommand.
Aslongasinputlinesarefedintotheloop,executionoftheloopcommandscontinues.
Assoonasalltheinputlinesarereadtheloopexits.
Sincetheloopconstructisconsideredtobeonecommandstructure(suchaswhileTEST-COMMAND;doCONSEQUENT-COMMANDS;done),theredirectionshouldoccurafterthedonestatement,sothatitcomplieswiththeformcommandbash#Thisscriptcreatesasubdirectoryinthecurrentdirectory,towhicholdBashGuideforBeginnersChapter9.
Repetitivetasks113#filesaremoved.
#Mightbesomethingforcron(ifslightlyadapted)toexecuteweeklyor#monthly.
ARCHIVENR=`date+%Y%m%d`DESTDIR="$PWD/archive-$ARCHIVENR"mkdir"$DESTDIR"#usingquotestocatchfilenamescontainingspaces,usingread-dformore#fool-proofusage:find"$PWD"-typef-a-mtime+5|whileread-d$'\000'filedogzip"$file";mv"$file".
gz"$DESTDIR"echo"$filearchived"doneFilesarecompressedbeforetheyaremovedintothearchivedirectory.
9.
5.
Breakandcontinue9.
5.
1.
Thebreakbuilt-inThebreakstatementisusedtoexitthecurrentloopbeforeitsnormalending.
Thisisdonewhenyoudon'tknowinadvancehowmanytimestheloopwillhavetoexecute,forinstancebecauseitisdependentonuserinput.
Theexamplebelowdemonstratesawhileloopthatcanbeinterrupted.
Thisisaslightlyimprovedversionofthewisdom.
shscriptfromSection9.
2.
2.
3.
#!
/bin/bash#Thisscriptprovideswisdom#Youcannowexitinadecentway.
FORTUNE=/usr/games/fortunewhiletrue;doecho"Onwhichtopicdoyouwantadvice"echo"1.
politics"echo"2.
startrek"echo"3.
kernelnewbies"echo"4.
sports"echo"5.
bofh-excuses"echo"6.
magic"echo"7.
love"echo"8.
literature"echo"9.
drugs"echo"10.
education"echoecho-n"Enteryourchoice,or0forexit:"readchoiceechocase$choicein1)$FORTUNEpoliticsBashGuideforBeginnersChapter9.
Repetitivetasks114;;2)$FORTUNEstartrek;;3)$FORTUNEkernelnewbies;;4)echo"Sportsareawasteoftime,energyandmoney.
"echo"Gobacktoyourkeyboard.
"echo-e"\t\t\t\t--\"Unhealthyismymiddlename\"Soggie.
";;5)$FORTUNEbofh-excuses;;6)$FORTUNEmagic;;7)$FORTUNElove;;8)$FORTUNEliterature;;9)$FORTUNEdrugs;;10)$FORTUNEeducation;;0)echo"OK,seeyou!
"break;;*)echo"Thatisnotavalidchoice,tryanumberfrom0to10.
";;esacdoneMindthatbreakexitstheloop,notthescript.
Thiscanbedemonstratedbyaddinganechocommandattheendofthescript.
Thisechowillalsobeexecuteduponinputthatcausesbreaktobeexecuted(whentheusertypes"0").
Innestedloops,breakallowsforspecificationofwhichlooptoexit.
SeetheBashinfopagesformore.
9.
5.
2.
Thecontinuebuilt-inThecontinuestatementresumesiterationofanenclosingfor,while,untilorselectloop.
Whenusedinaforloop,thecontrollingvariabletakesonthevalueofthenextelementinthelist.
Whenusedinawhileoruntilconstruct,ontheotherhand,executionresumeswithTEST-COMMANDatthetopoftheloop.
BashGuideforBeginnersChapter9.
Repetitivetasks1159.
5.
3.
ExamplesInthefollowingexample,filenamesareconvertedtolowercase.
Ifnoconversionneedstobedone,acontinuestatementrestartsexecutionoftheloop.
Thesecommandsdon'teatmuchsystemresources,andmostlikely,similarproblemscanbesolvedusingsedandawk.
However,itisusefultoknowaboutthiskindofconstructionwhenexecutingheavyjobs,thatmightnotevenbenecessarywhentestsareinsertedatthecorrectlocationsinascript,sparingsystemresources.
[carol@octarine~/test]cattolower.
sh#!
/bin/bash#Thisscriptconvertsallfilenamescontaininguppercasecharactersintofile#namescontaininLIST="$(ls)"fornamein"$LIST";doif[["$name"upper:thencontinuefiORIG="$name"NEW=`echo$name|tr'A-Z''a-z'`mv"$ORIG""$NEW"echo"newnamefor$ORIGis$NEW"doneThisscripthasatleastonedisadvantage:itoverwritesexistingfiles.
ThenoclobberoptiontoBashisonlyusefulwhenredirectionoccurs.
The-boptiontothemvcommandprovidesmoresecurity,butisonlysafeincaseofoneaccidentaloverwrite,asisdemonstratedinthistest:[carol@octarine~/test]rm*[carol@octarine~/test]touchtestTestTEST[carol@octarine~/test]bash-xtolower.
sh++ls+LIST=testTestTEST+[[test!
=*[[:upper:]]*]]+continue+[[Test!
=*[[:upper:]]*]]+ORIG=Test++echoTest++trA-Za-z+NEW=test+mv-bTesttest+echo'newnameforTestistest'newnameforTestistest+[[TEST!
=*[[:upper:]]*]]+ORIG=TEST++echoTEST++trA-Za-z+NEW=test+mv-bTESTtest+echo'newnameforTESTistest'newnameforTESTistestBashGuideforBeginnersChapter9.
Repetitivetasks116[carol@octarine~/test]ls-a.
/.
.
/testtest~Thetrispartofthetextutilspackage;itcanperformallkindsofcharactertransformations.
9.
6.
Makingmenuswiththeselectbuilt-in9.
6.
1.
General9.
6.
1.
1.
UseofselectTheselectconstructallowseasymenugeneration.
Thesyntaxisquitesimilartothatoftheforloop:selectWORD[inLIST];doRESPECTIVE-COMMANDS;doneLISTisexpanded,generatingalistofitems.
Theexpansionisprintedtostandarderror;eachitemisprecededbyanumber.
IfinLISTisnotpresent,thepositionalparametersareprinted,asifin$@wouldhavebeenspecified.
LISTisonlyprintedonce.
Uponprintingalltheitems,thePS3promptisprintedandonelinefromstandardinputisread.
Ifthislineconsistsofanumbercorrespondingtooneoftheitems,thevalueofWORDissettothenameofthatitem.
Ifthelineisempty,theitemsandthePS3promptaredisplayedagain.
IfanEOF(EndOfFile)characterisread,theloopexits.
Sincemostusersdon'thaveacluewhichkeycombinationisusedfortheEOFsequence,itismoreuser-friendlytohaveabreakcommandasoneoftheitems.
AnyothervalueofthereadlinewillsetWORDtobeanullstring.
ThereadlineissavedintheREPLYvariable.
TheRESPECTIVE-COMMANDSareexecutedaftereachselectionuntilthenumberrepresentingthebreakisread.
Thisexitstheloop.
9.
6.
1.
2.
ExamplesThisisaverysimpleexample,butasyoucansee,itisnotveryuser-friendly:[carol@octarinetestdir]catprivate.
sh#!
/bin/bashecho"Thisscriptcanmakeanyofthefilesinthisdirectoryprivate.
"echo"Enterthenumberofthefileyouwanttoprotect:"selectFILENAMEin*;doecho"Youpicked$FILENAME($REPLY),itisnowonlyaccessibletoyou.
"chmodgo-rwx"$FILENAME"done[carol@octarinetestdir].
/private.
shThisscriptcanmakeanyofthefilesinthisdirectoryprivate.
Enterthenumberofthefileyouwanttoprotect:1)archive-200301292)bash3)private.
sh#1BashGuideforBeginnersChapter9.
Repetitivetasks117Youpickedarchive-20030129(1)#SettingthePS3promptandaddingapossibilitytoquitmakesitbetter:#!
/bin/bashecho"Thisscriptcanmakeanyofthefilesinthisdirectoryprivate.
"echo"Enterthenumberofthefileyouwanttoprotect:"PS3="Yourchoice:"QUIT="QUITTHISPROGRAM-Ifeelsafenow.
"touch"$QUIT"selectFILENAMEin*;docase$FILENAMEin"$QUIT")echo"Exiting.
"break;;*)echo"Youpicked$FILENAME($REPLY)"chmodgo-rwx"$FILENAME";;esacdonerm"$QUIT"9.
6.
2.
SubmenusAnystatementwithinaselectconstructcanbeanotherselectloop,enabling(a)submenu(s)withinamenu.
Bydefault,thePS3variableisnotchangedwhenenteringanestedselectloop.
Ifyouwantadifferentpromptinthesubmenu,besuretosetitattheappropriatetime(s).
9.
7.
Theshiftbuilt-in9.
7.
1.
WhatdoesitdoTheshiftcommandisoneoftheBourneshellbuilt-insthatcomeswithBash.
Thiscommandtakesoneargument,anumber.
Thepositionalparametersareshiftedtotheleftbythisnumber,N.
ThepositionalparametersfromN+1to$#arerenamedtovariablenamesfrom$1to$#-N+1.
Sayyouhaveacommandthattakes10arguments,andNis4,then$4becomes$1,$5becomes$2andsoon.
$10becomes$7andtheoriginal$1,$2and$3arethrownaway.
IfNiszeroorgreaterthan$#,thepositionalparametersarenotchanged(thetotalnumberofarguments,seeSection7.
2.
1.
2)andthecommandhasnoeffect.
IfNisnotpresent,itisassumedtobe1.
ThereturnstatusiszerounlessNisgreaterthan$#orlessthanzero;otherwiseitisnon-zero.
BashGuideforBeginnersChapter9.
Repetitivetasks1189.
7.
2.
ExamplesAshiftstatementistypicallyusedwhenthenumberofargumentstoacommandisnotknowninadvance,forinstancewhenuserscangiveasmanyargumentsastheylike.
Insuchcases,theargumentsareusuallyprocessedinawhileloopwithatestconditionofThisconditionistrueaslongasthenumberofargumentsisgreaterthanzero.
The$1variableandtheshiftstatementprocesseachargument.
Thenumberofargumentsisreducedeachtimeshiftisexecutedandeventuallybecomeszero,uponwhichthewhileloopexits.
Theexamplebelow,cleanup.
sh,usesshiftstatementstoprocesseachfileinthelistgeneratedbyfind:#!
/bin/bash#Thisscriptcancleanupfilesthatwerelastaccessedover365daysago.
USAGE="Usage:$0dir1dir2dir3.
.
.
dirN"if0"];thenecho"$USAGE"exit1fiwhiledoif[[$(ls"$1"thenecho"Emptydirectory,nothingtobedone.
"elsefind"$1"-typef-a-atime+365-execrm-i{}\;fishiftdone-execvs.
xargsTheabovefindcommandcanbereplacedwiththefollowing:findoptions|xargs[commands_to_execute_on_found_files]Thexargscommandbuildsandexecutescommandlinesfromstandardinput.
Thishastheadvantagethatthecommandlineisfilleduntilthesystemlimitisreached.
Onlythenwillthecommandtoexecutebecalled,intheaboveexamplethiswouldberm.
Iftherearemorearguments,anewcommandlinewillbeused,untilthatoneisfulloruntiltherearenomorearguments.
Thesamethingusingfind-execcallsonthecommandtoexecuteonthefoundfileseverytimeafileisfound.
Thus,usingxargsgreatlyspeedsupyourscriptsandtheperformanceofyourmachine.
Inthenextexample,wemodifiedthescriptfromSection8.
2.
4.
4sothatitacceptsmultiplepackagestoinstallatonce:#!
/bin/bashif[$#-lt1];thenecho"Usage:$0package(s)"exit1fiwhile(($#));doyuminstall"$1"BashGuideforBeginnersChapter9.
Repetitivetasks119yCONFIRMshiftdone9.
8.
SummaryInthischapter,wediscussedhowrepetitivecommandscanbeincorporatedinloopconstructs.
Mostcommonloopsarebuiltusingthefor,whileoruntilstatements,oracombinationofthesecommands.
Theforloopexecutesataskadefinednumberoftimes.
Ifyoudon'tknowhowmanytimesacommandshouldexecute,useeitheruntilorwhiletospecifywhentheloopshouldend.
Loopscanbeinterruptedorreiteratedusingthebreakandcontinuestatements.
Afilecanbeusedasinputforaloopusingtheinputredirectionoperator,loopscanalsoreadoutputfromcommandsthatisfedintotheloopusingapipe.
Theselectconstructisusedforprintingmenusininteractivescripts.
Loopingthroughthecommandlineargumentstoascriptcanbedoneusingtheshiftstatement.
9.
9.
ExercisesRemember:whenbuildingscripts,workinstepsandtesteachstepbeforeincorporatingitinyourscript.
Createascriptthatwilltakea(recursive)copyoffilesin/etcsothatabeginningsystemadministratorcaneditfileswithoutfear.
1.
Writeascriptthattakesexactlyoneargument,adirectoryname.
Ifthenumberofargumentsismoreorlessthanone,printausagemessage.
Iftheargumentisnotadirectory,printanothermessage.
Forthegivendirectory,printthefivebiggestfilesandthefivefilesthatweremostrecentlymodified.
2.
CanyouexplainwhyitissoimportanttoputthevariablesinbetweendoublequotesintheexamplefromSection9.
4.
23.
WriteascriptsimilartotheoneinSection9.
5.
1,butthinkofawayofquittingaftertheuserhasexecuted3loops.
4.
Thinkofabettersolutionthanmove-bforthescriptfromSection9.
5.
3topreventoverwritingofexistingfiles.
Forinstance,testwhetherornotafileexists.
Don'tdounnecessarywork!
5.
Rewritethewhichdaemon.
shscriptfromSection7.
2.
4,sothatit:Printsalistofserverstocheck,suchasApache,theSSHserver,theNTPdaemon,anamedaemon,apowermanagementdaemon,andsoon.
Foreachchoicetheusercanmake,printsomesensibleinformation,likethenameofthewebserver,NTPtraceinformation,andsoon.
Optionally,buildinapossibilityforuserstocheckotherserversthantheoneslisted.
Forsuchcases,checkthatatleastthegivenprocessisrunning.
ReviewthescriptfromSection9.
2.
2.
4.
Notehowcharacterinputotherthanqisprocessed.
Rebuildthisscriptsothatitprintsamessageifcharactersaregivenasinput.
6.
BashGuideforBeginnersChapter9.
Repetitivetasks120Chapter10.
MoreonvariablesInthischapter,wewilldiscusstheadvanceduseofvariablesandarguments.
Uponcompletion,youwillbeableto:DeclareanduseanarrayofvariablesSpecifythesortofvariableyouwanttouseMakevariablesread-onlyUsesettoassignavaluetoavariable10.
1.
Typesofvariables10.
1.
1.
GeneralassignmentofvaluesAswealreadysaw,Bashunderstandsmanydifferentkindsofvariablesorparameters.
Thusfar,wehaven'tbotheredmuchwithwhatkindofvariablesweassigned,soourvariablescouldholdanyvaluethatweassignedtothem.
Asimplecommandlineexampledemonstratesthis:[bobin~]VARIABLE=12[bobin~]echo$VARIABLE12[bobin~]VARIABLE=string[bobin~]echo$VARIABLEstringTherearecaseswhenyouwanttoavoidthiskindofbehavior,forinstancewhenhandlingtelephoneandothernumbers.
Apartfromintegersandvariables,youmayalsowanttospecifyavariablethatisaconstant.
Thisisoftendoneatthebeginningofascript,whenthevalueoftheconstantisdeclared.
Afterthat,thereareonlyreferencestotheconstantvariablename,sothatwhentheconstantneedstobechanged,itonlyhastobedoneonce.
Avariablemayalsobeaseriesofvariablesofanytype,aso-calledarrayofvariables(VAR0VAR1,VAR2,.
.
.
VARN).
10.
1.
2.
Usingthedeclarebuilt-inUsingadeclarestatement,wecanlimitthevalueassignmenttovariables.
Thesyntaxfordeclareisthefollowing:declareOPTION(s)VARIABLE=valueThefollowingoptionsareusedtodeterminethetypeofdatathevariablecanholdandtoassignitattributes:Table10-1.
Optionstothedeclarebuilt-inOptionMeaningChapter10.
Moreonvariables121-aVariableisanarray.
-fUsefunctionnamesonly.
-iThevariableistobetreatedasaninteger;arithmeticevaluationisperformedwhenthevariableisassignedavalue(seeSection3.
4.
6).
-pDisplaytheattributesandvaluesofeachvariable.
When-pisused,additionaloptionsareignored.
-rMakevariablesread-only.
Thesevariablescannotthenbeassignedvaluesbysubsequentassignmentstatements,norcantheybeunset.
-tGiveeachvariablethetraceattribute.
-xMarkeachvariableforexporttosubsequentcommandsviatheenvironment.
Using+insteadof-turnsofftheattributeinstead.
Whenusedinafunction,declarecreateslocalvariables.
Thefollowingexampleshowshowassignmentofatypetoavariableinfluencesthevalue.
[bobin~]declare-iVARIABLE=12[bobin~]VARIABLE=string[bobin~]echo$VARIABLE0[bobin~]declare-pVARIABLEdeclare-iVARIABLE="0"NotethatBashhasanoptiontodeclareanumericvalue,butnonefordeclaringstringvalues.
Thisisbecause,bydefault,ifnospecificationsaregiven,avariablecanholdanytypeofdata:[bobin~]OTHERVAR=blah[bobin~]declare-pOTHERVARdeclare--OTHERVAR="blah"Assoonasyourestrictassignmentofvaluestoavariable,itcanonlyholdthattypeofdata.
Possiblerestrictionsareeitherinteger,constantorarray.
SeetheBashinfopagesforinformationonreturnstatus.
10.
1.
3.
ConstantsInBash,constantsarecreatedbymakingavariableread-only.
Thereadonlybuilt-inmarkseachspecifiedvariableasunchangeable.
Thesyntaxis:readonlyOPTIONVARIABLE(s)Thevaluesofthesevariablescanthennolongerbechangedbysubsequentassignment.
Ifthe-foptionisgiven,eachvariablereferstoashellfunction;seeChapter11.
If-aisspecified,eachvariablereferstoanarrayofvariables.
Ifnoargumentsaregiven,orif-pissupplied,alistofallread-onlyvariablesisdisplayed.
Usingthe-poption,theoutputcanbereusedasinput.
Thereturnstatusiszero,unlessaninvalidoptionwasspecified,oneofthevariablesorfunctionsdoesnotexist,or-fwassuppliedforavariablenameinsteadofforafunctionname.
[bobin~]readonlyTUX=penguinpowerBashGuideforBeginnersChapter10.
Moreonvariables122[bobin~]TUX=Mickeysoftbash:TUX:readonlyvariable10.
2.
Arrayvariables10.
2.
1.
CreatingarraysAnarrayisavariablecontainingmultiplevalues.
Anyvariablemaybeusedasanarray.
Thereisnomaximumlimittothesizeofanarray,noranyrequirementthatmembervariablesbeindexedorassignedcontiguously.
Arraysarezero-based:thefirstelementisindexedwiththenumber0.
Indirectdeclarationisdoneusingthefollowingsyntaxtodeclareavariable:ARRAY[INDEXNR]=valueTheINDEXNRistreatedasanarithmeticexpressionthatmustevaluatetoapositivenumber.
Explicitdeclarationofanarrayisdoneusingthedeclarebuilt-in:declare-aARRAYNAMEAdeclarationwithanindexnumberwillalsobeaccepted,buttheindexnumberwillbeignored.
Attributestothearraymaybespecifiedusingthedeclareandreadonlybuilt-ins.
Attributesapplytoallvariablesinthearray;youcan'thavemixedarrays.
Arrayvariablesmayalsobecreatedusingcompoundassignmentsinthisformat:ARRAY=(value1value2.
.
.
valueN)Eachvalueisthenintheformof[indexnumber=]string.
Theindexnumberisoptional.
Ifitissupplied,thatindexisassignedtoit;otherwisetheindexoftheelementassignedisthenumberofthelastindexthatwasassigned,plusone.
Thisformatisacceptedbydeclareaswell.
Ifnoindexnumbersaresupplied,indexingstartsatzero.
Addingmissingorextramembersinanarrayisdoneusingthesyntax:ARRAYNAME[indexnumber]=valueRememberthatthereadbuilt-inprovidesthe-aoption,whichallowsforreadingandassigningvaluesformembervariablesofanarray.
10.
2.
2.
DereferencingthevariablesinanarrayInordertorefertothecontentofaniteminanarray,usecurlybraces.
Thisisnecessary,asyoucanseefromthefollowingexample,tobypasstheshellinterpretationofexpansionoperators.
Iftheindexnumberis@or*,allmembersofanarrayarereferenced.
[bobin~]ARRAY=(onetwothree)[bobin~]echo${ARRAY[*]}BashGuideforBeginnersChapter10.
Moreonvariables123onetwothree[bobin~]echo$ARRAY[*]one[*][bobin~]echo${ARRAY[2]}three[bobin~]ARRAY[3]=four[bobin~]echo${ARRAY[*]}onetwothreefourReferringtothecontentofamembervariableofanarraywithoutprovidinganindexnumberisthesameasreferringtothecontentofthefirstelement,theonereferencedwithindexnumberzero.
10.
2.
3.
DeletingarrayvariablesTheunsetbuilt-inisusedtodestroyarraysormembervariablesofanarray:[bobin~]unsetARRAY[1][bobin~]echo${ARRAY[*]}onethreefour[bobin~]unsetARRAY[bobin~]echo${ARRAY[*]}10.
2.
4.
ExamplesofarraysPracticalexamplesoftheusageofarraysarehardtofind.
Youwillfindplentyofscriptsthatdon'treallydoanythingonyoursystembutthatdousearraystocalculatemathematicalseries,forinstance.
Andthatwouldbeoneofthemoreinterestingexamples.
.
.
mostscriptsjustshowwhatyoucandowithanarrayinanoversimplifiedandtheoreticalway.
Thereasonforthisdullnessisthatarraysarerathercomplexstructures.
Youwillfindthatmostpracticalexamplesforwhicharrayscouldbeusedarealreadyimplementedonyoursystemusingarrays,howeveronalowerlevel,intheCprogramminglanguageinwhichmostUNIXcommandsarewritten.
AgoodexampleistheBashhistorybuilt-incommand.
Thosereaderswhoareinterestedmightcheckthebuilt-insdirectoryintheBashsourcetreeandtakealookatfc.
def,whichisprocessedwhencompilingthebuilt-ins.
Anotherreasongoodexamplesarehardtofindisthatnotallshellssupportarrays,sotheybreakcompatibility.
Afterlongdaysofsearching,IfinallyfoundthisexampleoperatingatanInternetprovider.
ItdistributesApachewebserverconfigurationfilesontohostsinawebfarm:#!
/bin/bashif[$(whoami)!
='root'];thenecho"Mustberoottorun$0"exit1;fiBashGuideforBeginnersChapter10.
Moreonvariables124if[-z$1];thenecho"Usage:$0"exit1fihttpd_conf_new=$1httpd_conf_path="/usr/local/apache/conf"login=htuserfarm_hosts=(web03web04web05web06web07)foriin${farm_hosts[@]};dosu$login-c"scp$httpd_conf_new${i}:${httpd_conf_path}"su$login-c"ssh$isudo/usr/local/apache/bin/apachectlgraceful"doneexit0Firsttwotestsareperformedtocheckwhetherthecorrectuserisrunningthescriptwiththecorrectarguments.
Thenamesofthehoststhatneedtobeconfiguredarelistedinthearrayfarm_hosts.
ThenallthesehostsareprovidedwiththeApacheconfigurationfile,afterwhichthedaemonisrestarted.
NotetheuseofcommandsfromtheSecureShellsuite,encryptingtheconnectionstoremotehosts.
Thanks,Eugeneandcolleague,forthiscontribution.
DanRichtercontributedthefollowingexample.
Thisistheproblemhewasconfrontedwith:".
.
.
Inmycompany,wehavedemosonourwebsite,andeveryweeksomeonehastotestallofthem.
SoIhaveacronjobthatfillsanarraywiththepossiblecandidates,usesdate+%Wtofindtheweekoftheyear,anddoesamodulooperationtofindthecorrectindex.
Theluckypersongetsnotifiedbye-mail.
"Andthiswashiswayofsolvingit:#!
/bin/bash#Thisisget-tester-address.
sh##First,wetestwhetherbashsupportsarrays.
#(Supportforarrayswasonlyaddedrecently.
)#whotest[0]='test'||(echo'Failure:arraysnotsupportedinthisversionofbash.
'&&exit2)##Ourlistofcandidates.
(Feelfreetoaddor#removecandidates.
)#wholist=('BobSmith''JaneL.
Williams''EricS.
Raymond''LarryWall''LinusTorvalds')##Countthenumberofpossibletesters.
#(Loopuntilwefindanemptystring.
)#count=0while["x${wholist[count]}"!
="x"]BashGuideforBeginnersChapter10.
Moreonvariables125docount=$(($count+1))done##Nowwecalculatewhoseturnitis.
#week=`date'+%W'`#Theweekoftheyear(0.
.
53).
week=${week#0}#Removepossibleleadingzero.
let"index=$week%$count"#weekmodulocount=theluckypersonemail=${wholist[index]}#Gettheluckyperson'se-mailaddress.
echo$email#Outputtheperson'se-mailaddress.
Thisscriptisthenusedinotherscripts,suchasthisone,whichusesaheredocument:email=`get-tester-address.
sh`#Findwhotoe-mail.
hostname=`hostname`#Thismachine'sname.
##Sende-mailtotherightperson.
#mail$email-s'[DemoTesting]'Orlikethisexamplefromthe/etc/init.
d/amdscript,whereBash'soptimizationfeaturesareused:[$RETVAL=0]&&touch/var/lock/subsys/amdBashGuideforBeginnersChapter11.
Functions132Thecommandsafter&&areonlyexecutedwhenthetestprovestobetrue;thisisashorterwaytorepresentanif/then/fistructure.
Thereturncodeofthefunctionisoftenusedasexitcodeoftheentirescript.
You'llseealotofinitscriptsendinginsomethinglikeexit$RETVAL.
11.
1.
4.
DisplayingfunctionsAllfunctionsknownbythecurrentshellcanbedisplayedusingthesetbuilt-inwithoutoptions.
Functionsareretainedaftertheyareused,unlesstheyareunsetafteruse.
Thewhichcommandalsodisplaysfunctions:[lydia@cointreau~]whichzlesszlessisafunctionzless(){zcatPAGER"}[lydia@cointreau~]echo$PAGERlessThisisthesortoffunctionthatistypicallyconfiguredintheuser'sshellresourceconfigurationfiles.
Functionsaremoreflexiblethanaliasesandprovideasimpleandeasywayofadaptingtheuserenvironment.
Here'soneforDOSusers:dir(){ls-F--color=auto-lF--color=always"$@"|less-r}11.
2.
Examplesoffunctionsinscripts11.
2.
1.
RecyclingThereareplentyofscriptsonyoursystemthatusefunctionsasastructuredwayofhandlingseriesofcommands.
OnsomeLinuxsystems,forinstance,youwillfindthe/etc/rc.
d/init.
d/functionsdefinitionfile,whichissourcedinallinitscripts.
Usingthismethod,commontaskssuchascheckingifaprocessruns,startingorstoppingadaemonandsoon,onlyhavetobewrittenonce,inageneralway.
Ifthesametaskisneededagain,thecodeisrecycled.
Youcouldmakeyourown/etc/functionsfilethatcontainsallfunctionsthatyouuseregularlyonyoursystem,indifferentscripts.
Justputtheline.
/etc/functionssomewhereatthestartofthescriptandyoucanrecyclefunctions.
BashGuideforBeginnersChapter11.
Functions13311.
2.
2.
SettingthepathThissectionmightbefoundinyour/etc/profilefile.
Thefunctionpathmungeisdefinedandthenusedtosetthepathfortherootandotherusers:pathmunge(){if!
echo$PATH|/bin/egrep-q"(^|:)$1(thenif["$2"="after"];thenPATH=$PATH:$1elsePATH=$1:$PATHfifi}#Pathmanipulationif[`id-u`=0];thenpathmunge/sbinpathmunge/usr/sbinpathmunge/usr/local/sbinfipathmunge/usr/X11R6/binafterunsetpathmungeThefunctiontakesitsfirstargumenttobeapathname.
Ifthispathnameisnotyetinthecurrentpath,itisadded.
ThesecondargumenttothefunctiondefinesifthepathwillbeaddedinfrontorafterthecurrentPATHdefinition.
Normalusersonlyget/usr/X11R6/binaddedtotheirpaths,whilerootgetsacoupleofextradirectoriescontainingsystemcommands.
Afterbeingused,thefunctionisunsetsothatitisnotretained.
11.
2.
3.
RemotebackupsThefollowingexampleisonethatIuseformakingbackupsofthefilesformybooks.
ItusesSSHkeysforenablingtheremoteconnection.
Twofunctionsaredefined,buplinuxandbupbash,thateachmakea.
tarfile,whichisthencompressedandsenttoaremoteserver.
Afterthat,thelocalcopyiscleanedup.
OnSunday,onlybupbashisexecuted.
#/bin/bashLOGFILE="/nethome/tille/log/backupscript.
log"echo"Startingbackupsfor`date`">>"$LOGFILE"buplinux(){DIR="/nethome/tille/xml/db/linux-basics/"TAR="Linux.
tar"BZIP="$TAR.
bz2"SERVER="rincewind"RDIR="/var/www/intra/tille/html/training/"cd"$DIR"tarcf"$TAR"src/*.
xmlsrc/images/*.
pngsrc/images/*.
epsecho"Compressing$TAR.
LOGFILE"BashGuideforBeginnersChapter11.
Functions134bzip2"$TAR"echo".
.
.
done.
">>"$LOGFILE"echo"Copyingto$SERVER.
LOGFILE"scp"$BZIP""$SERVER:$RDIR">/dev/null2>&1echo".
.
.
done.
">>"$LOGFILE"echo-e"DonebackingupLinuxcourse:\nSourcefiles,PNGandEPSimages.
\nRubbishremoved.
">>"rm"$BZIP"}bupbash(){DIR="/nethome/tille/xml/db/"TAR="Bash.
tar"BZIP="$TAR.
bz2"FILES="bash-programming/"SERVER="rincewind"RDIR="/var/www/intra/tille/html/training/"cd"$DIR"tarcf"$TAR""$FILES"echo"Compressing$TAR.
LOGFILE"bzip2"$TAR"echo".
.
.
done.
">>"$LOGFILE"echo"Copyingto$SERVER.
LOGFILE"scp"$BZIP""$SERVER:$RDIR">/dev/null2>&1echo".
.
.
done.
">>"$LOGFILE"echo-e"DonebackingupBashcourse:\n$FILES\nRubbishremoved.
">>"$LOGFILE"rm"$BZIP"}DAY=`date+%w`if["$DAY"-lt"2"];thenecho"Itis`date+%A`,onlybackingupBashcourse.
">>"$LOGFILE"bupbashelsebuplinuxbupbashfiecho-e"Remotebackup`date`SUCCESS\n-LOGFILE"Thisscriptrunsfromcron,meaningwithoutuserinteraction,soweredirectstandarderrorfromthescpcommandto/dev/null.
Itmightbearguedthatalltheseparatestepscanbecombinedinacommandsuchastarcdir_to_backup/|bzip2|sshserver"cat>backup.
tar.
bz2"However,ifyouareinterestedinintermediateresults,whichmightberecovereduponfailureofthescript,thisisnotwhatyouwant.
Theexpressioncommand&>fileisequivalenttoBashGuideforBeginnersChapter11.
Functions135command>file2>&111.
3.
SummaryFunctionsprovideaneasywayofgroupingcommandsthatyouneedtoexecuterepetitively.
Whenafunctionisrunning,thepositionalparametersarechangedtothoseofthefunction.
Whenitstops,theyareresettothoseofthecallingprogram.
Functionsarelikemini-scripts,andjustlikeascript,theygenerateexitorreturncodes.
Whilethiswasashortchapter,itcontainsimportantknowledgeneededforachievingtheultimatestateoflazinessthatisthetypicalgoalofanysystemadministrator.
11.
4.
ExercisesHerearesomeusefulthingsyoucandousingfunctions:Addafunctiontoyour~/.
bashrcconfigfilethatautomatestheprintingofmanpages.
Theresultshouldbethatyoutypesomethinglikeprintman,uponwhichthefirstappropriatemanpagerollsoutofyourprinter.
Checkusingapseudoprinterdevicefortestingpurposes.
Asanextra,buildinapossibilityfortheusertosupplythesectionnumberofthemanpageheorshewantstoprint.
1.
Createasubdirectoryinyourhomedirectoryinwhichyoucanstorefunctiondefinitions.
Putacoupleoffunctionsinthatdirectory.
Usefulfunctionsmightbe,amongsothers,thatyouhavethesamecommandsasonDOSoracommercialUNIXwhenworkingwithLinux,orviceversa.
Thesefunctionsshouldthenbeimportedinyourshellenvironmentwhen~/.
bashrcisread.
2.
BashGuideforBeginnersChapter11.
Functions136Chapter12.
CatchingsignalsInthischapter,wewilldiscussthefollowingsubjects:AvailablesignalsUseofthesignalsUseofthetrapstatementHowtopreventusersfrominterruptingyourprograms12.
1.
Signals12.
1.
1.
Introduction12.
1.
1.
1.
FindingthesignalmanpageYoursystemcontainsamanpagelistingalltheavailablesignals,butdependingonyouroperatingsystem,itmightbeopenedinadifferentway.
OnmostLinuxsystems,thiswillbeman7signal.
Whenindoubt,locatetheexactmanpageandsectionusingcommandslikeman-ksignal|greplistorapropossignal|greplistSignalnamescanbefoundusingkill-l.
12.
1.
1.
2.
SignalstoyourBashshellIntheabsenceofanytraps,aninteractiveBashshellignoresSIGTERMandSIGQUIT.
SIGINTiscaughtandhandled,andifjobcontrolisactive,SIGTTIN,SIGTTOUandSIGTSTParealsoignored.
Commandsthatarerunastheresultofacommandsubstitutionalsoignorethesesignals,whenkeyboardgenerated.
SIGHUPbydefaultexitsashell.
AninteractiveshellwillsendaSIGHUPtoalljobs,runningorstopped;seethedocumentationonthedisownbuilt-inifyouwanttodisablethisdefaultbehaviorforaparticularprocess.
UsethehuponexitoptionforkillingalljobsuponreceivingaSIGHUPsignal,usingtheshoptbuilt-in.
12.
1.
1.
3.
SendingsignalsusingtheshellThefollowingsignalscanbesentusingtheBashshell:Table12-1.
ControlsignalsinBashStandardkeycombinationMeaningCtrl+CTheinterruptsignal,sendsSIGINTtothejobrunningintheforeground.
Chapter12.
Catchingsignals137Ctrl+YThedelayedsuspendcharacter.
Causesarunningprocesstobestoppedwhenitattemptstoreadinputfromtheterminal.
Controlisreturnedtotheshell,theusercanforeground,backgroundorkilltheprocess.
Delayedsuspendisonlyavailableonoperatingsystemssupportingthisfeature.
Ctrl+ZThesuspendsignal,sendsaSIGTSTPtoarunningprogram,thusstoppingitandreturningcontroltotheshell.
TerminalsettingsCheckyoursttysettings.
Suspendandresumeofoutputisusuallydisabledifyouareusing"modern"terminalemulations.
ThestandardxtermsupportsCtrl+SandCtrl+Qbydefault.
12.
1.
2.
UsageofsignalswithkillMostmodernshells,Bashincluded,haveabuilt-inkillfunction.
InBash,bothsignalnamesandnumbersareacceptedasoptions,andargumentsmaybejoborprocessIDs.
Anexitstatuscanbereportedusingthe-loption:zerowhenatleastonesignalwassuccessfullysent,non-zeroifanerroroccurred.
Usingthekillcommandfrom/usr/bin,yoursystemmightenableextraoptions,suchastheabilitytokillprocessesfromotherthanyourownuserIDandspecifyingprocessesbyname,likewithpgrepandpkill.
BothkillcommandssendtheTERMsignalifnoneisgiven.
Thisisalistofthemostcommonsignals:Table12-2.
CommonkillsignalsSignalnameSignalvalueEffectSIGHUP1HangupSIGINT2InterruptfromkeyboardSIGKILL9KillsignalSIGTERM15TerminationsignalSIGSTOP17,19,23StoptheprocessSIGKILLandSIGSTOPSIGKILLandSIGSTOPcannotbecaught,blockedorignored.
Whenkillingaprocessorseriesofprocesses,itiscommonsensetostarttryingwiththeleastdangeroussignal,SIGTERM.
Thatway,programsthatcareaboutanorderlyshutdowngetthechancetofollowtheproceduresthattheyhavebeendesignedtoexecutewhengettingtheSIGTERMsignal,suchascleaningupandclosingopenfiles.
IfyousendaSIGKILLtoaprocess,youremoveanychancefortheprocesstodoatidycleanupandshutdown,whichmighthaveunfortunateconsequences.
Butifacleanterminationdoesnotwork,theINTorKILLsignalsmightbetheonlyway.
Forinstance,whenaprocessdoesnotdieusingCtrl+C,itisbesttousethekill-9onthatprocessID:maud:~>ps-ef|grepstuck_processmaud56072214020:05pts/500:00:02stuck_processBashGuideforBeginnersChapter12.
Catchingsignals138maud:~>kill-95607maud:~>ps-ef|grepstuck_processmaud56142214020:15pts/500:00:00grepstuck_process[1]+Killedstuck_processWhenaprocessstartsupseveralinstances,killallmightbeeasier.
Ittakesthesameoptionasthekillcommand,butappliesonallinstancesofagivenprocess.
Testthiscommandbeforeusingitinaproductionenvironment,sinceitmightnotworkasexpectedonsomeofthecommercialUnices.
12.
2.
Traps12.
2.
1.
GeneralTheremightbesituationswhenyoudon'twantusersofyourscriptstoexituntimelyusingkeyboardabortsequences,forexamplebecauseinputhastobeprovidedorcleanuphastobedone.
Thetrapstatementcatchesthesesequencesandcanbeprogrammedtoexecutealistofcommandsuponcatchingthosesignals.
Thesyntaxforthetrapstatementisstraightforward:trap[COMMANDS][SIGNALS]ThisinstructsthetrapcommandtocatchthelistedSIGNALS,whichmaybesignalnameswithorwithouttheSIGprefix,orsignalnumbers.
Ifasignalis0orEXIT,theCOMMANDSareexecutedwhentheshellexits.
IfoneofthesignalsisDEBUG,thelistofCOMMANDSisexecutedaftereverysimplecommand.
AsignalmayalsobespecifiedasERR;inthatcaseCOMMANDSareexecutedeachtimeasimplecommandexitswithanon-zerostatus.
Notethatthesecommandswillnotbeexecutedwhenthenon-zeroexitstatuscomesfrompartofanifstatement,orfromawhileoruntilloop.
NeitherwilltheybeexecutedifalogicalAND(&&)orOR(||)resultinanon-zeroexitcode,orwhenacommand'sreturnstatusisinvertedusingthe!
operator.
Thereturnstatusofthetrapcommanditselfiszerounlessaninvalidsignalspecificationisencountered.
Thetrapcommandtakesacoupleofoptions,whicharedocumentedintheBashinfopages.
Hereisaverysimpleexample,catchingCtrl+Cfromtheuser,uponwhichamessageisprinted.
WhenyoutrytokillthisprogramwithoutspecifyingtheKILLsignal,nothingwillhappen:#!
/bin/bash#traptest.
shtrap"echoBooh!
"SIGINTSIGTERMecho"pidis$$"while:#Thisisthesameas"whiletrue".
dosleep60#Thisscriptisnotreallydoinganything.
done12.
2.
2.
HowBashinterpretstrapsWhenBashreceivesasignalforwhichatraphasbeensetwhilewaitingforacommandtocomplete,thetrapwillnotbeexecuteduntilthecommandcompletes.
WhenBashiswaitingforanasynchronouscommandviathewaitbuilt-in,thereceptionofasignalforwhichatraphasbeensetwillcausethewaitbuilt-intoreturnBashGuideforBeginnersChapter12.
Catchingsignals139immediatelywithanexitstatusgreaterthan128,immediatelyafterwhichthetrapisexecuted.
12.
2.
3.
Moreexamples12.
2.
3.
1.
DetectingwhenavariableisusedWhendebugginglongerscripts,youmightwanttogiveavariablethetraceattributeandtrapDEBUGmessagesforthatvariable.
NormallyyouwouldjustdeclareavariableusinganassignmentlikeVARIABLE=value.
Replacingthedeclarationofthevariablewiththefollowinglinesmightprovidevaluableinformationaboutwhatyourscriptisdoing:declare-tVARIABLE=valuetrap"echoVARIABLEisbeingusedhere.
"DEBUG#restofthescript12.
2.
3.
2.
RemovingrubbishuponexitThewhatiscommandreliesonadatabasewhichisregularlybuiltusingthemakewhatis.
cronscriptwithcron:#!
/bin/bashLOCKFILE=/var/lock/makewhatis.
lock#Previousmakewhatisshouldexecutesuccessfully:[-f$LOCKFILE]&&exit0#Uponexit,removelockfile.
trap"{rm-f$LOCKFILE;exit255;}"EXITtouch$LOCKFILEmakewhatis-u-wexit012.
3.
SummarySignalscanbesenttoyourprogramsusingthekillcommandorkeyboardshortcuts.
Thesesignalscanbecaught,uponwhichactioncanbeperformed,usingthetrapstatement.
Someprogramsignoresignals.
TheonlysignalthatnoprogramcanignoreistheKILLsignal.
12.
4.
ExercisesAcoupleofpracticalexamples:Createascriptthatwritesabootimagetoadisketteusingtheddutility.
IftheusertriestointerruptthescriptusingCtrl+C,displayamessagethatthisactionwillmakethedisketteunusable.
1.
BashGuideforBeginnersChapter12.
Catchingsignals140Writeascriptthatautomatestheinstallationofathird-partypackageofyourchoice.
ThepackagemustbedownloadedfromtheInternet.
Itmustbedecompressed,unarchivedandcompilediftheseactionsareappropriate.
Onlytheactualinstallationofthepackageshouldbeuninterruptable.
2.
BashGuideforBeginnersChapter12.
Catchingsignals141AppendixA.
ShellFeaturesThisdocumentgivesanoverviewofcommonshellfeatures(thesameineveryshellflavour)anddifferingshellfeatures(shellspecificfeatures).
A.
1.
CommonfeaturesThefollowingfeaturesarestandardineveryshell.
Notethatthestop,suspend,jobs,bgandfgcommandsareonlyavailableonsystemsthatsupportjobcontrol.
TableA-1.
CommonShellFeaturesCommandMeaning>Redirectoutput>>Appendtofilebash),Kornshell(ksh)andtheCshell(csh).
ShellcompatibilitySincetheBourneAgainSHellisasupersetofsh,allshcommandswillalsoworkinbash-butnotviceversa.
bashhasmanymorefeaturesofitsown,and,asthetablebelowdemonstrates,manyfeaturesincorporatedfromothershells.
SincetheTurboCshellisasupersetofcsh,allcshcommandswillworkintcsh,butnottheotherwayround.
TableA-2.
DifferingShellFeaturesshbashkshcshMeaning/Action$$$%DefaultuserpromptForceredirection>file2>&1&>fileor>file2>&1>file2>&1>&fileRedirectstdoutandstderrtofile{}{}Expandelementsinlist`command``command`or$(command)$(command)`command`Substituteoutputofenclosedcommand$HOME$HOME$HOME$homeHomedirectory~~~Homedirectorysymbol~+,~-,dirsNAccessdirectorystackvar=valueVAR=valuevar=valuesetvar=valueVariableassignmentBashGuideforBeginnersAppendixA.
ShellFeatures143exportvarexportVAR=valueexportvar=valsetenvvarvalSetenvironmentvariable${nnnn}${nn}Morethan9argumentscanbereferencedAllargumentsasseparatewordsargvNumberofargumentsstatusExitstatusofthemostrecentlyexecutedcommand$!
$!
$!
PIDofmostrecentlybackgroundedprocessCurrentoptions.
filesourcefileor.
file.
filesourcefileReadcommandsinfilealiasx='y'aliasx=yaliasxyNamexstandsforcommandycasecasecaseswitchorcaseChoosealternativesdonedonedoneendEndaloopstatementesacesacesacendswEndcaseorswitchexitnexitnexitnexit(expr)Exitwithastatusfor/dofor/dofor/doforeachLoopthroughvariablesset-f,set-onullglob|dotglob|nocaseglob|noglobnoglobIgnoresubstitutioncharactersforfilenamegenerationhashhashalias-thashstatDisplayhashedcommands(trackedaliases)hashcmdshashcmdsalias-tcmdsrehashRemembercommandlocationshash-rhash-runhashForgetcommandlocationshistoryhistoryhistoryListpreviouscommandsBashGuideforBeginnersAppendixA.
ShellFeatures144ArrowUp+Enteror!
!
r!
!
Redopreviouscommand!
strrstr!
strRedolastcommandthatstartswith"str"!
cmd:s/x/y/rx=ycmd!
cmd:s/x/y/Replace"x"with"y"inmostrecentcommandstartingwith"cmd",thenexecute.
if[$i-eq5]if[$i-eq5]if((i==5))if($i==5)SampleconditiontestfififiendifEndifstatementulimitulimitulimitlimitSetresourcelimitspwdpwdpwddirsPrintworkingdirectoryreadreadread$bash.
MoreinformationonfeaturesfoundonlyinBashcanberetrievedfromtheBashinfopages,inthe"BashFeatures"section.
Moreinformation:Youshouldatleastreadonemanual,beingthemanualofyourshell.
Thepreferredchoicewouldbeinfobash,bashbeingtheGNUshellandeasiestforbeginners.
Printitoutandtakeithome,studyitwheneveryouhave5minutes.
BashGuideforBeginnersAppendixA.
ShellFeatures145GlossaryThissectioncontainsanalphabeticaloverviewofcommonUNIXcommands.
Moreinformationabouttheusagecanbefoundinthemanorinfopages.
Aa2psFormatfilesforprintingonaPostScriptprinter.
acroreadPDFviewer.
adduserCreateanewuserorupdatedefaultnewuserinformation.
aliasCreateashellaliasforacommand.
anacronExecutecommandsperiodically,doesnotassumecontinuouslyrunningmachine.
aproposSearchthewhatisdatabaseforstrings.
apt-getAPTpackagehandlingutility.
aspellSpellchecker.
at,atq,atrmQueue,examineordeletejobsforlaterexecution.
aumixAdjustaudiomixer.
(g)awkPatternscanningandprocessinglanguage.
BbashBourneAgainSHell.
batchQueue,examineordeletejobsforlaterexecution.
bgRunajobinthebackground.
bitmapBitmapeditorandconverterutilitiesfortheXwindowSystem.
bzip2Ablock-sortingfilecompressor.
CcatConcatenatefilesandprinttostandardoutput.
Glossary146cdChangedirectory.
cdp/cdplayAninteractivetext-modeprogramforcontrollingandplayingaudioCDRomsunderLinux.
cdparanoiaAnaudioCDreadingutilitywhichincludesextradataverificationfeatures.
cdrecordRecordaCD-R.
chattrChangefileattributes.
chgrpChangegroupownership.
chkconfigUpdateorqueryrunlevelinformationforsystemservices.
chmodChangefileaccesspermissions.
chownChangefileownerandgroup.
compressCompressfiles.
cpCopyfilesanddirectories.
crontabMaintaincrontabfiles.
cshOpenaCshell.
cutRemovesectionsfromeachlineoffile(s).
DdatePrintorsetsystemdateandtime.
ddConvertandcopyafile(diskdump).
dfReportfilesystemdiskusage.
dhcpcdDHCPclientdaemon.
diffFinddifferencesbetweentwofiles.
digSenddomainnamequerypacketstonameservers.
dmesgPrintorcontrolthekernelringbuffer.
duEstimatefilespaceusage.
BashGuideforBeginnersGlossary147EechoDisplayalineoftext.
ediffDifftoEnglishtranslator.
egrepExtendedgrep.
ejectUnmountandejectremovablemedia.
emacsStarttheEmacseditor.
execInvokesubprocess(es).
exitExitcurrentshell.
exportAddfunction(s)totheshellenvironment.
Ffax2psConvertaTIFFfacsimiletoPostScript.
fdformatFormatfloppydisk.
fdiskPartitiontablemanipulatorforLinux.
fetchmailFetchmailfromaPOP,IMAP,ETRNorODMR-capableserver.
fgBringajobintheforeground.
fileDeterminefiletype.
findFindfiles.
formailMail(re)formatter.
fortunePrintarandom,hopefullyinterestingadage.
ftpTransferfiles(unsafeunlessanonymousaccountisused!
)services.
GgaleonGraphicalwebbrowser.
gdmGnomeDisplayManager.
(min/a)gettyBashGuideforBeginnersGlossary148Controlconsoledevices.
gimpImagemanipulationprogram.
grepPrintlinesmatchingapattern.
grubThegrubshell.
gvAPostScriptandPDFviewer.
gzipCompressorexpandfiles.
HhaltStopthesystem.
headOutputthefirstpartoffiles.
helpDisplayhelponashellbuilt-incommand.
hostDNSlookuputility.
httpdApachehypertexttransferprotocolserver.
IidPrintrealandeffectiveUIDsandGIDs.
ifconfigConfigurenetworkinterfaceorshowconfiguration.
infoReadInfodocuments.
initProcesscontrolinitialization.
iostatDisplayI/Ostatistics.
ipDisplay/changenetworkinterfacestatus.
ipchainsIPfirewalladministration.
iptablesIPpacketfilteradministration.
JjarJavaarchivetool.
jobsBashGuideforBeginnersGlossary149Listbackgroundedtasks.
KkdmDesktopmanagerforKDE.
kill(all)Terminateprocess(es).
kshOpenaKornshell.
LldapmodifyModifyanLDAPentry.
ldapsearchLDAPsearchtool.
lessmorewithfeatures.
liloLinuxbootloader.
linksTextmodeWWWbrowser.
lnMakelinksbetweenfiles.
loadkeysLoadkeyboardtranslationtables.
locateFindfiles.
logoutClosecurrentshell.
lpSendrequeststotheLPprintservice.
lpcLineprintercontrolprogram.
lpqPrintspoolqueueexaminationprogram.
lprOfflineprint.
lprmRemoveprintrequests.
lsListdirectorycontent.
lynxTextmodeWWWbrowser.
MmailBashGuideforBeginnersGlossary150Sendandreceivemail.
manReadmanpages.
mcopyCopyMSDOSfilesto/fromUnix.
mdirDisplayanMSDOSdirectory.
memusageDisplaymemoryusage.
memusagestatDisplaymemoryusagestatistics.
mesgControlwriteaccesstoyourterminal.
mformatAddanMSDOSfilesystemtoalow-levelformattedfloppydisk.
mkbootdiskCreatesastand-alonebootfloppyfortherunningsystem.
mkdirCreatedirectory.
mkisofsCreateahybridISO9660filesystem.
moreFilterfordisplayingtextonescreenatthetime.
mountMountafilesystemordisplayinformationaboutmountedfilesystems.
mozillaWebbrowser.
mtControlmagnetictapedriveoperation.
mtrNetworkdiagnostictool.
mvRenamefiles.
NnamedInternetdomainnameserver.
ncftpBrowserprogramforftpservices(insecure!
).
netstatPrintnetworkconnections,routingtables,interfacestatistics,masqueradeconnections,andmulti-castmemberships.
nfsstatPrintstatisticsaboutnetworkedfilesystems.
niceRunaprogramwithmodifiedschedulingpriority.
nmapNetworkexplorationtoolandsecurityscanner.
ntsysvBashGuideforBeginnersGlossary151Simpleinterfaceforconfiguringrunlevels.
PpasswdChangepassword.
pdf2psGhostscriptPDFtoPostScripttranslator.
perlPracticalExtractionandReportLanguage.
pgPagethroughtextoutput.
pingSendechorequesttoahost.
prConverttextfilesforprinting.
printenvPrintallorpartofenvironment.
procmailAutonomousmailprocessor.
psReportprocessstatus.
pstreeDisplayatreeofprocesses.
pwdPrintpresentworkingdirectory.
QquotaDisplaydiskusageandlimits.
RrcpRemotecopy(unsafe!
)rdesktopRemoteDesktopProtocolclient.
rebootStopandrestartthesystem.
reniceAlterpriorityofarunningprocess.
rloginRemotelogin(telnet,insecure!
).
rmRemoveafile.
rmdirRemoveadirectory.
rpmBashGuideforBeginnersGlossary152RPMPackageManager.
rshRemoteshell(insecure!
).
SscpSecureremotecopy.
screenScreenmanagerwithVT100emulation.
setDisplay,setorchangevariable.
settermSetterminalattributes.
sftpSecure(encrypted)ftp.
shOpenastandardshell.
shutdownBringthesystemdown.
sleepWaitforagivenperiod.
slocateSecurityEnhancedversionoftheGNULocate.
slrnntextmodeUsenetclient.
snortNetworkintrusiondetectiontool.
sortSortlinesoftextfiles.
sshSecureshell.
ssh-keygenAuthenticationkeygeneration.
sttyChangeandprintterminallinesettings.
suSwitchuser.
TtacConcatenateandprintfilesinreverse.
tailOutputthelastpartoffiles.
talkTalktoauser.
tarArchivingutility.
tcshBashGuideforBeginnersGlossary153OpenaTurboCshell.
telnetUserinterfacetotheTELNETprotocol(insecure!
).
texTextformattingandtypesetting.
timeTimeasimplecommandorgiveresourceusage.
tinNewsreadingprogram.
topDisplaytopCPUprocesses.
touchChangefiletimestamps.
traceroutePrinttheroutepacketstaketonetworkhost.
tripwireAfileintegritycheckerforUNIXsystems.
twmTabWindowManagerfortheXWindowSystem.
UulimitControllresources.
umaskSetuserfilecreationmask.
umountUnmountafilesystem.
uncompressDecompresscompressedfiles.
uniqRemoveduplicatelinesfromasortedfile.
updateKerneldaemontoflushdirtybuffersbacktodisk.
uptimeDisplaysystemuptimeandaverageload.
userdelDeleteauseraccountandrelatedfiles.
Vvi(m)Startthevi(improved)editor.
vimtutorTheVimtutor.
vmstatReportvirtualmemorystatistics.
BashGuideforBeginnersGlossary154WwShowwhoisloggedonandwhattheyaredoing.
wallSendamessagetoeverybody'sterminal.
wcPrintthenumberofbytes,wordsandlinesinfiles.
whichShowsthefullpathof(shell)commands.
whoShowwhoisloggedon.
whoamiPrinteffectiveuserID.
whoisQueryawhoisornicnamedatabase.
writeSendamessagetoanotheruser.
XxauthXauthorityfileutility.
xcdroastGraphicalfrontendtocdrecord.
xclockAnalog/digitalclockforX.
xconsoleMonitorsystemconsolemessageswithX.
xdmXDisplayManagerwithsupportforXDMCP,hostchooser.
xdviDVIviewer.
xfsXfontserver.
xhostServeraccesscontrolprogramforXxinetdTheextendedInternetservicesdaemon.
xloadSystemloadaveragedisplayforX.
xlsfontsServerfontlistdisplayerforX.
xmmsAudioplayerforX.
xpdfPDFviewer.
xtermTerminalemulatorforX.
BashGuideforBeginnersGlossary155ZzcatCompressorexpandfiles.
zgrepSearchpossiblycompressedfilesforaregularexpression.
zmoreFilterforviewingcompressedtext.
BashGuideforBeginnersGlossary156IndexAaliasesSection3.
5.
1ANSI-CquotingSection3.
3.
5argumentsSection7.
2.
1.
2arithmeticexpansionSection3.
4.
7arithmeticoperatorsSection3.
4.
7arraySection10.
2.
1awkSection6.
1awkprogramSection6.
1.
2BbashSection1.
2.
bash_loginSection3.
1.
2.
2.
bash_logoutSection3.
1.
2.
5.
bash_profileSection3.
1.
2.
1.
bashrcSection3.
1.
2.
4batcheditorSection5.
1.
1breakSection9.
5.
1booleanoperatorsSection7.
2.
4BourneshellSection1.
1.
2braceexpansionSection3.
4.
3built-incommandsSection1.
3.
2Index157CcasestatementsSection7.
2.
5characterclassesSection4.
2.
2.
2,Section4.
3.
2childprocessSection1.
3.
1combinedexpressionsSection7.
1.
1.
1commandsubstitutionSection3.
4.
6commentsSection2.
2.
2conditionalsSection7.
1configurationfilesSection3.
1constantsSection10.
1.
3continueSection9.
5.
2controlsignalsSection12.
1.
1.
3creatingvariablesSection3.
2.
2cshTheCshell,Section1.
1.
2DdebuggingscriptsSection2.
3declareSection10.
1.
2,Section10.
2.
1doublequotesSection3.
3.
4EechoSection1.
5.
5,Section2.
1.
2,Section2.
3.
2,Section8.
1.
2editorsSection2.
1.
1elseSection7.
2.
1emacsSection2.
1.
1envBashGuideforBeginnersIndex158Section3.
2.
1.
1esacSection7.
2.
5escapecharactersSection3.
3.
2escapesequencesSection8.
1.
2/etc/bashrcSection3.
1.
1.
2/etc/passwdSection1.
1.
2/etc/profileSection3.
1.
1/etc/shellsSection1.
1.
2execSection1.
3.
1,Section8.
2.
4.
2executepermissionsSection2.
1.
3executionSection2.
1.
3exitSection7.
2.
5exitstatusSection7.
1.
2.
1expansionSection1.
4.
1.
5,Section3.
4exportSection3.
2.
3extendedregularexpressionsSection4.
1.
3FfiledescriptorsSection8.
2.
3,Section8.
2.
4.
1filenameexpansionSection3.
4.
9findandreplaceSection5.
2.
4forSection9.
1forkSection1.
3.
1functionsSection11.
1.
1BashGuideforBeginnersIndex159GgawkSection6.
1.
1gawkcommandsSection6.
1.
2gawkfieldsSection6.
2.
1gawkformattingSection6.
2.
2gawkscriptsSection6.
2.
5gawkvariablesSection6.
3geditSection2.
1.
1globalvariablesSection3.
2.
1.
1globbingSection2.
3.
2grepSection4.
2.
1HheredocumentSection8.
2.
4.
4IifSection7.
1.
1initSection1.
3.
1,Section1.
5.
6initializationfilesSection3.
1inputfieldseparatorSection3.
2.
4.
1,Section3.
2.
5,Section6.
3interactiveeditingSection5.
2interactivescriptsSection8.
1interactiveshellSection1.
2.
2.
2.
1,Section1.
2.
2.
2.
2,Section1.
2.
2.
3.
3invocationSection1.
2.
2.
1BashGuideforBeginnersIndex160JKkillSection12.
1.
2killallSection12.
1.
2kshKornshell,Section1.
1.
2LlengthofavariableSection10.
3.
2lineanchorsSection4.
2.
2.
1localeSection3.
3.
6locateSection2.
1.
1logicflowSection1.
5.
4loginshellSection1.
2.
2.
2.
1MmenuSection9.
6metacharactersSection4.
1.
2NnestedifstatementsSection7.
2.
3noglobSection2.
3.
2non-interactiveeditingSection5.
3non-interactiveshellSection1.
2.
2.
2.
3non-loginshellSection1.
2.
2.
2.
2numericcomparisonsSection7.
1.
2.
2BashGuideforBeginnersIndex161OoptionsSection3.
6.
1outputfieldseparatorSection6.
3.
2.
1outputrecordseparatorSection6.
3.
2.
2PparameterexpansionSection3.
4.
5PATHSection2.
1.
2patternmatchingSection4.
3positionalparamsSection3.
2.
5,Section11.
1.
3POSIXSection1.
2.
1POSIXmodeSection1.
2.
2.
2.
5primaryexpressionsSection7.
1.
1.
1printenvSection3.
2.
1.
1printfSection1.
5.
5,Section6.
3.
6processsubstitutionSection3.
4.
8.
profileSection3.
1.
2.
3promptSection3.
1.
3QquotingcharactersSection3.
3RredirectionSection1.
4.
1.
7,Section3.
6.
2,Section8.
2.
3,Section9.
4rbashSection1.
2.
2.
10readSection8.
2.
1BashGuideforBeginnersIndex162readonlySection10.
1.
3regularexpressionoperatorsSection4.
1.
2,Section5.
2,Section6.
2.
4regularexpressionsSection4.
1remoteinvocationSection1.
2.
2.
2.
6removingaliasesSection3.
5.
2reservedvariablesSection3.
2.
4returnSection11.
1.
3SsedSection5.
1sededitingcommandsSection5.
1.
2sedoptionsSection5.
1.
2sedscriptSection5.
3.
2selectSection9.
6setSection3.
2.
1.
2,Section3.
6.
1,Section11.
1.
4shiftSection9.
7signalsSection12.
1.
1singlequotesSection3.
3.
3sourceSection2.
1.
3specialparametersSection3.
2.
5specialvariablesSection3.
2.
5standarderrorSection8.
2.
3.
1standardinputSection8.
2.
3.
1standardoutputSection8.
2.
3.
1stringcomparisonsSection7.
1.
2.
3sttyBashGuideforBeginnersIndex163Section12.
1.
1submenuSection9.
6.
2subshellSection2.
2.
1substitutionSection10.
3.
3.
1,Section10.
3.
3.
3substringSection10.
3.
3.
2syntaxSection1.
4.
1.
1TtcshSection1.
1.
2terminologySection1.
5.
3thenSection7.
1.
1.
2tildeexpansionSection3.
4.
4transformationofvariablesSection10.
3.
3trapsSection12.
2.
1trueSection9.
2.
2.
2UunaliasSection3.
5.
1,Section3.
5.
2unsetSection3.
2.
2,Section10.
2.
3,Section11.
1.
4untilSection9.
3userinputSection8.
2.
1,Section8.
2.
2usermessagesSection8.
1.
1VvariablesSection3.
2,Section10.
1variableexpansionSection3.
4.
5verboseBashGuideforBeginnersIndex164Section2.
3.
2vi(m)Section2.
1.
1WwaitSection12.
2.
2whereisSection2.
1.
1whichSection2.
1.
1whileSection9.
2wildcardsSection4.
2.
2.
3wordanchorsSection4.
2.
2.
1wordsplittingSection3.
4.
9XxtraceSection2.
3.
1,Section2.
3.
2YZBashGuideforBeginnersIndex165

vpsdime:VPS内存/2核/VPS,4G内存/2核/50gSSD/2T流量/达拉斯机房达拉斯机房,新产品系列-Windows VPS

vpsdime上了新产品系列-Windows VPS,配置依旧很高但是价格依旧是走低端线路。或许vpsdime的母公司Nodisto IT想把核心产品集中到vpsdime上吧,当然这只是站长个人的猜测,毕竟winity.io也是专业卖Windows vps的,而且也是他们自己的品牌。vpsdime是一家新上来不久的奇葩VPS提供商,实际是和backupspy以及crowncloud等都是同一家公司...

HostYun(月18元),CN2直连香港大带宽VPS 50M带宽起

对于如今的云服务商的竞争着实很激烈,我们可以看到国内国外服务商的各种内卷,使得我们很多个人服务商压力还是比较大的。我们看到这几年的服务商变动还是比较大的,很多新服务商坚持不超过三个月,有的是多个品牌同步进行然后分别的跑路赚一波走人。对于我们用户来说,便宜的服务商固然可以试试,但是如果是不确定的,建议月付或者主力业务尽量的还是注意备份。HostYun 最近几个月还是比较活跃的,在前面也有多次介绍到商...

港云网络(¥1/月活动机器),香港CN2 4核4G 1元/月 美国CN2

港云网络官方网站商家简介港云网络成立于2016年,拥有IDC/ISP/云计算资质,是正规的IDC公司,我们采用优质硬件和网络,为客户提供高速、稳定的云计算服务。公司拥有一流的技术团队,提供7*24小时1对1售后服务,让您无后顾之忧。我们目前提供高防空间、云服务器、物理服务器,高防IP等众多产品,为您提供轻松上云、安全防护。点击进入港云网络官方网站港云网络中秋福利1元领【每人限量1台】,售完下架,活...

bash为你推荐
电脑桌面背景图片电脑桌面壁纸在哪里?!聚酯纤维和棉哪个好袜子是棉的好还是聚酯纤维的好?迈腾和帕萨特哪个好新迈腾和新帕萨特哪个更好一点·哪个更实用一点 ···明白人给解释一下·固定利率和浮动利率哪个好应当选择固定利率,还是选择浮动利率还款?看书软件哪个好推荐几个好用的手机看书软件扣扣空间登录QQ空间怎么老是提示登陆?q空间登录腾讯qq空间登陆qq空间登录不上为什么我的qq空间登不上去考生个人空间登录自学考试的“考生个人空间”密码忘记了……一定要本人带身份证和考籍证去有关部门吗?辽宁联通网上营业厅中国联通网上营业厅固定电话费查询
me域名注册 合肥虚拟主机 泛域名 移动服务器租用 南通服务器租用 过期备案域名查询 日本动态vps 二级域名申请 新世界机房 win8.1企业版升级win10 typecho 网站实时监控 已备案删除域名 如何注册阿里云邮箱 服务器硬件防火墙 腾讯总部在哪 台湾google 腾讯数据库 域名和主机 酷锐 更多