0
我想獲取作爲參數傳遞的命令的退出狀態(到我的sh腳本a.sh
)。shell腳本:執行命令的退出狀態不正確
我已經試過:
#!/bin/sh
CMD="[email protected]"
echo "arg passed CMD: $CMD"
($CMD) >/dev/null 2>&1
res=$?
echo "exit status: $res"
CMD="echo aaa | grep -q zzz"
echo "in script CMD: $CMD"
($CMD) >/dev/null 2>&1
res=$?
echo "exit status: $res"
一旦執行:
./a.sh 'echo aa | grep -q zzz'
arg passed CMD: echo aa | grep -q zzz
exit status: 0
in script CMD: echo aaa | grep -q zzz
exit status: 0
但是如果我直接在shell中運行命令我看到:
/bin/sh -c 'echo aa | grep -q zzz ; echo $?'
1
應該怎樣我的劇本的樣子像,得到正確的狀態1
而不是0
的執行逗號第二?