为什么我会得到“意外的运算符"?我在Bash中的状况有误吗? [英] Why do I get an "unexpected operator" error for my condition in Bash?

查看:112
本文介绍了为什么我会得到“意外的运算符"?我在Bash中的状况有误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了用于监视Apache的代码.文件名是test.sh.我对代码做了一点改动.

I got the code for monitoring apache. The name of the file is test.sh. I changed the code a bit.

我要找的是什么时候做的:

What I was looking for is, when I do:

./test.sh -H localhost -wr 2 -cr 5 -arg cpu_load

它应该测试apache的cpu_load,即我试图用我的-arg参数控制监视apache,但这似乎不起作用.

It should test apache for its cpu_load, i.e., I tried to control monitoring apache with my -arg parameter, but that doesn't seem to be working.

运行此命令时:

./test.sh -H localhost -wr 2 -cr 5 -arg cpu_load

我收到错误消息:

./test.sh: 282: [: -ge: unexpected operator
./test.sh: 286: [: -ge: unexpected operator

这是代码的一部分:

#!/bin/sh


while test -n "$1"; do
    case "$1" in
        --help|-h)
            print_help
            exit $ST_UK
            ;;
        --version|-v)
            print_version $PROGNAME $VERSION
            exit $ST_UK
            ;;
        --hostname|-H)
            hostname=$2
            shift
            ;;
        --port|-P)
            port=$2
            shift
            ;;
        --timeout|-t)
            timeout=$2
            shift
            ;;
        --remote-server|-R)
            remote_srv=1
            ;;
        --binary_path|-b)
            path_binary=$2
            shift
            ;;
        --pid_path|-p)
            path_pid=$2
            shift
            ;;
        --pid_name|-n)
            name_pid=$2
            shift
            ;;
        --status-page|-s)
            status_page=$2
            shift
            ;;
        --secure|-S)
            secure=1
            ;;
        --warning-req|-wr)
            warn_req=$2
            shift
            ;;
        --critical-req|-cr)
            crit_req=$2
            shift
            ;;
    --userargument|-arg)
       user_arg=$3
       shift
           ;;
        *)
            echo "Unknown argument: $1"
            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

我在哪里犯错?

推荐答案

if条件下的变量之一(user_argwarn_req等)可能为空.

One of your variables ( user_arg, warn_req etc ) in the if condition might be empty.

更好的编写方式是将变量引号为(如果要比较为整数,在您的情况下可能会失败):

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}" ]

另一种方法是指定默认值,以使变量为null或未定义时不会失败,如下所示.

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} ]

这篇关于为什么我会得到“意外的运算符"?我在Bash中的状况有误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆