Cyclone
Cyclone

Reputation: 15279

[: : bad number on the bash script

This is my bash script:

#!/usr/local/bin/bash -x

touch /usr/local/p
touch /usr/local/rec
DATA_FULL=`date +%Y.%m.%d.%H`
CHECK=`netstat -an | grep ESTAB | egrep '(13001|13002|13003|13004|13061|13099|16001|16002|16003|16004|16061|16099|18001|18002|18003|18004|18061|18099|20001|20002|20003|20004|20061|20099|13000|16000|18000|20000)' | awk '{ print $5 }' | sort -u | wc -l`
netstat -an | grep ESTAB | egrep '(13001|13002|13003|13004|13061|13099|16001|16002|16003|16004|16061|16099|18001|18002|18003|18004|18061|18099|20001|20002|20003|20004|20061|20099|13000|16000|18000|20000)' | awk '{ print $5 }' | sort -u | wc -l > /usr/local/www/p

STAT=`cat /usr/local/www/rec`
if [ "$CHECK" -gt "$STAT" ]; then
echo $CHECK"\n"$DATA_FULL > /usr/local/p
fi

Ofcourse I've runned chmod +x script.sh and then sh script.sh, then I receive the following message: [: : bad number.

Why does it happends?

Upvotes: 1

Views: 8526

Answers (4)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57680

If these are integer for sure, use this syntax,

if [ "0$(echo $CHECK|tr -d ' ')" -gt "0$(echo $STAT|tr -d ' ')" ];

A simple hack. Only works if $STAT is always either empty or positive number.

Upvotes: 1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143249

probably your /usr/local/www/rec is empty. Try

STAT=`cat /usr/local/www/rec 2>/dev/null || echo 0`

maybe.

Upvotes: 1

Udayaditya Barua
Udayaditya Barua

Reputation: 1162

Run your script using

sh -x script.sh

It'll print every line it executes and the variable output. Run the netstat command and stat command outside and check.

Upvotes: 3

user647772
user647772

Reputation:

Are you sure that both STAT and CHECK are numbers that can be compared with -gt?

Upvotes: 1

Related Questions