Exit the script if any statement fails
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm using nested if
statements in my shell script. If any statement fails, it has to come out of the script with out executing other sections in the script. I tried using exit 1
and set -e
.
I'm not able to use set -e
as I have used a few grep
statements which would return non zero status.
Could someone please help me How to stop executing a script if any statements fails?
bash shell-script
add a comment |Â
up vote
1
down vote
favorite
I'm using nested if
statements in my shell script. If any statement fails, it has to come out of the script with out executing other sections in the script. I tried using exit 1
and set -e
.
I'm not able to use set -e
as I have used a few grep
statements which would return non zero status.
Could someone please help me How to stop executing a script if any statements fails?
bash shell-script
DoesnâÂÂtexit 1
work?
â Jeff Schaller
Jul 11 at 18:04
I'd recommend fixing thegrep
's that aren't returning correctly as well. Enablingset -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.
â slmâ¦
Jul 11 at 21:05
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm using nested if
statements in my shell script. If any statement fails, it has to come out of the script with out executing other sections in the script. I tried using exit 1
and set -e
.
I'm not able to use set -e
as I have used a few grep
statements which would return non zero status.
Could someone please help me How to stop executing a script if any statements fails?
bash shell-script
I'm using nested if
statements in my shell script. If any statement fails, it has to come out of the script with out executing other sections in the script. I tried using exit 1
and set -e
.
I'm not able to use set -e
as I have used a few grep
statements which would return non zero status.
Could someone please help me How to stop executing a script if any statements fails?
bash shell-script
edited Jul 11 at 21:04
slmâ¦
233k65479651
233k65479651
asked Jul 11 at 17:58
user_297020
116
116
DoesnâÂÂtexit 1
work?
â Jeff Schaller
Jul 11 at 18:04
I'd recommend fixing thegrep
's that aren't returning correctly as well. Enablingset -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.
â slmâ¦
Jul 11 at 21:05
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09
add a comment |Â
DoesnâÂÂtexit 1
work?
â Jeff Schaller
Jul 11 at 18:04
I'd recommend fixing thegrep
's that aren't returning correctly as well. Enablingset -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.
â slmâ¦
Jul 11 at 21:05
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09
DoesnâÂÂt
exit 1
work?â Jeff Schaller
Jul 11 at 18:04
DoesnâÂÂt
exit 1
work?â Jeff Schaller
Jul 11 at 18:04
I'd recommend fixing the
grep
's that aren't returning correctly as well. Enabling set -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.â slmâ¦
Jul 11 at 21:05
I'd recommend fixing the
grep
's that aren't returning correctly as well. Enabling set -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.â slmâ¦
Jul 11 at 21:05
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
You can still use set -e
. If you have specific statement which you expect to fail, you simply need to catch the error state and "handle" it:
$ cat 454756.sh
#!/bin/bash
set -e
[[ 1 -eq 2 ]] || :
echo "Still got here!"
$ ./454756.sh
Still got here!
set -e
will abort the script on any uncaught error condition. Otherwise you would not be able to use if
statements.
# this also still works
set -e
if /bin/false; then
echo "nope"
else
echo "yep"
fi
The incantation || :
is a succinct way to "eat" any error thrown by any command, which is handy when you're wanting to use set -e
and have specific commands whose failure is perfectly okay.
Another way to do this is to unset -e
before the command in question, and reset it afterward:
set -e
do_stuff
set +e
/bin/false
set -e
1
Might worth mentioningtrue
and:
are effectively the same andtrue
is a bit more explicit
â Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You can still use set -e
. If you have specific statement which you expect to fail, you simply need to catch the error state and "handle" it:
$ cat 454756.sh
#!/bin/bash
set -e
[[ 1 -eq 2 ]] || :
echo "Still got here!"
$ ./454756.sh
Still got here!
set -e
will abort the script on any uncaught error condition. Otherwise you would not be able to use if
statements.
# this also still works
set -e
if /bin/false; then
echo "nope"
else
echo "yep"
fi
The incantation || :
is a succinct way to "eat" any error thrown by any command, which is handy when you're wanting to use set -e
and have specific commands whose failure is perfectly okay.
Another way to do this is to unset -e
before the command in question, and reset it afterward:
set -e
do_stuff
set +e
/bin/false
set -e
1
Might worth mentioningtrue
and:
are effectively the same andtrue
is a bit more explicit
â Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
add a comment |Â
up vote
3
down vote
You can still use set -e
. If you have specific statement which you expect to fail, you simply need to catch the error state and "handle" it:
$ cat 454756.sh
#!/bin/bash
set -e
[[ 1 -eq 2 ]] || :
echo "Still got here!"
$ ./454756.sh
Still got here!
set -e
will abort the script on any uncaught error condition. Otherwise you would not be able to use if
statements.
# this also still works
set -e
if /bin/false; then
echo "nope"
else
echo "yep"
fi
The incantation || :
is a succinct way to "eat" any error thrown by any command, which is handy when you're wanting to use set -e
and have specific commands whose failure is perfectly okay.
Another way to do this is to unset -e
before the command in question, and reset it afterward:
set -e
do_stuff
set +e
/bin/false
set -e
1
Might worth mentioningtrue
and:
are effectively the same andtrue
is a bit more explicit
â Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can still use set -e
. If you have specific statement which you expect to fail, you simply need to catch the error state and "handle" it:
$ cat 454756.sh
#!/bin/bash
set -e
[[ 1 -eq 2 ]] || :
echo "Still got here!"
$ ./454756.sh
Still got here!
set -e
will abort the script on any uncaught error condition. Otherwise you would not be able to use if
statements.
# this also still works
set -e
if /bin/false; then
echo "nope"
else
echo "yep"
fi
The incantation || :
is a succinct way to "eat" any error thrown by any command, which is handy when you're wanting to use set -e
and have specific commands whose failure is perfectly okay.
Another way to do this is to unset -e
before the command in question, and reset it afterward:
set -e
do_stuff
set +e
/bin/false
set -e
You can still use set -e
. If you have specific statement which you expect to fail, you simply need to catch the error state and "handle" it:
$ cat 454756.sh
#!/bin/bash
set -e
[[ 1 -eq 2 ]] || :
echo "Still got here!"
$ ./454756.sh
Still got here!
set -e
will abort the script on any uncaught error condition. Otherwise you would not be able to use if
statements.
# this also still works
set -e
if /bin/false; then
echo "nope"
else
echo "yep"
fi
The incantation || :
is a succinct way to "eat" any error thrown by any command, which is handy when you're wanting to use set -e
and have specific commands whose failure is perfectly okay.
Another way to do this is to unset -e
before the command in question, and reset it afterward:
set -e
do_stuff
set +e
/bin/false
set -e
answered Jul 11 at 18:01
DopeGhoti
39.7k54679
39.7k54679
1
Might worth mentioningtrue
and:
are effectively the same andtrue
is a bit more explicit
â Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
add a comment |Â
1
Might worth mentioningtrue
and:
are effectively the same andtrue
is a bit more explicit
â Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
1
1
Might worth mentioning
true
and :
are effectively the same and true
is a bit more explicitâ Sergiy Kolodyazhnyy
Jul 11 at 18:18
Might worth mentioning
true
and :
are effectively the same and true
is a bit more explicitâ Sergiy Kolodyazhnyy
Jul 11 at 18:18
1
1
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
In bash source they're implemented via same function, and both return exit status of success no matter what, and take arguments without doing anything
â Sergiy Kolodyazhnyy
Jul 11 at 18:52
1
1
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
Fair enough, I sit corrected.
â DopeGhoti
Jul 11 at 18:54
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%2f454756%2fexit-the-script-if-any-statement-fails%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
DoesnâÂÂt
exit 1
work?â Jeff Schaller
Jul 11 at 18:04
I'd recommend fixing the
grep
's that aren't returning correctly as well. Enablingset -e
is a level of conformance that you're demanding of your script, so you're basically working against it rather than using its enforcements. Just my $0.02.â slmâ¦
Jul 11 at 21:05
By "any statements" you seem to mean "almost any statements".
â Kusalananda
Jul 11 at 21:09