Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return an exit code without closing shell

I'd like to return an exit code from a BASH script that is called within another script, but could also be called directly. It roughly looks like this:

#!/bin/bash dq2-get $1 if [ $? -ne 0 ]; then   echo "ERROR: ..."   # EXIT HERE fi # extract, do some stuff # ... 

Now in the line EXIT HERE the script should exit and return exit code 1. The problem is that

  • I cannot use return, because when I forget to source the script instead of calling it, return will not exit, and the rest of the script will be executed and mess things up.
  • I cannot use exit, because this closes the shell.
  • I cannot use the nice trick kill -SIGINT $$, because this doesn't allow to return an exit code.

Is there any viable alternative that I have overlooked?

like image 278
fuenfundachtzig Avatar asked May 24 '11 15:05

fuenfundachtzig


People also ask

How do I exit shell script without exiting shell?

Use return . The return bash builtin will exit the sourced script without stopping the calling (parent/sourcing) script. Causes a function to stop executing and return the value specified by n to its caller.

Does grep return exit code?

Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if the -q or --quiet or --silent option is used and a line is selected, the exit status is 0 even if an error occurred. Other grep implementations may exit with status greater than 2 on error.

How do you get the last code out of Exit command?

Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.

How do you exit a running shell script?

Hold Ctrl and press p q .


2 Answers

The answer to the question title (not in the body as other answers have addressed) is:

Return an exit code without closing shell

(exit 33) 

If you need to have -e active and still avoid exiting the shell with a non-zero exit code, then do:

(exit 33) && true 

The true command is never executed but is used to build a compound command that is not exited by the -e shell flag.

That sets the exit code without exiting the shell (nor a sourced script).

For the more complex question of exiting (with an specific exit code) either if executed or sourced:

#!/bin/bash [ "$BASH_SOURCE" == "$0" ] &&     echo "This file is meant to be sourced, not executed" &&          exit 30  return 88 

Will set an exit code of 30 (with an error message) if executed.
And an exit code of 88 if sourced. Will exit both the execution or the sourcing without affecting the calling shell.

like image 189
IsaaC Avatar answered Sep 21 '22 22:09

IsaaC


Use this instead of exit or return:

[ $PS1 ] && return || exit; 

Works whether sourced or not.

like image 28
FractalSpace Avatar answered Sep 18 '22 22:09

FractalSpace