Sample Header Ad - 728x90

How can I skip the rest of a script without exiting the invoking shell, when sourcing the script?

16 votes
4 answers
23272 views
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.
Asked by Tim (106420 rep)
Aug 2, 2018, 02:45 PM
Last activity: Nov 15, 2023, 11:39 PM