为什么在 Bash 中我的条件会出现“意外的运算符”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12262110/
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
Why do I get an "unexpected operator" error for my condition in Bash?
提问by batman
I got the code for monitoring apache. The name of the file is test.sh. I changed the code a bit.
我得到了监控 apache 的代码。该文件的名称是test.sh. 我稍微更改了代码。
What I was looking for is, when I do:
我正在寻找的是,当我这样做时:
./test.sh -H localhost -wr 2 -cr 5 -arg cpu_load
It should test apache for its cpu_load, i.e., I tried to control monitoring apache with my -argparameter, but that doesn't seem to be working.
它应该为它的 apache 测试cpu_load,即,我试图用我的-arg参数控制监控 apache ,但这似乎不起作用。
When I run this:
当我运行这个时:
./test.sh -H localhost -wr 2 -cr 5 -arg cpu_load
I get the error :
我收到错误:
./test.sh: 282: [: -ge: unexpected operator
./test.sh: 286: [: -ge: unexpected operator
Here is some part of the code:
这是代码的一部分:
#!/bin/sh
while test -n ""; do
case "" in
--help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version $PROGNAME $VERSION
exit $ST_UK
;;
--hostname|-H)
hostname=
shift
;;
--port|-P)
port=
shift
;;
--timeout|-t)
timeout=
shift
;;
--remote-server|-R)
remote_srv=1
;;
--binary_path|-b)
path_binary=
shift
;;
--pid_path|-p)
path_pid=
shift
;;
--pid_name|-n)
name_pid=
shift
;;
--status-page|-s)
status_page=
shift
;;
--secure|-S)
secure=1
;;
--warning-req|-wr)
warn_req=
shift
;;
--critical-req|-cr)
crit_req=
shift
;;
--userargument|-arg)
user_arg=
shift
;;
*)
echo "Unknown argument: "
print_help
exit $ST_UK
;;
esac
shift
done
#other codes
if [ ${wclvls_req} = 1 ]
then
if [ ${user_arg} -ge ${warn_req} -a ${user_arg} -lt ${crit_req} ]
then
echo "WARNING - ${output} | ${perfdata}"
exit $ST_WR
elif [ ${user_arg} -ge ${crit_req} ]
then
echo "CRITICAL - ${output} | ${perfdata}"
exit $ST_CR
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
fi
Where am I making the mistake?
我在哪里犯了错误?
回答by Rohan
One of your variables ( user_arg, warn_reqetc ) in the ifcondition might be empty.
条件中的变量 ( user_arg, warn_reqetc ) 之一if可能为空。
Better way to write that is with quoting the variables as (which may fail in your case if you want to compare as integers):
更好的写法是将变量引用为(如果您想作为整数进行比较,这在您的情况下可能会失败):
if [ "${user_arg}" -ge "${warn_req}" -a "${user_arg}" -lt "${crit_req}" ]
Or another way is to specify the default values so that if variable is null or undefined if won't fail as below.
或者另一种方法是指定默认值,以便如果变量为空或未定义,则不会失败,如下所示。
if [ ${user_arg:-0} -ge ${warn_req:-0} -a ${user_arg:-0} -lt ${crit_req:-0} ]
回答by chepner
If you don't need POSIX compatibility, you can use bash's arithmetic evaluation command instead:
如果您不需要 POSIX 兼容性,您可以使用 bash 的算术求值命令代替:
if (( user_arg >= 0 && user_arg < crit_req )); then
Unset variables will be implicitly treated as 0-valued, so using default value expansion is unnecessary.
未设置的变量将被隐式视为 0 值,因此不需要使用默认值扩展。

