- 论坛徽章:
- 1
|
To waker兄。
您的说法我部分同意,另外的部分则不能苟同。^_^
* shell脚本可以不在首行指定解释器,缺省的解释器/bin/sh会被用来解释、执行它。这一点你说的是对的,又是我孤陋寡闻了,受教。
* |不是重定向的操作符,这点也同意,我说错了。
至于赋值操作能不能算作命令,我的理解和你不同。
bash man page没有明确的说法,那让我们看看ksh的。下面摘自是Korn shell 93的man page:
...
Commands.
A simple-command is a list of variable assignments (see VariableAssignments below) or a sequence of blank separated words which may be preceded by a list of variable assignments (see Environment below). The first word specifies the name of the command to be executed. Except as specified below, the remaining words are passed as arguments to the invoked command. ...
第一句就明确指出赋值的列表是simple-command的一种。
再看另一段(抱歉,做这样大段的引述):
...
A command is either a simple-command or one of the following. Unless otherwise stated, the value returned by a command is that of the last simple-command executed in the command.
for vname [ in word . . . ] ;do list ;done
Each time a for command is executed, vname is set to the next word taken from the in word list. If in word . . . is omitted, then the for command executes the do list once for each positional parameter that is set starting from 1 (see ParameterExpansion below). Execution ends when there are no more words in the list.
for (( [ expr1 ] ; [ expr2 ] ; [ expr3 ] )) ;do list ;done
The arithmetic expression expr1 is evaluated first (see ArithmeticEvaluation below). The arithmetic expression expr2 is repeatedly evaluated until it evaluates to zero and when non-zero, list is executed and the arithmetic expression expr3 evaluated. If any expression is omitted, then it behaves as if it evaluated to 1.
select vname [ in word . . . ] ;do list ;done
A select command prints on standard error (file descriptor 2) the set of words, each preceded by a number. If in word . . . is omitted, then the positional parameters starting from 1 are used instead (see ParameterExpansion below). The PS3 prompt is printed and a line is read from the standard input. If this line consists of the number of one of the listed words, then the value of the variable vname is set to the word corresponding to this number. If this line is empty, the selection list is printed again. Otherwise the value of the variable vname is set to null. The contents of the line read from standard input is saved in the variable REPLY. The list is executed for each selection until a break or end-of-file is encountered. If the REPLY variable is set to null by the execution of list, then the selection list is printed before displaying the PS3 prompt for the next selection.
case word in [ [ ( ]pattern [ | pattern ] . . . ) list ;; ] . . . esac
A case command executes the list associated with the first pattern that matches word. The form of the patterns is the same as that used for file-name generation (see FileNameGeneration below). The ;; operator causes execution of case to terminate. If ;& is used in place of ;; the next subsequent list, if any, is executed.
if list ;then list [ elif list ;then list ] . . . [ ;else list ] ;fi
The list following if is executed and, if it returns a zero exit status, the list following the first then is executed. Otherwise, the list following elif is executed and, if its value is zero, the list following the next then is executed. Failing each successive elif list , the else list is executed. If the if list has non-zero exit status and there is no else list, then the if command returns a zero exit status.
while list ;do list ;done
until list ;do list ;done
A while command repeatedly executes the while list and, if the exit status of the last command in the list is zero, executes the do list; otherwise the loop terminates. If no commands in the do list are executed, then the while command returns a zero exit status; until may be used in place of while to negate the loop termination test.
((expression ))
The expression is evaluated using the rules for arithmetic evaluation described below. If the value of the arithmetic expression is non-zero, the exit status is 0, otherwise the exit status is 1.
(list )
Execute list in a separate environment. Note, that if two adjacent open parentheses are needed for nesting, a space must be inserted to avoid evaluation as an arithmetic command as described above.
{ list ;}
list is simply executed. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur at the beginning of a line or after a ; in order to be recognized.
[[ expression ]]
Evaluates expression and returns a zero exit status when expression is true. See ConditionalExpressions below, for a description of expression.
function varname { list ;}
varname () { list ;}
Define a function which is referenced by varname. A function whose varname contains a . is called a discipline function and the portion of the varname preceding the last . must refer to an existing variable. The body of the function is the list of commands between { and }. A function defined with the function varname syntax can also be used as an argument to the . special built-in command to get the equivalent behavior as if the varname () syntax were used to define it. (See Functions below.)
time [ pipeline ]
If pipeline is omitted the user and system time for the current shell and completed child processes is printed on standard error. Otherwise, pipeline is executed and the elapsed time as well as the user and system time are printed on standard error.
The following reserved words are recognized as reserved only when they are the first word of a command and are not quoted:
if then else elif fi case esac for while until do done { } function select time [[ ]] !
...
看到吗?连[[...]], ((...))也被承认是command(*not* just statement)。^_^
再看看linux下的ash的man page:
...
Simple Commands
If a simple command has been recognized, the shell performs the following actions:
1. Leading words of the form "name=value" are stripped off and assigned to the environment of the simple com-
mand. Redirection operators and their arguments (as described below) are stripped off and saved for pro-
cessing.
2. The remaining words are expanded as described in the section called "Expansions", and the first remaining
word is considered the command name and the command is located. The remaining words are considered the
arguments of the command. If no command name resulted, then the "name=value" variable assignments recog-
nized in item 1 affect the current shell.
...
其中也承认只有赋值命令而没有命令名的“Simple Commands”。
呵呵,太长了!先贴这么多,其余的下一回贴再贴。^_^
[ 本帖最后由 woodie 于 2006-4-20 20:11 编辑 ] |
|