[:: : bash 脚本上的错误数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8941874/
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
[: : bad number on the bash script
提问by Cyclone
This is my bash script:
这是我的 bash 脚本:
#!/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 }' | 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 }' | 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.shand then sh script.sh, then I receive the following message: [: : bad number.
Ofcourse我跑了chmod +x script.sh然后sh script.sh,然后我收到以下消息:[: : bad number。
Why does it happends?
为什么会发生?
回答by Uday
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.
它将打印它执行的每一行和变量输出。在外面运行netstat命令和stat命令并检查。
回答by Shiplu Mokaddim
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.
一个简单的黑客。仅当 $STAT 始终为空或正数时才有效。
回答by Michael Krelin - hacker
probably your /usr/local/www/recis empty. Try
可能你/usr/local/www/rec是空的。尝试
STAT=`cat /usr/local/www/rec 2>/dev/null || echo 0`
maybe.
也许。
回答by Michael Krelin - hacker
Are you sure that both STATand CHECKare numbers that can be compared with -gt?
您确定STAT和CHECK都是可以与 进行比较的数字-gt吗?

