Automatically exit when bash command produce return code non zero [duplicate]
This question already has an answer here:
Automatic exit from bash shell script on error
7 answers
Error handling in Bash
14 answers
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
bash
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41
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.
|
show 2 more comments
This question already has an answer here:
Automatic exit from bash shell script on error
7 answers
Error handling in Bash
14 answers
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
bash
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41
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.
3
You can useset -eor#!/bin/bash -e
– anubhava
Nov 26 '18 at 18:35
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?
– Cyrus
Nov 26 '18 at 18:37
@anubhava thanks that's what I need. Strangely mybash --helpdoesn't show this option, regardless, it works. Could you please move it as an answer?
– RonPringadi
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,set -ehas lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.
– Charles Duffy
Nov 26 '18 at 18:39
|
show 2 more comments
This question already has an answer here:
Automatic exit from bash shell script on error
7 answers
Error handling in Bash
14 answers
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
bash
This question already has an answer here:
Automatic exit from bash shell script on error
7 answers
Error handling in Bash
14 answers
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
This question already has an answer here:
Automatic exit from bash shell script on error
7 answers
Error handling in Bash
14 answers
bash
bash
asked Nov 26 '18 at 18:33
RonPringadiRonPringadi
342114
342114
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41
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.
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:41
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.
3
You can useset -eor#!/bin/bash -e
– anubhava
Nov 26 '18 at 18:35
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?
– Cyrus
Nov 26 '18 at 18:37
@anubhava thanks that's what I need. Strangely mybash --helpdoesn't show this option, regardless, it works. Could you please move it as an answer?
– RonPringadi
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,set -ehas lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.
– Charles Duffy
Nov 26 '18 at 18:39
|
show 2 more comments
3
You can useset -eor#!/bin/bash -e
– anubhava
Nov 26 '18 at 18:35
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?
– Cyrus
Nov 26 '18 at 18:37
@anubhava thanks that's what I need. Strangely mybash --helpdoesn't show this option, regardless, it works. Could you please move it as an answer?
– RonPringadi
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,set -ehas lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.
– Charles Duffy
Nov 26 '18 at 18:39
3
3
You can use
set -e or #!/bin/bash -e– anubhava
Nov 26 '18 at 18:35
You can use
set -e or #!/bin/bash -e– anubhava
Nov 26 '18 at 18:35
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?– Cyrus
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?– Cyrus
Nov 26 '18 at 18:37
@anubhava thanks that's what I need. Strangely my
bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?– RonPringadi
Nov 26 '18 at 18:39
@anubhava thanks that's what I need. Strangely my
bash --help doesn't show this option, regardless, it works. Could you please move it as an answer?– RonPringadi
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,
set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.– Charles Duffy
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,
set -e has lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.– Charles Duffy
Nov 26 '18 at 18:39
|
show 2 more comments
1 Answer
1
active
oldest
votes
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"
add a comment |
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"
add a comment |
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"
answered Nov 26 '18 at 18:39
chepnerchepner
251k33236331
251k33236331
add a comment |
add a comment |
3
You can use
set -eor#!/bin/bash -e– anubhava
Nov 26 '18 at 18:35
^^ + Otherwise known as the "Error Exit" flag
– Zak
Nov 26 '18 at 18:37
mv file_a /somedir/file_a || exit?– Cyrus
Nov 26 '18 at 18:37
@anubhava thanks that's what I need. Strangely my
bash --helpdoesn't show this option, regardless, it works. Could you please move it as an answer?– RonPringadi
Nov 26 '18 at 18:39
Note BashFAQ #105, describing why what you're asking for is a bad idea. It's impossible to tell the difference between "X returned a value of false" and "X failed", and lots of things return nonzero exit status for reasons that aren't actually failures. Worse,
set -ehas lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.– Charles Duffy
Nov 26 '18 at 18:39