bash shell脚本中的整数表达式预期错误

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

Integer expression expected error in shell script

bashshell

提问by user2904832

I'm a newbie to shell scripts so I have a question. What Im doing wrong in this code?

我是 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

When I'm trying to open this script it asks me for my age and then it says

当我试图打开这个脚本时,它会询问我的年龄,然后它说

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

I don't have any idea what I'm doing wrong. If someone could tell me how to fix it I would be thankful, sorry for my poor English I hope you guys can understand me.

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

回答by kamituel

You can use this syntax:

您可以使用以下语法:

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

回答by chepner

If you are using -o(or -a), it needs to be inside the brackets of the testcommand:

如果您使用-o(或-a),它需要在test命令的括号内:

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

However, their use is deprecated, and you should use separate testcommands joined by ||(or &&) instead:

但是,不推荐使用它们,您应该使用test||(或&&)连接的单独命令:

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

Make sure the closing brackets are preceded with whitespace, as they are technically arguments to [, not simply syntax.

确保右括号前面有空格,因为它们是 的技术参数[,而不仅仅是语法。

In bashand some other shells, you can use the superior [[expression as shown in kamituel's answer. The above will work in any POSIX-compliant shell.

bash和其他一些 shell 中,您可以使用高级[[表达式,如kamituel's answer所示。以上将在任何符合 POSIX 的 shell 中工作。

回答by Mr-IDE

This error can also happen if the variable you are comparing has hidden characters that are not numbers/digits.

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

For example, if you are retrieving an integer from a third-party script, you must ensure that the returned string does not contain hidden characters, like "\n"or "\r".

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

For example:

例如:

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

This will result in a script error : integer expression expectedbecause $acontains a non-digit newline character "\n". You have to remove this character using the instructions here: How to remove carriage return from a string in Bash

这将导致脚本错误,: integer expression expected因为$a包含非数字换行符"\n"。您必须使用此处的说明删除此字符:如何从 Bash 中的字符串中删除回车符

So use something like this:

所以使用这样的东西:

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

You can also use set -xvto debug your bash script and reveal these hidden characters. See https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-error-integer-expression-expected-934465/

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

回答by muha

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

Be careful with " "

小心 " "

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

This is because you need to have space between brackets like:

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

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

look: added space, and no " "

看:增加了空间,没有 " "

回答by Harry1992

Try this:

尝试这个:

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

回答by markfree

If you are just comparing numbers, I think there's no need to change syntax, just correct those lines, lines 6 and 9 brackets.

如果您只是比较数字,我认为无需更改语法,只需更正那些行、第 6 行和第 9 行括号即可。

Line 6 before: if [ "$age" -le "7"]-o [ "$age" -ge " 65"]

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

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

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

Line 9 before: elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]

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

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

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