1

I have two scripts main script and sub script and I called the subscript using source script, if the specified package is not installed then it should return exit code 1.

If I run the main script by using bash main.sh I am unable to get the subScriptExitCode from the main script

main script

source "sub.sh"

subScriptExitCode=$?

log "subScript ExitCode: $subScriptExitCode"

if [ $subScriptExitCode -ne 0 ]; then
    exit $subScriptExitCode
fi

sub script

type -p <package>

subScriptExitCode=$?

if [ $subScriptExitCode -ne 0 ]; then
    exit 1
fi
1
  • 2
    You may prefer to use return instead of exit inside sub.sh Commented Jul 31, 2018 at 13:30

3 Answers 3

6

When a file is sourced, don't use exit, as this will terminate the whole execution. Instead, use return in sub.sh :

return [n]

Causes a function to exit with the return value specified by n. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false.

sub.sh

type -p <package>

subScriptExitCode="$?"

if [ "$subScriptExitCode" -ne 0 ]; then
    return 1
fi
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of sourcing the sub script run it as below and check the return code

Main.sh

sh sub.sh

subScriptExitCode=$?

log "subScript ExitCode: $subScriptExitCode"

if [ $subScriptExitCode -ne 0 ]; then
    exit $subScriptExitCode
fi

Comments

1

If you have a look at the manual of Bash, then you read

source filename [arguments]: Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain ...

source: man bash

These are two very important properties which are related to your problem:

  1. If your sub_script encounters a subScriptExitCode which is different from zero. It will terminate the main_script instantaneously due to the exit statement.

  2. The main_script will set subScriptExitCode to the exit state of the if-statement. This is zero in case subScriuptExitCode of sub_script equals 0.

    if list; then list; [ elif list; then list; ] ... [ else list; ] fi: ... The exit status is the exit status of the last command executed, or zero if no condition tested true.

    source: man bash

A possible way to solve your problem, making use only of the properties of source would be:

sub_script:

type -p <package>
[ $? -eq 0 ]

Here, the test command will exit with the state 0 if type p <package> terminated with zero, otherwise the test-command will exit with state 1. This state is then picked up in your main_source as $?. However, since type -p can only return 0 or 1, you can just get rid of the test and reduce sub_script to:

type -p <package>

type [-aftpP] name [name ...]: ... type returns true if all of the arguments are found, false if any are not found.

[source: man bash]

2 Comments

[ $? -eq 0 ] only changes all nonzero values to 1; it serves no other useful purpose -- which is to say, sub_script's exit status will be that of the last command, type -p, without it.
@CharlesDuffy I just realized what you meant. I have updated the post to reflect this.

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.