exit not terminating the script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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
shell
add a comment |Â
up vote
1
down vote
favorite
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
shell
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
shell
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
shell
shell
asked 38 mins ago
clarkk
48241122
48241122
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
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.
¹ 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.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
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.
¹ 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.
add a comment |Â
up vote
4
down vote
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.
¹ 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.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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.
¹ 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.
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.
¹ 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.
edited 29 mins ago
answered 34 mins ago


Stéphane Chazelas
289k54536875
289k54536875
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477719%2fexit-not-terminating-the-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password