If statement fails with integer expression expected

Below is what i have in my script.

htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.mypc.com:887/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-887\" |grep sBusy | cut -d ' ' -f2)
echo $htcount
if [ $htcount -ge 10 ]; then 
echo "more than 10"
else
echo "Less than 10"
fi

Output:

3
: integer expression expected/tmp/logs/myscript1.sh: line 11: [: 3

I tried various fixes like expr and quotes but i cannot overcome this error as seen in the output.

bash$: uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Kindly suggest.

there's probably a hidden/trailing space in htcount.
Could you try and post the output?

htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.mypc.com:887/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-887\" |grep sBusy | cut -d ' ' -f2)
echo "[${htcount}]"

also modify the if line to this:

if [ "${htcount}" -ge 10 ]; then 

After implementing your suggestion echo "[${htcount}]" here is the new output.

]1
: integer expression expected/tmp/logs/myscript1.sh: line 11: [: 3 

well.... that tells me that your curl ... | grep ... | cut ... is not returning what you think it's returning.
You need to debug how the htcount is derived.

Oh no, hang on - this doesn't make much sense... the echo "[${htcount}]" is supposed to return the value inside , but it isn't.
The issue is somewhere else.
Add set -x at the top of your script and see what comes out.
And change
echo "[${htcount}]" to echo "htcount->[${htcount}]"

1 Like
more /tmp/script1.sh
#!/bin/bash
set -x
htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.myc.com:443/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-443\" |grep currentThreadsBusy | cut -d ' ' -f2)
echo "htcount->[${htcount}]"
if [ "${htcount}" -ge 10 ]; then
echo "ALERT ::: "
else
echo "We are good"
fi

Output:

+++ hostname
++ grep currentThreadsBusy
++ cut -d ' ' -f2
++ curl -s --user tomcatstatus:tomcatstatus 'http://mymac.myc.com:443/manager/jmxproxy?qry=Catalina:type=ThreadPool,name="http-nio-443"'
+ htcount=$'1\r'
]'echo 'htcount->[1
]tcount->[1
+ '[' $'1\r' -ge 10 ']'
: integer expression expected
+ echo 'We are good'
We are good

No. What you're seeing is evidence that the output from the curl command is using DOS <carriage-return><newline> line terminators instead of UNIX <newline> line terminators. The echo output is showing that htcount has been set to " 1<cr> " (where <cr> is a carriage return character). Since <cr> is not a numeric character, the test utility is complaining that "${htcount}" expands to a non-numeric string that is not valid in an operand of the binary -ge test operator. Note that an easier way to see this is to use:

printf '%s' "$htcount" | od -bc

Try adding:

tr -d '\r'

to the pipeline before or after the grep command.

1 Like

@Don: Thank you for the explanation.

tr -d '\r'

resolved the issue.