我有以下幾點:如何在shell條件中設置退出代碼?
[[email protected] SCB]# cat test_exit.sh
#!/usr/bin/ksh
if [[ -e "test_exit.sh" ]]; then
echo "No existential crisis here"
fake_command
if [[ $? -ne 0 ]] ; then
echo "You can't run fake commands"
exit 256
fi
else
echo "WTF?"
fi
[[email protected] SCB]# ./test_exit.sh
No existential crisis here
./test_exit.sh[7]: fake_command: not found [No such file or directory]
You can't run fake commands
[[email protected] SCB]# echo $?
0
我意料的是,我應該得到256
,不0
。
我似乎記得在某處讀到KornShell中的條件爲if
會產生子進程。起初,我認爲這可能是問題,但即使這樣也不能解釋它。如果我的記憶是正確的,那麼for
過程將退出$? == 256
。所有其他退出將是一個隱含的exit $?
,這將傳播256的價值回到原來的外殼。
任何人都可以解釋爲什麼我沒有看到我期待看到的256?
糟糕。只是做了一個小錯字。它被糾正了。 – 2011-03-15 23:50:28
每當我看到這個答案我facepalm :) – Dancrumb 2013-11-12 16:27:49