I have a bash script, where I call exit
somewhere to skip the rest of the script when getopts
doesn't recognize an option or doesn't find an expected option argument.
while getopts ":t:" opt; do
case $opt in
t)
timelen="$OPTARG"
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
# reset of the script
I source
the script in a bash shell. When something is wrong, the shell exits.
Is there some way other than exit
to skip the rest of the script but without exiting the invoking shell?
Replacing exit
with return
doesn't work like for a function call, and the rest of the script will runs.
Thanks.
return
seems to work for me. If I have a scriptecho hello ; return ; echo there
then source it I only get thehello
output.exit
inside a while loop. I guess replacing it withreturn
only exits the while loop, but continue to run the rest of the program after the while loop. How can I skip the rest of the program then?break
exits a loop,return
exits a function or a sourced script.