📄 t2
字号:
.bp.SH2.0\ Shell\ procedures.LPThe shell may be used to read and execute commandscontained in a file.For example,.DS sh file [ args \*(ZZ ].DEcalls the shell to read commands from \fIfile.\fPSuch a file is called a \fIcommand procedure\fPor \fIshell procedure.\fPArguments may be supplied with the calland are referred to in \fIfile\fPusing the positional parameters\fB$1, $2, \*(ZZ\|.\fRFor example, if the file \fIwg\fP contains.DS who \*(VT grep $1.DEthen.DS sh wg fred.DEis equivalent to.DS who \*(VT grep fred.DE.LPUNIX files have three independent attributes,\fIread,\fP \fIwrite\fP and \fIexecute.\fPThe UNIX command \fIchmod\fP (1) may be usedto make a file executable.For example,.DS chmod +x wg.DEwill ensure that the file \fIwg\fP has execute status.Following this, the command.DS wg fred.DEis equivalent to.DS sh wg fred.DEThis allows shell procedures and programsto be used interchangeably.In either case a new process is created torun the command..LPAs well as providing names for the positionalparameters,the number of positional parameters in the callis available as \fB$#\|.\fPThe name of the file being executedis available as \fB$0\|.\fP.LPA special shell parameter \fB$\*(ST\fPis used to substitute for all positional parametersexcept \fB$0\|.\fPA typical use of this is to providesome default arguments,as in,.DS nroff \(miT450 \(mims $\*(ST.DEwhich simply prepends some argumentsto those already given..SH2.1\ Control\ flow\ -\ for.LPA frequent use of shell procedures is to loopthrough the arguments (\fB$1, $2, \*(ZZ\fR)executing commands once for each argument.An example of such a procedure is\fItel\fP that searches the file\fB/usr/lib/telnos\fRthat contains lines of the form.DS \*(ZZ fred mh0123 bert mh0789 \*(ZZ.DEThe text of \fItel\fP is.DS for i do grep $i /usr/lib/telnos; done.DEThe command.DS tel fred.DEprints those lines in \fB/usr/lib/telnos\fRthat contain the string \fIfred\|.\fP.DS tel fred bert.DEprints those lines containing \fIfred\fPfollowed by those for \fIbert.\fP.LPThe \fBfor\fP loop notation is recognized by the shelland has the general form.DS \fBfor\fR \fIname\fR \fBin\fR \fIw1 w2 \*(ZZ\fR \fBdo\fR \fIcommand-list\fR \fBdone\fR.DEA \fIcommand-list\fP is a sequence of one or moresimple commands separated or terminated by a newline or semicolon.Furthermore, reserved wordslike \fBdo\fP and \fBdone\fP are onlyrecognized following a newline orsemicolon.\fIname\fP is a shell variable that is setto the words \fIw1 w2 \*(ZZ\fR in turn each time the \fIcommand-list\fPfollowing \fBdo\fPis executed.If \fBin\fR \fIw1 w2 \*(ZZ\fRis omitted then the loopis executed once for each positional parameter;that is, \fBin\fR \fI$\*(ST\fR is assumed..LPAnother example of the use of the \fBfor\fPloop is the \fIcreate\fP commandwhose text is.DS for i do >$i; done.DEThe command.DS create alpha beta.DEensures that two empty files\fIalpha\fP and \fIbeta\fP existand are empty.The notation \fI>file\fP may be used on itsown to create or clear the contents of a file.Notice also that a semicolon (or newline) is required before \fBdone.\fP.SH2.2\ Control\ flow\ -\ case.LPA multiple way branch is provided for by the\fBcase\fP notation.For example,.DS case $# in \*(Ca1) cat \*(AP$1 ;; \*(Ca2) cat \*(AP$2 <$1 ;; \*(Ca\*(ST) echo \\'usage: append [ from ] to\\' ;; esac.DEis an \fIappend\fP command.When calledwith one argument as.DS append file.DE\fB$#\fP is the string \fI1\fP andthe standard input is copied onto theend of \fIfile\fPusing the \fIcat\fP command..DS append file1 file2.DEappends the contents of \fIfile1\fPonto \fIfile2.\fPIf the number of arguments supplied to\fIappend\fP is other than 1 or 2then a message is printed indicatingproper usage..LPThe general form of the \fBcase\fP commandis.DS \fBcase \fIword \fBin \*(Ca\fIpattern\|\fB)\ \fIcommand-list\fB\|;; \*(Ca\*(ZZ \fBesac\fR.DEThe shell attempts to match\fIword\fR with each \fIpattern,\fRin the order in which the patternsappear.If a match is found theassociated \fIcommand-list\fP isexecuted and executionof the \fBcase\fP is complete.Since \*(ST is the pattern that matches anystring it can be used for the default case..LPA word of caution:no check is made to ensure that onlyone pattern matchesthe case argument.The first match found defines the set of commandsto be executed.In the example below the commands followingthe second \*(ST will never be executed..DS case $# in \*(Ca\*(ST) \*(ZZ ;; \*(Ca\*(ST) \*(ZZ ;; esac.DE.LPAnother example of the use of the \fBcase\fPconstruction is to distinguishbetween different formsof an argument.The following example is a fragment of a \fIcc\fP command..DS for i do case $i in \*(DC\(mi[ocs]) \*(ZZ ;; \*(DC\(mi\*(ST) echo \\'unknown flag $i\\' ;; \*(DC\*(ST.c) /lib/c0 $i \*(ZZ ;; \*(DC\*(ST) echo \\'unexpected argument $i\\' ;; \*(DOesac done.DE.LPTo allow the same commands to be associatedwith more than one patternthe \fBcase\fP command providesfor alternative patternsseparated by a \*(VT\|.For example,.DS case $i in \*(Ca\(mix\*(VT\(miy) \*(ZZ esac.DEis equivalent to.DS case $i in \*(Ca\(mi[xy]) \*(ZZ esac.DE.LPThe usual quoting conventions applyso that.DS case $i in \*(Ca\\\\?) \*(ZZ.DEwill match the character \fB?\|.\fP.SH2.3\ Here\ documents.LPThe shell procedure \fItel\fPin section 2.1 uses the file \fB/usr/lib/telnos\fRto supply the datafor \fIgrep.\fPAn alternative is to include thisdatawithin the shell procedure as a \fIhere\fP document, as in,.DS for i do grep $i \*(HE! \*(DO\*(ZZ \*(DOfred mh0123 \*(DObert mh0789 \*(DO\*(ZZ ! done.DEIn this examplethe shell takes the lines between \fB\*(HE!\fR and \fB!\fRas the standard input for \fIgrep.\fPThe string \fB!\fR is arbitrary, the documentbeing terminated by a line that consistsof the string following \*(HE\|..LPParameters are substituted in the documentbefore it is made available to \fIgrep\fPas illustrated by the following procedurecalled \fIedg\|.\fP.DS ed $3 \*(HE% g/$1/s//$2/g w %.DEThe call.DS edg string1 string2 file.DEis then equivalent to the command.DS ed file \*(HE% g/string1/s//string2/g w %.DEand changes all occurrences of \fIstring1\fPin \fIfile\fP to \fIstring2\|.\fPSubstitution can be prevented using \\to quote the special character \fB$\fPas in.DS ed $3 \*(HE+ 1,\\\\$s/$1/$2/g w +.DE(This version of \fIedg\fP is equivalent tothe first except that \fIed\fP will printa \fB?\fR if there are no occurrences ofthe string \fB$1\|.\fP)Substitution within a \fIhere\fP documentmay be prevented entirely by quotingthe terminating string,for example,.DS grep $i \*(HE\\\\# \*(ZZ #.DEThe document is presentedwithout modification to \fIgrep.\fPIf parameter substitution is not requiredin a \fIhere\fP document this latter formis more efficient..SH2.4\ Shell\ variables.LPThe shellprovides string-valued variables.Variable names begin with a letterand consist of letters, digits andunderscores.Variables may be given values by writing, for example,.DS user=fred\ box=m000\ acct=mh0000.DEwhich assigns values to the variables\fBuser, box\fP and \fBacct.\fPA variable may be set to the null stringby saying, for example,.DS null=.DEThe value of a variable is substitutedby preceding its name with \fB$\|;\fPfor example,.DS echo $user.DEwill echo \fIfred.\fP.LPVariables may be used interactivelyto provide abbreviations for frequentlyused strings.For example,.DS b=/usr/fred/bin mv pgm $b.DEwill move the file \fIpgm\fPfrom the current directory to the directory \fB/usr/fred/bin\|.\fRA more general notation is available for parameter(or variable)substitution, as in,.DS echo ${user}.DEwhich is equivalent to.DS echo $user.DEand is used when the parameter name isfollowed by a letter or digit.For example,.DS tmp=/tmp/ps ps a >${tmp}a.DEwill direct the output of \fIps\fRto the file \fB/tmp/psa,\fRwhereas,.DS ps a >$tmpa.DEwould cause the value of the variable \fBtmpa\fPto be substituted..LPExcept for \fB$?\fP the followingare set initially by the shell.\fB$?\fP is set after executing each command..RS.IP \fB$?\fP 8The exit status (return code)of the last command executedas a decimal string.Most commands return a zero exit statusif they complete successfully,otherwise a non-zero exit status is returned.Testing the value of return codes is dealt withlater under \fBif\fP and \fBwhile\fP commands..IP \fB$#\fP 8The number of positional parameters(in decimal).Used, for example, in the \fIappend\fP commandto check the number of parameters..IP \fB$$\fP 8The process number of this shell (in decimal).Since process numbers are unique amongall existing processes, this string isfrequently used to generateuniquetemporary file names.For example,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -