22

I want to source a file eg name.sh from another script and check the return code and decide on it's error code. If I use

source name.sh

then if name.sh return non-zero return code my main script stop to run but I need to decide on exit code weather to continue or stop.

If I use

ret_code="`source name.sh`"
echo $ret_code

then ret_code is null and no error code is printed. I can't use these:

sh name.sh
./name.sh
bash name.sh

because name.sh is not executable and I don't want it to be executable

4
  • Why not enclosing the executable code of "name.sh" in a function ? Commented May 21, 2013 at 11:49
  • @doukremt because my script should run a serial of other scripts in a directory Commented May 21, 2013 at 11:55
  • 1
    The backticks (``) spawn an subordinate shell which then executes source name.sh; this is equivalent to bash name.sh (assuming $SHELL is bash). Commented May 21, 2013 at 11:56
  • What do you think is a return code of a sourced file? When you execute exit command in the file being sourced then normally the shell will be terminated and no commands after the source command will be executed. When you are sourcing a file no new shell is started. --- The variants sh name.sh and bash name.sh do not require name.sh to be executable! You can use them if you do not need to modify your environment in name.sh and have it in the parent script. Commented Jul 19, 2023 at 9:16

3 Answers 3

18

File does not need to be executable to run sh name.sh. Than use $?.

sh name.sh
ret_code=$?
Sign up to request clarification or add additional context in comments.

1 Comment

I think that you misunderstand Amin's requirement. He is sourcing the file, not running it. The return value of the sourcing is simply the return value of the last command executed.
13

The return code should be in the variable $?. You can do the following:

source name.sh         # Execute shell script
ret_code=$?            # Capture return code
echo $ret_code

4 Comments

if i use source name.sh if name.sh contain exit 1 the code stop to continue
The purpose of using source is to run the script within the current shell. Hence, the exit will stop the current shell as well. Your original question had an explicit requirement to use source and I assumed that you know the consequences.
I have mentioned that if i use source what problem happen but how ever thank for your help
The answer does not say what the return code here is. --- In bash and in POSIX shells it is the return code of the last command in the sourced file. See: pubs.opengroup.org/onlinepubs/9699919799/utilities/…
1

Amin, the return value of name.sh is simply the return value of the last command executed. Access it using $? as usual.

The simplest approach to capturing a return code is to structure "name.sh" so that failed commands result in no more commands being executed (which might succeed and then mask the error). Since you are using bash, you can also use functions. Just move your logic into a function and call it. This way, your name.sh becomes a one-liner and the value of $? will be whatever the function return.

For example:

# cat fred
function f() {complicated; logic; here; export FRED="asdf";return 1;}
f
# source fred
# echo $?
1
# echo $FRED
asdf

However, if you put your logic into a function, then you might as well source the file and then call the function at your leisure.

# cat fred
function f() {complicated; logic; here; export FRED="asdf";return 1;}
# source fred
# f && echo "that worked!" || echo "failed again"
failed again
# echo $FRED
asdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.