Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

When Bash -e Doesn't Exit as You Expect

$
0
0

In bash scripting, it's a good practice to exit for any unexpected or unhandled errors. Usually, I enforce this by 'bash -e my_script.sh'. TodayI got a surprise with ' bash -e '.

Check out the simple test below, because you might get bitten by this as well.


When Bash -e Doesn't Exit as You Expect

Source: http://dennyzhang.com/bash_errcode_exit

Here is my previous assumption: suppose we run a shell code block with ' bash -e ' or ' set -e '. If any commands have problems in the middle, the whole code block shallfail and quit.

The Usual Case of 'bash -e'

As we expect in below test, "ls /wontexists" fails. Thus, we don't see the further output generated by the second echo command.

cat > /tmp/test1.sh << EOF #!/bin/bash echo "msg1" && ls /wontexists echo "should not see this" EOF bash -xe /tmp/test1.sh echo $? Problematic Example: 'bash -e' Doesn't Exit

Running the below code, we will see anoutput of "should not see this". And $? is zero! Strange, isn't it?

cat > /tmp/test2.sh << EOF #!/bin/bash echo "msg1" && ls /wontexists && echo "msg2" echo "should not see this" EOF bash -xe /tmp/test2.sh echo $? Uncover the Mystery

set -e only exits on an 'uncaught' error. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list. To be simple, the shell does not exit if the command that fails is part of the command list .

The official explanation for bash -e can be foundhere. There is a similar discussion inStackOverflow.

-e Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists), or a compound command (see Compound Commands) returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command’s return status is being inverted with !. If a compound command other than a subshell returns a non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits.

More Reading: Shell Redirect Output To File, And Still Have It On Screen .

Like our blog posts? Discuss with us on LinkedIn , Twitter ,Or Newsletter.


Viewing all articles
Browse latest Browse all 11063

Trending Articles