简体   繁体   English

从错误上自动退出bash shell脚本:set + e似乎没有完成这项工作

[英]Automatic exit from bash shell script on error: set +e does not seem to do the job

While I knew for 20 years that shell scripts do not care about errors I keep being amused about this carelessness by default. 虽然我知道20年来shell脚本并不关心错误,但我默默地对这种粗心大意感到好笑。 And even when you explicitly require them not to swallow error and to follow crash early principle that still does not happen. 即使你明确要求他们不要吞下错误并遵循崩溃早期原则仍然不会发生。

Referring to Automatic exit from bash shell script on error , set +e does not seem to do the job, here is a short example: 参考自动退出bash shell脚本时出错set +e似乎没有完成这项工作,这里有一个简短的例子:

#!/bin/bash -vx
set +e
apt-get install nonexisting1
apt-get install nonexisting2
set -e

Output: 输出:

#!/bin/bash -vx
set +e
+ set +e
apt-get install nonexisting1
+ apt-get install nonexisting1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nonexisting1  <--- first error, now stop!
apt-get install nonexisting2           <--- why do we continue here?
+ apt-get install nonexisting2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nonexisting2
set -e
+ set -e

How do I make sure that my script either does all the commands without error or stops immediately? 如何确保我的脚本执行所有命令而不会出现错误或立即停止? I do not like to write || exit 1 我不喜欢写|| exit 1 || exit 1 at the end of almost every line. 在几乎每一行的末尾都|| exit 1

Options in shell are (counter-intuitively) switched on using a minus and switched off using a plus. shell中的选项(反直觉地)使用减号打开并使用加号关闭。

Even so, the set -e option will only work if the program correctly returns non-zero error statuses. 即便如此, set -e选项仅在程序正确返回非零错误状态时才有效。

Although the apt-get manual says it should do this, other posts indicate that it frequently does not (see apt-get update exit status ). 虽然apt-get手册说它应该这样做,但其他帖子表明它经常没有(参见apt-get update退出状态 )。

Verify the return status of your apt command after it runs (eg using echo $? ). 运行后验证apt命令的返回状态(例如,使用echo $? )。 If it returns non-zero, set -e should work. 如果它返回非零,则set -e应该起作用。

I guess you are simply using set +e in place of set -e and vice versa. 我猜你只是用set + e代替set -e,反之亦然。 If you simply do 如果你只是这样做

#!/bin/bash -vx
set -e
apt-get install nonexisting1
apt-get install nonexisting2
set +e

it should stop after the first line. 它应该在第一行之后停止。

Why you set "+e" first of script ? 为什么要先设置“+ e”脚本?
this is my example : 这是我的例子:

#!/bin/bash  -xv

set -e 

dskakj

echo "Hello dear" 
apt-get install bash

output : 输出:

#!/bin/bash  -xv

set -e 
+ set -e

dskakj
+ dskakj
./a.sh: line 5: dskakj: command not found

Note : not need set "+e" end of script . 注意:不需要设置脚本的“+ e”结尾。

For a four-line script like yours, set -e does feel reasonable, assuming apt-get does exit with a non-zero exit code for errors. 对于像你这样的四行脚本, set -e确实合理,假设apt-get确实以非零退出代码退出错误。

However, lots of discussions regarding set -e on SO indicate that it is not a reliable technique for error handling. 但是,关于SO上set -e大量讨论表明它不是一种可靠的错误处理技术。 The Bash documentation confirms it. Bash文档证实了这一点。

We have no option but to check the status variable $? 我们别无选择,只能查看状态变量$? after each command if we want our script to be reliable. 如果我们希望我们的脚本可靠,那么在每个命令之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM