被excuted你好,我是新來shell script
,我需要處理error
從命令來進行excuted內echo
像下面shell腳本處理從一個命令是錯誤的,從迴音
echo -e "some internal command that I can't share \nq" | nc localhost 10000
我想說的
if [[ there's no error ]]
被excuted你好,我是新來shell script
,我需要處理error
從命令來進行excuted內echo
像下面shell腳本處理從一個命令是錯誤的,從迴音
echo -e "some internal command that I can't share \nq" | nc localhost 10000
我想說的
if [[ there's no error ]]
嘗試
echo "YOUR SUPERSECRET COMMAND" | nc localhost 10000 | grep "Your expected error"
if [[ $? -eq 0 ]]; then
echo "Do something useful with error"
else
echo "Success"
fi
grep
上匹配返回0,並返回1,當它沒有找到匹配的字符串。
shell變量$?
會給你退出代碼。所以,你可以這樣做:
echo -e "some internal command that I can't share \nq" | nc localhost 10000
rc=$?
if [[ $rc == 0 ]]; then
echo "success"
fi
或者乾脆,
if echo -e "some internal command that I can't share \nq" | nc localhost 10000; then
echo "success"
fi
這裏是做這件事的簡潔的方式:
internalcommand &>/dev/null 2>&1 && echo OK || echo FAILED
如果internalcommand
成功確定將被打印到標準輸出,如果失敗,失敗的原因是打印。
在Bash v4上測試的筆記
謝謝你的回答,但狀態碼$?無論是否存在錯誤,始終爲0 – Edevo
@Rifai_deve試試這個。 –