Le 24/05/2010 17:05, Lenga, Yair a écrit: > I would like to propose an improvement to the bash error handling: > Add flag/option "err_return", that will cause a user defined shell > functions to immediately return, when a simple command will return a > non-zero status code. > The behavior is similar to the existing '-e', but is different in > the following case. Under current bash, the "Abort On Error" logic > is suspended if the command is executed under "if", "while", "||" or > similar. It will make it possible to implement much better error > handling for scripts that involve user defined functions.
I agree, this looks like it could be an extremely useful feature. The recurring and intricate discussions on this list clearly demonstrate that "errexit": - draws a lot of interest, - has a broken design. Some examples (randomly picked up among many others) http://thread.gmane.org/gmane.comp.shells.bash.bugs/6071 http://thread.gmane.org/gmane.comp.shells.bash.bugs/12117 http://thread.gmane.org/gmane.comp.shells.bash.bugs/12281 http://thread.gmane.org/gmane.comp.shells.bash.bugs/13726 http://thread.gmane.org/gmane.comp.shells.bash.bugs/13465 Since "errexit" is hopelessly caught forever in design by committee and backward compatibility traps, a brand new "errreturn" feature could provide something useful instead. This is basically about some very minimal, poor-man's exceptions. An extremely basic feature to stop code running wild when something unexpected and bad happens. Only prehistoric or low-level, operating systems languages like C miss such a feature. > Adding error handling to this script can be done in two ways: > 1 - Modifying each functions/script: > Function f1 { > cp /new/data /current/data || return 1 > step1 || return 1 > step2 || return 1 > } I find this slightly better: Function f1 { cp /new/data /current/data || return $? step1 || return $? step2 || return $? }