I have a parent function and child function:
parent() {
child aa1 bb1 cc1
child aa2 bb2 cc2
child aa3 bb3 cc3
child aa4 bb4 cc4
}
child() {
....
if [ some error reason ]; then return 1; fi
...
}
How to make the return 1 (of the child) cause a return in the parent and then avoid the execute the remaining child calls?
without adding a check of the returned value after each child call like this
parent() {
child aa1 bb1 cc1
e=$?; [ "$e" != "0" ] && return $e
child aa2 bb2 cc2
e=$?; [ "$e" != "0" ] && return $e
child aa3 bb3 cc3
e=$?; [ "$e" != "0" ] && return $e
child aa4 bb4 cc4
e=$?; [ "$e" != "0" ] && return $e
}
Advertisement
Answer
There is a solution other than set -e and || return
It’s simple, just use exit instead of return and the parent should called in a sub-shell:
parent() {
child aa1 bb1 cc1
child aa2 bb2 cc2
child aa3 bb3 cc3
child aa4 bb4 cc4
}
child() {
....
if [ some error reason ]; then exit 1; fi
...
}
And I call the parent in this way:
( parent )