次のスクリプトがありますjudge
。
#!/bin/bash
echo "last exit status is $?"
常に「最後の終了状態がゼロでした」を出力します。たとえば、
ls -l; judge # correctly reports 0
ls -z; judge # incorrectly reports 0
beedogs; judge # incorrectly reports 0
なぜ?
ベストアンサー1
各コード行は異なる bash プロセスによって実行され、$?
プロセス間で共有されません。judge
bash関数を生成することでこの問題を解決できます。
[root@xxx httpd]# type judge
judge is a function
judge ()
{
echo "last exit status is $?"
}
[root@xxx httpd]# ls -l / >/dev/null 2>&1; judge
last exit status is 0
[root@xxx httpd]# ls -l /doesntExist >/dev/null 2>&1; judge
last exit status is 2
[root@xxx httpd]#