Revision 9031
Added by Aaron Marcuse-Kubitza about 11 years ago
util.sh | ||
---|---|---|
45 | 45 |
|
46 | 46 |
#### exceptions |
47 | 47 |
|
48 |
# usage: cmd || { save_e; ...; rethrow; }
|
|
49 |
alias save_e='e=$?'
|
|
48 |
# usage: cmd || { export_e; ...; rethrow; }
|
|
49 |
alias export_e='e=$?'
|
|
50 | 50 |
alias rethrow='return "$e"' |
51 | 51 |
alias rethrow_subshell='exit "$e"' |
52 | 52 |
|
... | ... | |
56 | 56 |
function log_e () # usage: log_e cmd... [ || use $e (or $?) ] |
57 | 57 |
{ |
58 | 58 |
if "$@"; then :; else # don't use ! because that resets $? |
59 |
save_e
|
|
59 |
export_e
|
|
60 | 60 |
echo "! $*" >&2 |
61 | 61 |
echo "! command exited with error $e" >&2 |
62 | 62 |
rethrow |
... | ... | |
66 | 66 |
|
67 | 67 |
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try |
68 | 68 |
|
69 |
function try () { e=0; "$@" || { save_e; true; }; }
|
|
69 |
function try () { e=0; "$@" || { export_e; true; }; }
|
|
70 | 70 |
alias try='declare e; try ' # last space alias-expands next word |
71 | 71 |
|
72 | 72 |
catch () { test "$e" -eq "$1"; e=0; } |
Also available in: Unified diff
lib/sh/util.sh: save_e: renamed to export_e because $e overwrites any previous value in the calling context