shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与手动执行结果不一致
打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中。 先找出其中一台远程机器,查看main进程运行情况 1 2 3 4 5 [root@two002 tmp] # ps -ef|grep main root 23448 23422 0 11:40 pts /0 00:00:00 grep --color=auto main [root@two002 tmp] # ps -ef|grep main|grep -v grep|wc -l 0 shell检查脚本如下 1 2 3 4 5 6 7 8 [root@two002 tmp] # cat /tmp/main_check.sh #!/bin/bash NUM=$( ps -ef| grep main| grep - v grep | wc -l) if [ $NUM - eq 0 ]; then echo "It's not good! main is stoped!" else echo "Don't worry! main is running!" fi 执行脚本 1 2 3 4 5 6 7 8 9 10 11 12 [root@two002 tmp] # sh -x /tmp/main_check.sh ++ grep main ++ grep - v grep ++ wc -l ++ ps