📄 t3
字号:
.bp.SH3.0\ Keyword\ parameters.LPShell variables may be given valuesby assignmentor when a shell procedure is invoked.An argument to a shell procedure of the form\fIname=value\fPthat precedes the command namecauses \fIvalue\fPto be assigned to \fIname\fPbefore execution of the procedure begins.The value of \fIname\fP in the invokingshell is not affected.For example,.DS user=fred\ command.DEwill execute \fIcommand\fP with\fBuser\fP set to \fIfred\fP.The \fB\(mik\fR flag causes arguments of the form\fIname=value\fP to be interpreted in this wayanywhere in the argument list.Such \fInames\fP are sometimescalled keyword parameters.If any arguments remain theyare available as positionalparameters \fB$1, $2, \*(ZZ\|.\fP.LPThe \fIset\fP commandmay also be used to set positional parametersfrom within a procedure.For example,.DS set\ \(mi\ \*(ST.DEwill set \fB$1\fP to the first file namein the current directory, \fB$2\fP to the next,and so on.Note that the first argument, \(mi, ensures correct treatmentwhen the first file name begins with a \(mi\|..LP.SH3.1\ Parameter\ transmission.LPWhen a shell procedure is invoked both positionaland keyword parameters may be supplied with the call.Keyword parameters are also made available implicitlyto a shell procedureby specifying in advance that such parametersare to be exported.For example,.DS export\ user\ box.DEmarks the variables \fBuser\fP and \fBbox\fPfor export.When a shell procedure is invokedcopies are made of all exportable variablesfor use within the invoked procedure.Modification of such variableswithin the procedure does notaffect the values in the invoking shell.It is generally true ofa shell procedurethat itmay not modify the stateof its caller without explicitrequest on the part of the caller.(Shared file descriptors are anexception to this rule.).LPNames whose value is intended to remainconstant may be declared \fIreadonly\|.\fPThe form of this command is the same as that of the \fIexport\fPcommand,.DS readonly name \*(ZZ.DESubsequent attempts to set readonly variablesare illegal..SH3.2\ Parameter\ substitution.LPIf a shell parameter is not setthen the null string is substituted for it.For example, if the variable \fBd\fPis not set.DS echo $d.DEor.DS echo ${d}.DEwill echo nothing.A default string may be givenas in.DS echo ${d\(mi\fB.\fR}.DEwhich will echothe value of the variable \fBd\fPif it is set and `\fB.\fP' otherwise.The default string is evaluated using the usualquoting conventions so that.DS echo ${d\(mi\'\*(ST\'}.DEwill echo \fB\*(ST\fP if the variable \fBd\fPis not set.Similarly.DS echo ${d\(mi$1}.DEwill echo the value of \fBd\fP if it is setand the value (if any) of \fB$1\fP otherwise.A variable may be assigned a default valueusingthe notation.DS echo ${d=\fB.\fR}.DEwhich substitutes the same string as.DS echo ${d\(mi\fB.\fR}.DEand if \fBd\fP were not previously setthen it will be set to the string `\fB.\fP'\|.(The notation ${\*(ZZ=\*(ZZ}is not available for positional parameters.).LPIf there is no sensible default thenthe notation.DS echo ${d?message}.DEwill echo the value of the variable \fBd\fP if it hasone, otherwise \fImessage\fP is printed by the shell andexecution of the shell procedure is abandoned.If \fImessage\fP is absent then a standard messageis printed.A shell procedure that requires some parametersto be set might start as follows..DS :\ ${user?}\ ${acct?}\ ${bin?} \*(ZZ.DEColon (\fB:\fP) is a commandthat isbuilt in to the shell and does nothingonce its arguments have been evaluated.If any of the variables \fBuser, acct\fPor \fBbin\fP are not set then the shellwill abandon execution of the procedure..SH3.3\ Command\ substitution.LPThe standard output from a command can besubstituted in a similar way to parameters.The command \fIpwd\fP prints on its standardoutput the name of the current directory.For example, if the current directory is\fB/usr/fred/bin\fRthen the command.DS d=\`pwd\`.DEis equivalent to.DS d=/usr/fred/bin.DE.LPThe entire string between grave accents (\`\*(ZZ\`)is taken as the commandto be executedand is replaced with the output fromthe command.The command is written using the usualquoting conventionsexcept that a \fB\`\fR must be escaped usinga \fB\\\|.\fRFor example,.DS ls \`echo "$1"\`.DEis equivalent to.DS ls $1.DECommand substitution occurs in all contextswhere parameter substitution occurs (including \fIhere\fP documents) and thetreatment of the resulting text is the samein both cases.This mechanism allows stringprocessing commands to be used withinshell procedures.An example of such a command is \fIbasename\fPwhich removes a specified suffix from a string.For example,.DS basename main\fB.\fPc \fB.\fPc.DEwill print the string \fImain\|.\fPIts use is illustrated by the followingfragment from a \fIcc\fP command..DS case $A in \*(Ca\*(ZZ \*(Ca\*(ST\fB.\fPc) B=\`basename $A \fB.\fPc\` \*(Ca\*(ZZ esac.DEthat sets \fBB\fP to the part of \fB$A\fPwith the suffix \fB.c\fP stripped..LPHere are some composite examples..RS.IP \(bu.ft Bfor i in \`ls \(mit\`; do \*(ZZ.ft R.brThe variable \fBi\fP is setto the names of files in time order,most recent first..IP \(bu.ft Bset \`date\`; echo $6 $2 $3, $4.ft R.brwill print, e.g.,.ft I1977 Nov 1, 23:59:59.ft R.RE.SH3.4\ Evaluation\ and\ quoting.LPThe shell is a macro processor thatprovides parameter substitution, command substitution and filename generation for the arguments to commands.This section discusses the order in whichthese evaluations occur and theeffects of the various quoting mechanisms..LPCommands are parsed initially according to the grammargiven in appendix A.Before a command is executedthe followingsubstitutions occur..RS.IP \(buparameter substitution, e.g. \fB$user\fP.IP \(bucommand substitution, e.g. \fB\`pwd\`\fP.RS.LPOnly one evaluation occurs so that if, for example, the value of the variable\fBX\fPis the string \fI$y\fPthen.DS echo $X.DEwill echo \fI$y\|.\fP.RE.IP \(bublank interpretation.RS.LPFollowing the above substitutionsthe resulting charactersare broken into non-blank words (\fIblank interpretation\fP).For this purpose `blanks' are the characters of the string\fB$\s-1IFS\s0\fP.By default, this string consists of blank, tab and newline.The null stringis not regarded as a word unless it is quoted.For example,.DS echo \'\'.DEwill pass on the null string as the first argument to \fIecho\fP,whereas.DS echo $null.DEwill call \fIecho\fR with no argumentsif the variable \fBnull\fP is not setor set to the null string..RE.IP \(bufile name generation.RS.LPEach wordis then scanned for the file pattern characters\fB\*(ST, ?\fR and \fB[\*(ZZ]\fRand an alphabetical list of file namesis generated to replace the word.Each such file name is a separate argument..RE.RE.LPThe evaluations just described also occurin the list of words associated with a \fBfor\fPloop.Only substitution occursin the \fIword\fP usedfor a \fBcase\fP branch..LPAs well as the quoting mechanisms describedearlier using \fB\\\fR and \fB\'\*(ZZ\'\fRa third quoting mechanism is provided using double quotes.Within double quotes parameter and command substitutionoccurs but file name generation and the interpretationof blanks does not.The following charactershave a special meaning within double quotesand may be quoted using \fB\\\|.\fP.DS \fB$ \fPparameter substitution \fB\`\fP command substitution \fB"\fP ends the quoted string \fB\e\fP quotes the special characters \fB$ \` " \e\fP.DEFor example,.DS echo "$x".DEwill pass the value of the variable \fBx\fP as asingle argument to \fIecho.\fPSimilarly,.DS echo "$\*(ST".DEwill pass the positional parameters as a singleargument and is equivalent to.DS echo "$1 $2 \*(ZZ".DEThe notation \fB$@\fPis the same as \fB$\*(ST\fRexcept when it is quoted..DS echo "$@".DEwill pass the positional parameters, unevaluated, to \fIecho\fRand is equivalent to.DS echo "$1" "$2" \*(ZZ.DE.LPThe following table gives, for each quoting mechanism,the shell metacharacters that are evaluated..DS.ce.ft Imetacharacter.ft.in 1.5i \e $ * \` " \'\' n n n n n t\` y n n t n n" y y n y t n t terminator y interpreted n not interpreted.in.ft B.ceFigure 2. Quoting mechanisms.ft.DE.LPIn cases where more than one evaluation of a stringis required the built-in command \fIeval\fPmay be used.For example,if the variable \fBX\fP has the value\fI$y\fP, and if \fBy\fP has the value \fIpqr\fPthen.DS eval echo $X.DEwill echo the string \fIpqr\|.\fP.LPIn general the \fIeval\fP commandevaluates its arguments (as do all commands)and treats the result as input to the shell.The input is read and the resulting command(s)executed.For example,.DS wg=\\'eval who\*(VTgrep\\' $wg fred.DEis equivalent to.DS who\*(VTgrep fred.DEIn this example,\fIeval\fP is requiredsince there is no interpretationof metacharacters, such as \fB\*(VT\|,\fP following
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -