【问题标题】:Integer expression expected error in shell scriptshell脚本中的整数表达式预期错误
【发布时间】:2013-10-21 21:36:49
【问题描述】:

我是 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 `]'

我不知道我做错了什么。如果有人能告诉我如何解决它,我将不胜感激,对不起我的英语不好,希望你们能理解我。

【问题讨论】:

  • 你通常不应该在命令的参数之间有空格吗?为什么省略了?
  • this page 解释了很多关于各种 bash 比较语法的内容。
  • 是整数还是空白?

标签: bash shell


【解决方案1】:

您可以使用以下语法:

#!/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

【讨论】:

  • 当我这样做时,我得到test.sh: line 3: [[: 109 test: syntax error in expression (error token is "test")
  • @BradenBest ,其他人:我遇到了类似的问题:
    while [ "$COUNTER+1" -lt "$array_length" ];做
    它给了我:有人可以帮助我吗。
【解决方案2】:

如果你使用-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。

【讨论】:

  • 是的,看看shebang,我假设OP使用bash。不过,很高兴指出 POSIX 解决方案,+1。
【解决方案3】:

如果您要比较的变量包含不是数字/数字的隐藏字符,也会发生此错误。

例如,如果您从第三方脚本中检索整数,则必须确保返回的字符串不包含hidden characters,例如"\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"。您必须使用此处的说明删除此字符:How to remove carriage return from a string in 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/

【讨论】:

  • 完美!我得到了93,(带逗号),这导致了这个错误。
【解决方案4】:
./bilet.sh: line 6: [: 7]: integer expression expected

小心" "

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

这是因为您需要在括号之间留出空格,例如:

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

看:加了空格,没有" "

【讨论】:

    【解决方案5】:

    试试这个:

    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 这些对我不起作用
    【解决方案6】:

    如果你只是比较数字,我认为没有必要更改语法,只需更正那些行,第 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" ]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多