batman
batman

Reputation: 4908

Why do I get an "unexpected operator" error for my condition in Bash?

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

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.

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 "$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

Where am I making the mistake?

Upvotes: 2

Views: 13630

Answers (2)

chepner
chepner

Reputation: 530970

If you don't need POSIX compatibility, you can use bash's arithmetic evaluation command instead:

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.

Upvotes: 3

Rohan
Rohan

Reputation: 53326

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

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

Upvotes: 7

Related Questions