2013-04-25 141 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