1

I'm having a hard time getting the return value from a script. The script that is giving me trouble is second.sh below. But there's an interaction present with a script called first.sh, and that interaction seems to be the source of the trouble.

The first script is a test script and drives the second script. It needs the second script to set a cross-compile environment (hence the reason for a leading dot). The first script then invokes make with various variables set, like CXX and CXXFLAGS.

# first.sh

. ./second.sh arg1
if [ "$?" -eq "0" ]; then
    make
    ...
fi

. ./second.sh arg2
if [ "$?" -eq "0" ]; then
    make
    ...
fi

. ./second.sh arg3
if [ "$?" -eq "0" ]; then
    make
    ...
fi

The second script is usually run stand-alone by the user, and it's quite involved because it's detecting platforms, architectures and other miscellaneous features. It sets various variables, like CXX and CXXFLAGS used by make in the first script.

# second.sh
...

# if success, return 0
exit 0
...

# if failure, return non-0
exit 1

The problem I am having is the call to exit in second.sh kills the terminal, so first.sh is stopped dead in its tracks. A similar problem occurs when the user runs it stand alone.

When I attempt to change the second script to:

# second.sh
...

# if success, return 0
return 0

# else for failure, return non-0
return 1

Then, from first.sh perspective, second.sh always fails, even with return 0.

How do I add a return code that does not kill the terminal under Bash?


To date, I've been running without the first test script, so the second script does not need to return success/failure. But I'm trying to automate testing, so I really need to be able to get a return code

  • Because you dotted it in, it's exiting script 1. Perhaps organize script into functions ? – Jeff Schaller Mar 9 '16 at 3:27
  • I've a question for you. Do you really need to source your script? It is not enough to execute them? – Hastur Mar 10 '16 at 10:14
2

One perhaps not ideal way is to do alias exit=return, which then makes the second.sh exits do script return instead, with the code as given. This of course doesn't cut through nesting of functions like exit does, but it should work well for top-level exit calls.

You might need to unalias exit eventually.

1

The knot of the problem is that you are sourcing (.) the script and not simply executing them (./script2.sh or /bin/bash script2.sh).

If you really need to source them do it in a subshell (. ./myscript2.sh args)

If you can execute normally the scripts you can store in a variable the exit code that is automatically updated after each execution in $?.

./myscript2.sh arg1
FirstExitCode=$?
./myscript2.sh arg2
SecondExitCode=$?
 ...

Read more about source with help .

1

Based on Hastur's answer about sourcing, this seems to be what I need in second.sh to return a status without killing the terminal:

#!/usr/bin/env bash
...

# if success, return 0
[ "$0" = "$BASH_SOURCE" ] && exit 0 || return 0

# else for failure, return non-0
[ "$0" = "$BASH_SOURCE" ] && exit 1 || return 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy