2013-04-25 170 views
10

我有一個Bash父腳本,它在意外的輸入上調用記錄錯誤的錯誤記錄子腳本。我也希望在發生錯誤和調用錯誤腳本時停止執行。但是,如果我從錯誤處理腳本調用exit,它不會阻止父腳本執行。我該如何去阻止孩子的父母腳本?Bash - 從子腳本退出父腳本

回答

10

嘗試..

#normal flow 
[[ $(check_error_condition ]] && /some/error_reporter.sh || exit 1 

所以,

  • 當error_reporter將退出狀態退出> 0父將終止過
  • 如果error_reporter將與狀態退出= 0父繼續...

你不想stop the parent from a child父母通常不喜歡這種行爲):),你不是想tell to parent - need stop和他將停止本身(如果想);)

0

不要試圖終止孩子的父母。在子腳本返回後,請在父級中調用exit

if [ condition ]; then 
    /path/to/child.sh 
    exit 1 
fi 

或更短

[ condition ] && { /path/to/child.sh; exit 1; } 
6

嘗試:

在父腳本:

trap "echo exitting because my child killed me.>&2;exit" SIGUSR1 

在孩子的CRIPT:

kill -SIGUSR1 `ps --pid $$ -oppid=`; exit 

另一種方法是:

在孩子的腳本:

kill -9 `ps --pid $$ -oppid=`; exit 

但是,不建議,因爲父母需要有大約被殺&一些信息因此如果需要做一些清理。


另一種方式: 而不是調用子腳本,exec它的。


但是,正如在其他答案中指出的,最簡潔的方法是在孩子返回後從父母退出。

+2

從這裏開始看起來像在恐怖 - 孩子殺死父母... :) :)(但是,它也是一個解決方案):) :) – jm666 2013-04-25 13:12:54

+0

^^ Yups ....我曾經有一個要求像這樣:'command1 | (command 2; command3)'&command3應該用SIGINT來殺死command1 :-) – anishsane 2013-04-25 13:17:38