Shell脚本中出现了期望为整数表达式的错误。

43

我是一个shell脚本的新手,所以我有一个问题。在这段代码中我做错了什么?

#!/bin/bash
echo " Write in your age: "
read age
if [ "$age" -le "7"] -o [ "$age" -ge " 65" ]
then
echo " You can walk in for free "
elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]
then
echo " You have to pay for ticket "
fi

当我尝试打开这个脚本时,它要求我输入年龄,然后显示:

./bilet.sh: line 6: [: 7]: integer expression expected
./bilet.sh: line 9: [: missing `]'

我不知道我的做法有什么问题。如果有人能告诉我如何修复它,我将非常感激。对于我的糟糕英语,我很抱歉,希望你们能理解我。


1
你通常不应该在命令参数之间留有空格吗?你为什么省略了它? - Ignacio Vazquez-Abrams
2
这个网页解释了许多关于各种bash比较句法的内容。 - Stuart Mclean
它是整数还是空白? - neverMind9
6个回答

45

您可以使用以下语法:

#!/bin/bash

echo " Write in your age: "
read age

if [[ "$age" -le 7 || "$age" -ge 65 ]] ; then
    echo " You can walk in for free "
elif [[ "$age" -gt 7 && "$age" -lt 65 ]] ; then
    echo " You have to pay for ticket "
fi

5
当我这样做时,会得到test.sh:第3行:[[: 109 test:表达式中的语法错误(错误令牌为“test”) - Braden Best
@BradenBest ,其他人:我遇到了类似的问题:<br/>while [ "$COUNTER+1" -lt "$array_length" ]; do <br/> 它给了我:有人能帮帮我吗? - Bhavya Arora

16

如果你要比较的变量中有非数字/数字字符,也可能会出现此错误。

例如,如果从第三方脚本中检索整数,必须确保返回的字符串不包含换行符隐式字符, 如 "\n""\r"

例如:

#!/bin/bash

# Simulate an invalid number string returned
# from a script, which is "1234\n"
a='1234
'

if [ "$a" -gt 1233 ] ; then
    echo "number is bigger"
else
    echo "number is smaller"
fi

这将导致脚本错误: integer expression expected,因为$a包含一个非数字换行符"\n"。您需要使用此处的说明删除此字符:如何在Bash中从字符串中删除回车符 因此,请使用以下内容:
#!/bin/bash

# Simulate an invalid number string returned
# from a script, which is "1234\n"
a='1234
'

# Remove all new line, carriage return, tab characters
# from the string, to allow integer comparison
a="${a//[$'\t\r\n ']}"

if [ "$a" -gt 1233 ] ; then
    echo "number is bigger"
else
    echo "number is smaller"
fi

您还可以使用set -xv来调试您的bash脚本,并揭示这些隐藏字符。详情请见https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-error-integer-expression-expected-934465/


1
太好了!我得到了 93,(带逗号),这就导致了这个错误。 - Alexey Volodko

15
如果您正在使用-o(或-a),它需要在test命令的括号内:
if [ "$age" -le "7" -o "$age" -ge " 65" ]

然而,它们的使用已被弃用,您应该使用单独的 test 命令,通过 ||(或者 &&)连接起来。
if [ "$age" -le "7" ] || [ "$age" -ge " 65" ]

确保闭合括号前面有空格,因为它们实际上是 [ 的参数,而不仅仅是语法。
在 bash 和其他一些 shell 中,可以使用更强大的 [[ 表达式,如 kamituel's answer 所示。上述方法适用于任何符合 POSIX 标准的 shell。

1
是的,看到 shebang,我认为 OP 正在使用 bash。指向 POSIX 解决方案是个好主意,+1。 - kamituel

6
./bilet.sh: line 6: [: 7]: integer expression expected

小心使用 " "

./bilet.sh: line 9: [: missing `]'

这是因为你需要在括号之间加上空格,例如:
if [ "$age" -le 7 ] -o [ "$age" -ge 65 ]

看这里:添加了空格,并且没有" "


1

试试这个:

If [ $a -lt 4 ] || [ $a -gt 64 ] ; then \n
     Something something \n
elif [ $a -gt 4 ] || [ $a -lt 64 ] ; then \n
     Something something \n
else \n
    Yes it works for me :) \n

它对我有效,不要使用combian或不要使用-o -a。这些对我无效。 - Harry1992

0

如果你只是在比较数字,我认为没有必要改变语法,只需更正这些行的括号,第6行和第9行。

第6行之前:if [ "$age" -le "7"] -o [ "$age" -ge " 65" ]

之后:if [ "$age" -le "7" -o "$age" -ge "65" ]

第9行之前:elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]

之后:elif [ "$age" -gt "7" -a "$age" -lt "65" ]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接