BASH 中的“[:too many arguments”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28127253/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:15:01  来源:igfitidea点击:

"[:too many arguments" error in BASH

bashparameters

提问by Anurag Iyer

I'm learning BASH through HackerRank.There's an exercise in which the lengths of the triangle is given and then you need to find whether the triangle is isosceles,scalene or equilateral.I wrote the following code:

我正在通过HackerRank学习BASH。有一个练习,其中给出了三角形的长度,然后您需要找出三角形是等腰,不等边还是等边。我写了以下代码:

read a
read b
read c
if  [ [ "$a" -eq  "$b" ] && [ "$b" -eq  "$c" ] ]  
then 
    echo "EQUILATERAL"
    elif  [ [ "$a" -eq "$b" ] || [ "$b" -eq  "$c" ] ]  
    then
    echo "ISOSCELES"
    else
    echo "SCALENE"
    fi

But then I get the following error

但后来我收到以下错误

solution.sh: line 4: [: too many arguments
solution.sh: line 7: [: too many arguments
solution.sh: line 7: [: too many arguments

Why is this happening? I tried long and hard to rectify it but nothing worked out

为什么会这样?我尝试了很长时间并努力纠正它,但没有任何效果

回答by nlu

You can combine conditions either ommiting the surrounding brackets like this

您可以组合条件,或者像这样省略周围的括号

if [ "$a" -eq  "$b" ] && [ "$b" -eq  "$c" ]

or by combining the conditions with -a/-o like this

或者像这样将条件与 -a/-o 结合使用

if [ "$a" -eq  "$b" -a "$b" -eq  "$c" ]

see http://wiki.bash-hackers.org/commands/classictest#and_and_or

http://wiki.bash-hackers.org/commands/classictest#and_and_or

回答by Michael Jaros

&&and ||are Bash list operators. In a chain of commands, the next command is executed only if the previous command returned 0 (&&) or nonzero (||).

&&||是 Bash 列表运算符。在命令链中,仅当上一个命令返回 0 ( &&) 或非零 ( ||) 时,才会执行下一个命令。

[is an alias for the Bash internal testcommand and has arguments such as -eqor -ne. ]ends its command line. Type help testfor more information.

[是 Bash 内部test命令的别名,并具有诸如-eq或 之类的参数-ne]结束它的命令行。键入help test以获取更多信息。

So if you write a conditional expression, you do not put the list operators inside brackets.

因此,如果您编写条件表达式,则不要将列表运算符放在括号内。

Try, for example, this instead of the respective line in your code:

例如,尝试使用以下代码代替代码中的相应行:

if  [ "$a" -eq  "$b" ] && [ "$b" -eq  "$c" ]
then

回答by Jasen

[isn't a grouping operator in bash, you can't use it to group tests.

[不是 bash 中的分组运算符,您不能使用它对测试进行分组。

there are a number of different ways to express the tests you want to make, numeric evaluation mode is probably easiest to read

有许多不同的方式来表达您想要进行的测试,数字评估模式可能最容易阅读

 if (( a == b && b == c ))

 if (( a == b || b == c || c == a ))

This is going to break if you have decimal fractions, but will work fine for integers.

如果您有小数,这将会中断,但对于整数来说可以正常工作。

回答by Zulu

[is a conditional command, like an alias for sh's testbuilt-in command.

[是一个条件命令,就像shtest内置命令的别名。

[[is the same for bashwhich has more test options.

[[是相同的bash,有更多的测试选项。

So make a choice between [and [[but not [ [which means two command.

所以在[and[[但不是[ [这意味着两个命令之间做出选择。

Example:

例子:

# [ [ -n 'test' ] ]
bash: [: too many arguments
# [ -n 'test' ] && echo $?
0
# [[ -n 'test' ]] && echo $?
0