简体   繁体   中英

How to get the exit code from the source script

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

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

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

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 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 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM