1. 程式人生 > >linux Shell 中grep+wc取值在shell中的結果與手動執行結果不一致的坑

linux Shell 中grep+wc取值在shell中的結果與手動執行結果不一致的坑

vim restart.sh

#!/bin/bash

VDS=ps -ef |grep -w vds|grep -v grep|wc -l

if [ $VDS -eq 0 ];then
cd /usr/local/program;./linux-start.sh restart
else
echo "It's ok,don't restart!"
fi

注:如果在shell視窗直接執行上面的變數賦值不加 -w沒有問題,但在腳本里,用 sh -x restart.sh 裡來看並不一致,解決方法就是加了 -w 引數,精確取值後Ok