bash shell 脚本语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash shell script syntax error
提问by Progress Programmer
I wrote a function in bash script. However, it's complaining about syntax. I really can't see what is it..... the error message is [: missing `]'
我在 bash 脚本中编写了一个函数。但是,它抱怨语法。我真的看不出是什么..... 错误信息是 [: missing `]'
addem() {
if [ $# -eq 0] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ + ]
else
echo $[ + ]
fi
}
回答by pugmarx
You need a space before the first ]. That is:
change:if [ $# -eq 0] || [ $# -gt 2 ]
to:if [ $# -eq 0 ] || [ $# -gt 2 ]
在第一个之前需要一个空格]。即:
将:更改if [ $# -eq 0] || [ $# -gt 2 ]
为:if [ $# -eq 0 ] || [ $# -gt 2 ]
回答by Sean
Try:
尝试:
if [ $# -eq 0 ] || [ $# -gt 2 ]
(There was no space between 0 and ].)
(0 和 ] 之间没有空格。)
回答by Stan Graves
indyK1ng: The "#" is not treated as a comment, since the "$" escapes the next character. The "$#" is an internal variable representing the number of positional parameters that exist at the current context. This can be thought of as the number of command line arguments to the shell script, but that array can be reset using the "set -- [args]" built in.
indyK1ng:“#”不被视为注释,因为“$”会转义下一个字符。“$#”是一个内部变量,表示当前上下文中存在的位置参数的数量。这可以被认为是 shell 脚本的命令行参数的数量,但可以使用内置的“set -- [args]”重置该数组。
Joakim Elofsson: The overall structure of the if statement is correct, the ";" is only required before the "then" and before the "fi" if those are not listed on a separate line.
Joakim Elofsson:if 语句的整体结构是正确的,“;” 如果没有在单独的行中列出,则只需要在“then”和“fi”之前。
The problem is the space between the "0" and the bracket. Bash requires that brackets used to delimit conditional expressions be set off with at least a single space from the expression.
问题是“0”和括号之间的空格。Bash 要求用于分隔条件表达式的括号与表达式中至少有一个空格。
if [ $# -eq 0] || [ $# -gt 2 ] # Wrong
if [ $# -eq 0 ] || [ $# -gt 2 ] # Correct
On an additional note, the two conditional expressions can be combined. The operator association will ensure that everything works out.
另外,这两个条件表达式可以组合使用。运营商协会将确保一切顺利。
if [ $# -eq 0 -a $# -gt 2 ] # Even Better
I tend to prefer the expanded features offered with double brackets for expression evaluation. Note that the combination of the two evaluations is done with a different operator. I find this to be more readable.
我倾向于使用双括号提供的扩展功能进行表达式评估。请注意,两个评估的组合是使用不同的运算符完成的。我觉得这更具可读性。
if [[ $# -eq 0 || $# -gt 2 ]] # My preference
Later in the script, the use of single brackets for integer addition is not recommended. The single brackets are evaluating an expression to a boolean. Double parens are used for integer math.
在脚本的后面,不推荐使用单括号进行整数加法。单括号将表达式计算为布尔值。双括号用于整数数学。
echo $[ + ] # Evaluation of an expression
echo $(( + )) # Integer math
回答by Jayan
I would use extended test constructs (BASH) as demonstrated bellow. I think it would reduce the no of characters and increase readability (at least for programmers). :-)
我将使用扩展测试结构 (BASH),如下所示。我认为它会减少字符数并提高可读性(至少对于程序员而言)。:-)
addem() {
if (( $# == 0 || $# > 2 ))
then
echo -1
elif (( $# == 1 ))
then
echo (( + ))
else
echo (( + ))
fi
}
回答by Steve B.
Bash is sensitive to spaces. In your first line, replace if [ Y -eq X] with [ Y -eq X ] (space before the "]")
Bash 对空格很敏感。在您的第一行中,将 if [ Y -eq X] 替换为 [ Y -eq X ] (“]”之前的空格)
回答by hendry
You should avoid brackets and use testinstead:
您应该避免使用括号并使用test:
if test $# -eq 0 || test $# -gt 2
then
echo -1
elif test $# -eq 1
then
echo $(( + ))
else
echo $(( + ))
fi
Getting a better shell stylewill make you much better. :)
获得更好的外壳样式会让你变得更好。:)

