
正文
shell计算小问题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.shell处理两数相加时报错:
req_all=$(($hits+$misses))
error: invalid arithmetic operator (error token is "
查询原因发现:
hits=`$REDISCLI -h $IP -p $PORT info 2>&1 |grep -w keyspace_hits |cut -d : -f2`
misses=`$REDISCLI -h $IP -p $PORT info 2>&1 |grep -w keyspace_misses |cut -d : -f2`
echo $hits
echo $misses
变量的值结尾需要处理,所以要计算的话要用下面的方式
req_all=$((${hits//$'\r'}+${misses//$'\r'}))
计算百分比的方法:
hit_ratio=$(printf "%0.1f" $((${hits//$'\r'}*100/${req_all})))
或
hit_ratio=`awk 'BEGIN{printf "%.2f\n",('${hits//$'\r'}'/'${req_all}')*100}'`






![【电子书】[职业技术] 《Linux命令行与shell脚本编程大全(第3版)》[Richard Blum][epub+mobi+azw3] 【电子书】[职业技术] 《Linux命令行与shell脚本编程大全(第3版)》[Richard Blum][epub+mobi+azw3]](https://www.04ip.com/template/qe/style/noimg/19.jpg)

