bash shell脚本语法错误
我在 bash 脚本中编写了一个函数。 然而,它抱怨语法。 我真的看不出它是什么......错误消息是[:缺少`]'
addem() {
if [ $# -eq 0] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
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 `]'
addem() {
if [ $# -eq 0] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该避免使用括号并使用
test
代替:获取更好的 shell 样式会让你变得更好。 :)
You should avoid brackets and use
test
instead:Getting a better shell style will make you much better. :)
第一个
]
之前需要一个空格。 即:更改:
if [ $# -eq 0] || [ $# -gt 2 ]
至:
if [ $# -eq 0 ] || [ $# -gt 2 ]
You need a space before the first
]
. That is:change:
if [ $# -eq 0] || [ $# -gt 2 ]
to:
if [ $# -eq 0 ] || [ $# -gt 2 ]
尝试:
(0 和 ] 之间没有空格。)
Try:
(There was no space between 0 and ].)
indyK1ng:“#”不被视为注释,因为“$”转义了下一个字符。 “$#”是一个内部变量,表示当前上下文中存在的位置参数的数量。 这可以被认为是 shell 脚本的命令行参数的数量,但是可以使用内置的“set -- [args]”重置该数组。
Joakim Elofsson:if 语句的整体结构是正确的, “;” 仅当“then”和“fi”未在单独的行中列出时才需要在“then”之前和“fi”之前。
问题在于“0”和括号之间的空格。 Bash 要求用于分隔条件表达式的括号与表达式之间至少有一个空格。
另外需要注意的是,这两个条件表达式可以组合起来。 运营商协会将确保一切顺利。
我倾向于更喜欢用双括号提供的扩展功能来进行表达式求值。 请注意,两个评估的组合是使用不同的运算符完成的。 我发现这更具可读性。
在脚本的后面部分,不建议使用单括号进行整数加法。 单括号将表达式计算为布尔值。 双括号用于整数数学。
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.
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.
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.
On an additional note, the two conditional expressions can be combined. The operator association will ensure that everything works out.
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.
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.
Bash 对空格很敏感。 在第一行中,将 if [ Y -eq X] 替换为 [ Y -eq X ] (“]”之前的空格)
Bash is sensitive to spaces. In your first line, replace if [ Y -eq X] with [ Y -eq X ] (space before the "]")
我将使用扩展测试结构(BASH),如下所示。 我认为这会减少字符数量并提高可读性(至少对于程序员而言)。 :-)
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). :-)