⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sh.1

📁 Android 一些工具
💻 1
📖 第 1 页 / 共 4 页
字号:
then leading tabs in the here-doc-text are stripped..Ss Search and ExecutionThere are three types of commands: shell functions, builtin commands, andnormal programs -- and the command is searched for (by name) in that order.They each are executed in a different way..PpWhen a shell function is executed, all of the shell positional parameters(except $0, which remains unchanged) are set to the arguments of the shellfunction.The variables which are explicitly placed in the environment ofthe command (by placing assignments to them before the function name) aremade local to the function and are set to the values given.Then the command given in the function definition is executed.The positional parameters are restored to their original valueswhen the command completes.This all occurs within the current shell..PpShell builtins are executed internally to the shell, without spawning anew process..PpOtherwise, if the command name doesn't match a function or builtin, thecommand is searched for as a normal program in the file system (asdescribed in the next section).When a normal program is executed, the shell runs the program,passing the arguments and the environment to the program.If the program is not a normal executable file (i.e., if it doesnot begin with the "magic number" whose.Tn ASCIIrepresentation is "#!", so.Xr execve 2returns.Er ENOEXECthen) the shell will interpret the program in a subshell.The child shell will reinitialize itself in this case,so that the effect will be as if anew shell had been invoked to handle the ad-hoc shell script, except thatthe location of hashed commands located in the parent shell will beremembered by the child..PpNote that previous versions of this document and the source code itselfmisleadingly and sporadically refer to a shell script without a magicnumber as a "shell procedure"..Ss Path SearchWhen locating a command, the shell first looks to see if it has a shellfunction by that name.Then it looks for a builtin command by that name.If a builtin command is not found, one of two things happen:.Bl -enum.ItCommand names containing a slash are simply executed without performingany searches..ItThe shell searches each entry in.Ev PATHin turn for the command.The value of the.Ev PATHvariable should be a series of entries separated by colons.Each entry consists of a directory name.The current directory may be indicatedimplicitly by an empty directory name, or explicitly by a single period..El.Ss Command Exit StatusEach command has an exit status that can influence the behaviorof other shell commands.The paradigm is that a command exitswith zero for normal or success, and non-zero for failure,error, or a false indication.The man page for each commandshould indicate the various exit codes and what they mean.Additionally, the builtin commands return exit codes, as doesan executed shell function..PpIf a command consists entirely of variable assignments then theexit status of the command is that of the last command substitutionif any, otherwise 0..Ss Complex CommandsComplex commands are combinations of simple commands with controloperators or reserved words, together creating a larger complex command.More generally, a command is one of the following:.Bl -bullet.Itsimple command.Itpipeline.Itlist or compound-list.Itcompound command.Itfunction definition.El.PpUnless otherwise stated, the exit status of a command is that of the lastsimple command executed by the command..Ss PipelinesA pipeline is a sequence of one or more commands separatedby the control operator |.The standard output of all butthe last command is connected to the standard inputof the next command.The standard output of the lastcommand is inherited from the shell, as usual..PpThe format for a pipeline is:.Pp.Dl [!] command1 [ | command2 ...].PpThe standard output of command1 is connected to the standard input ofcommand2.The standard input, standard output, or both of a command isconsidered to be assigned by the pipeline before any redirection specifiedby redirection operators that are part of the command..PpIf the pipeline is not in the background (discussed later), the shellwaits for all commands to complete..PpIf the reserved word ! does not precede the pipeline, the exit status isthe exit status of the last command specified in the pipeline.Otherwise, the exit status is the logical NOT of the exit status of thelast command.That is, if the last command returns zero, the exit statusis 1; if the last command returns greater than zero, the exit status iszero..PpBecause pipeline assignment of standard input or standard output or bothtakes place before redirection, it can be modified by redirection.For example:.Pp.Dl $ command1 2\*[Gt]\*[Am]1 | command2.Ppsends both the standard output and standard error of command1to the standard input of command2..PpA ; or.Aq newlineterminator causes the preceding AND-OR-list (describednext) to be executed sequentially; a \*[Am] causes asynchronous execution ofthe preceding AND-OR-list..PpNote that unlike some other shells, each process in the pipeline is achild of the invoking shell (unless it is a shell builtin, in which caseit executes in the current shell -- but any effect it has on theenvironment is wiped)..Ss Background Commands -- \*[Am]If a command is terminated by the control operator ampersand (\*[Am]), theshell executes the command asynchronously -- that is, the shell does notwait for the command to finish before executing the next command..PpThe format for running a command in background is:.Pp.Dl command1 \*[Am] [command2 \*[Am] ...].PpIf the shell is not interactive, the standard input of an asynchronouscommand is set to.Pa /dev/null ..Ss Lists -- Generally SpeakingA list is a sequence of zero or more commands separated by newlines,semicolons, or ampersands, and optionally terminated by one of these threecharacters.The commands in a list are executed in the order they are written.If command is followed by an ampersand, the shell starts thecommand and immediately proceed onto the next command; otherwise it waitsfor the command to terminate before proceeding to the next one..Ss Short-Circuit List Operators.Dq \*[Am]\*[Am]and.Dq ||are AND-OR list operators..Dq \*[Am]\*[Am]executes the first command, and then executes the second command if and onlyif the exit status of the first command is zero..Dq ||is similar, but executes the second command if and only if the exit statusof the first command is nonzero..Dq \*[Am]\*[Am]and.Dq ||both have the same priority.Note that these operators are left-associative, so.Dq true || echo bar && echo bazwrites.Dq bazand nothing else.This is not the way it works in C..Ss Flow-Control Constructs -- if, while, for, caseThe syntax of the if command is.Bd -literal -offset indentif listthen list[ elif listthen    list ] ...[ else list ]fi.Ed.PpThe syntax of the while command is.Bd -literal -offset indentwhile listdo   listdone.Ed.PpThe two lists are executed repeatedly while the exit status of thefirst list is zero.The until command is similar, but has the worduntil in place of while, which causes it torepeat until the exit status of the first list is zero..PpThe syntax of the for command is.Bd -literal -offset indentfor variable in word ...do   listdone.Ed.PpThe words are expanded, and then the list is executed repeatedly with thevariable set to each word in turn.do and done may be replaced with.Dq {and.Dq } ..PpThe syntax of the break and continue command is.Bd -literal -offset indentbreak [ num ]continue [ num ].Ed.PpBreak terminates the num innermost for or while loops.Continue continues with the next iteration of the innermost loop.These are implemented as builtin commands..PpThe syntax of the case command is.Bd -literal -offset indentcase word inpattern) list ;;\&...esac.Ed.PpThe pattern can actually be one or more patterns (see.Sx Shell Patternsdescribed later), separated by.Dq \*(Bacharacters..Ss Grouping Commands TogetherCommands may be grouped by writing either.Pp.Dl (list).Ppor.Pp.Dl { list; }.PpThe first of these executes the commands in a subshell.Builtin commands grouped into a (list) will not affect the current shell.The second form does not fork another shell so is slightly more efficient.Grouping commands together this way allows you to redirecttheir output as though they were one program:.Pp.Bd -literal -offset indent{ echo -n \*q hello \*q ; echo \*q world" ; } \*[Gt] greeting.Ed.PpNote that.Dq }must follow a control operator (here,.Dq \&; )so that it is recognized as a reserved word and not as another command argument..Ss FunctionsThe syntax of a function definition is.Pp.Dl name ( ) command.PpA function definition is an executable statement; when executed itinstalls a function named name and returns an exit status of zero.The command is normally a list enclosed between.Dq {and.Dq } ..PpVariables may be declared to be local to a function by using a localcommand.This should appear as the first statement of a function, and the syntax is.Pp.Dl local [ variable | - ] ....PpLocal is implemented as a builtin command..PpWhen a variable is made local, it inherits the initial value and exportedand readonly flags from the variable with the same name in the surroundingscope, if there is one.Otherwise, the variable is initially unset.The shell uses dynamic scoping, so that if you make the variable x local tofunction f, which then calls function g, references to the variable x madeinside g will refer to the variable x declared inside f, not to the globalvariable named x..PpThe only special parameter that can be made local is.Dq - .Making.Dq -local any shell options that are changed via the set command inside thefunction to be restored to their original values when the functionreturns..PpThe syntax of the return command is.Pp.Dl return [ exitstatus ].PpIt terminates the currently executing function.Return is implemented as a builtin command..Ss Variables and ParametersThe shell maintains a set of parameters.A parameter denoted by a name is called a variable.When starting up, the shell turns all the environmentvariables into shell variables.New variables can be set using the form.Pp.Dl name=value.PpVariables set by the user must have a name consisting solely ofalphabetics, numerics, and underscores - the first of which must not benumeric.A parameter can also be denoted by a number or a specialcharacter as explained below..Ss Positional ParametersA positional parameter is a parameter denoted by a number (n \*[Gt] 0).The shell sets these initially to the values of its command line argumentsthat follow the name of the shell script.The.Ic setbuiltin can also be used to set or reset them..Ss Special ParametersA special parameter is a parameter denoted by one of the following specialcharacters.The value of the parameter is listed next to its character..Bl -tag -width thinhyphena.It *Expands to the positional parameters, starting from one.When theexpansion occurs within a double-quoted string it expands to a singlefield with the value of each parameter separated by the first character ofthe.Ev IFSvariable, or by a.Aq spaceif.Ev IFSis unset..It @Expands to the positional parameters, starting from one.When the expansion occurs within double-quotes, each positionalparameter expands as a separate argument.If there are no positional parameters, theexpansion of @ generates zero arguments, even when @ isdouble-quoted.What this basically means, for example, isif $1 is.Dq abcand $2 is.Dq def ghi ,then.Qq $@expands tothe two arguments:.Pp.Sm off.Dl \*q abc \*q \  \*q def\ ghi \*q.Sm on.It #Expands to the number of positional parameters..It \&?Expands to the exit status of the most recent pipeline..It - (Hyphen.)Expands to the current option flags (the single-letteroption names concatenated into a string) as specified oninvocation, by the set builtin command, or implicitlyby the shell..It $Expands to the process ID of the invoked shell.A subshell retains the same value of $ as its parent..It \&!Expands to the process ID of the most recent backgroundcommand executed from the current shell.For a pipeline, the process ID is that of the last command in the pipeline..It 0 (Zero.)Expands to the name of the shell or shell script..El.Ss Word ExpansionsThis clause describes the various expansions that are performed on words.Not all expansions are performed on every word, as explained later..PpTilde expansions, parameter expansions, command substitutions, arithmeticexpansions, and quote removals that occur within a single word expand to asingle field.It is only field splitting or pathname expansion that cancreate multiple fields from a single word.The single exception to thisrule is the expansion of the special parameter @ within double-quotes, aswas described above..PpThe order of word expansion is:.Bl -enum.ItTilde Expansion, Parameter Expansion, Command Substitution,Arithmetic Expansion (these all occur at the same time)..ItField Splitting is performed on fieldsgenerated by step (1) unless the.Ev IFSvariable is null..ItPathname Expansion (unless set.Fl fis in effect)..ItQuote Removal..El.PpThe $ character is used to introduce parameter expansion, commandsubstitution, or arithmetic evaluation..Ss Tilde Expansion (substituting a user's home directory)A word beginning with an unquoted tilde character (~) issubjected to tilde expansion.All the characters up toa slash (/) or the end of the word are treated as a usernameand are replaced with the user's home directory.If the username is missing (as in.Pa ~/foobar ) ,the tilde is replaced with the value of the.Va HOMEvariable (the current user's home directory)..Ss Parameter ExpansionThe format for parameter expansion is as follows:.Pp.Dl ${expression}.Ppwhere expression consists of all characters until the matching.Dq } .Any.Dq }escaped by a backslash or within a quoted string, and characters inembedded arithmetic expansions, command substitutions, and variableexpansions, are not examined in determining the matching.Dq } ..PpThe simplest form for parameter expansion is:.Pp.Dl ${parameter}.PpThe value, if any, of parameter is substituted..PpThe parameter name or symbol can be enclosed in braces, which areoptional except for positional parameters with more than one digit orwhen parameter is followed by a character that could be interpreted aspart of the name.If a parameter expansion occurs inside double-quotes:.Bl -enum.ItPathname expansion is not performed on the results of the expansion..ItField splitting is not performed on the results of theexpansion, with the exception of @..El.PpIn addition, a parameter expansion can be modified by using one of thefollowing formats..Bl -tag -width aaparameterwordaaaaa.It ${parameter:-word}Use Default Values.If parameter is unset or null, the expansion of wordis substituted; otherwise, the value of parameter is substituted..It ${parameter:=word}Assign Default Values.If parameter is unset or null, the expansion ofword is assigned to parameter.In all cases, the final value of parameter is substituted.Only variables, not positional parameters or specialparameters, can be assigned in this way..It ${parameter:?[word]}Indicate Error if Null or Unset.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -