In the code below, the "if" statement never succeeds because even if getopt
returns a non-0 exit code, the local
assignment replaces it with a 0 exit code.
function usage {
echo "Usage: $(basename $0) [ --help | -? | -h ] | [ --email-to --email-sender ]"
}
function parse_args()
if ! local -r parsed_args=$(getopt --long email-sender:,email-to:,help -o h -- $@); then
usage
exit 1
fi
# process $parsed_pargs
}
For example:
function inner {
echo "failed!"
return 1
}
function outer {
local -r result=$(inner)
echo "exit status=$?"
echo "result='$result'"
inner
echo "2nd exit status=$?"
}
outer
Prints to the console:
exit status=0
result='failed!'
failed!
2nd exit status=1
Is there a workaround for this?