今天要做一个计算,脚本如下:
#!/bin/bash HIT=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_hits" | awk -F‘:‘ ‘{print $2}‘) MISS=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_misses" | awk -F‘:‘ ‘{print $2}‘) all=`expr $HIT + $MISS` echo $all
运行报错:expr: non-numeric argument,最后各种测试,单独给HIT和MISS赋值,没报错,就是不知道哪里的原因,查看运行详细模式:
仔细一看,后面有"\r",以为是编码的问题
# cat -v 1.sh #!/bin/bash HIT=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_hits" | awk -F‘:‘ ‘{print $2}‘) MISS=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_misses" | awk -F‘:‘ ‘{print $2}‘) all=`expr $HIT + $MISS` echo $all
没发现哪里有问题,算了,还是先转一下吧:
yum install dos2unix
转格式:
# dos2unix 1.sh dos2unix: converting file 1.sh to UNIX format ...
再执行,还是不行:
# sh 1.sh expr: non-numeric argument
我想试试删除一下"\r"吧
sed -i ""s/\r//"" 1.sh
执行还是不行:
# sh 1.sh expr: non-numeric argument
最后恍然大悟,"\r"是计算结果里带出来的!!!
修改脚本:
# vim 1.sh #!/bin/bash HIT=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_hits" | awk -F‘:‘ ‘{print $2}‘) MISS=$(/app/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info | grep "keyspace_misses" | awk -F‘:‘ ‘{print $2}‘) echo $HIT echo $MISS HIT1=`echo $HIT | tr -d ‘\r‘` MISS1=`echo $MISS | tr -d ‘\r‘` all=`expr $HIT1 + $MISS1` echo $all
再执行:
真是FUCK了DOG了,竟然这个小问题,浪费了我半个小时,所以要认真!
时间: 2024-10-06 15:21:44