7

This question already has an answer here:

exit doesn't terminate the script when error is called..

output

Error: Could not resolve localhost
after exit

script

#!/bin/sh

resolve_ip (){
    if [ -z "$1" ]; then
        host="localhost"
        ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
    else
        host="$1"
        ip=$(dig +short $1)
    fi

    if [ -z "$ip" ]; then
        error "Could not resolve $host"
    fi

    echo "$ip"
}

error (){
    (>&2 echo "Error: $1")
    exit 1
}

master_host='google.com'

if [ "$(resolve_ip)" = "$(resolve_ip $master_host)" ]; then
    error "some error"
fi

echo "after exit"
exit

marked as duplicate by Archemar, JigglyNaga, Isaac, schily, RalfFriedl Oct 25 '18 at 17:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Aside: Why are you running the echo in error inside a subshell? – Konrad Rudolph Oct 25 '18 at 15:03
18

exit exits the current shell process¹.

In $(resolve_ip), resolve_ip is running in a subshell process.

You can do:

my_ip=$(resolve_ip) || exit
master_ip=$(resolve_ip "$hostname") || exit
if [ "$my_ip" = "$master_ip" ]; ...

For the main shell to exit (with the same exit code as the subshell) when the subshell exits with a non-zero exit status.

Also, as resolve_ip is run in a subshell environment, the $ip and $host variables will not survive after that subshell returns.

Also note that the (...) in (>&2 echo "Error: $1") also starts a subshell. Not really necessary here unless you want to cover for the case where stderr is a broken pipe and writing the error message would cause a SIGPIPE delivery to the main shell process as echo is builtin.

Here, instead of returning the output over stdout, you could return it by storing it in user supplied variables:

resolve_ip (){ # args: ip_var [host]
    if [ "$#" -eq 1 ]; then
        host=localhost
        eval "$1="'$(dig +short myip.opendns.com @resolver1.opendns.com)'
    else
        host=$2
        eval "$1="'$(dig +short "$2")'
    fi

    if eval '[ -z "${'"$1"'}" ]'; then
        error "Could not resolve $host"
    fi
}

# ...

resolve_ip my_ip
resolve_ip master_ip "$hostname"

if [ "$my_ip" = "$master_ip" ]; ...

¹ Strictly speaking subshell environments don't have to be implemented with child processes, and some shells like ksh93 don't as an optimisation, but still exit there only exits the subshell, not the main shell. ksh93 however has a ${ ...; } form or command substitution that doesn't involve a subshell environment, so exit in that would exit the main shell.

Not the answer you're looking for? Browse other questions tagged or ask your own question.