test.sh文件
ps -ef|grep gitbook|grep 24002|grep 4002
運行結果
root 1960 1948 7 14:25 ? 00:00:02 node /usr/local/bin/gitbook serve --lrport 24002 --port 4002
可以查到相應的進程
test2.sh文件(用變量作爲grep查詢條件)
lrport=24002
port=4002
ps -ef|grep gitbook|grep $lrport|grep $port
運行後查詢不到相關內容。查詢不到相關內容
排查問題原因:
用cat -A和bash -x命令查看test2.sh文件
cat -A test2.sh
lrport=24002^M$
port=4002^M$
ps -ef|grep gitbook|grep $lrport|grep $portr
bash -x test2.sh
+ lrport=$'24002\r'
+ port=$'4002\r'
+ grep $'4002\r'
+ grep $'24002\r'
+ grep gitbook
+ ps -ef
發現變量後都有換行符\r,而源文件中除去最後一行,每行後面都有'^M$’,網上查了一下最後發現了導致問題的最終原因:
test2.sh是在windows上編寫然後拷貝到linux上的,可能因爲兩者的編碼方式不一樣。
在linux上用vi重新編寫一份一摸一樣的文件,能夠正常查詢到結果。