如何在 bash 中“尝试做某事然后检测它是否失败”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7251619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:41:47  来源:igfitidea点击:

How can I "try to do something and then detect if it fails" in bash?

bashif-statementtry-catch

提问by David LeBauer

In an answer to a previous question:

在对上一个问题的回答中:

How can I use 'do I have root access?' as a conditional in bash?

我如何使用“我有 root 访问权限吗?” 作为 bash 的条件?

The suggestion to 'try to do something and detect if it fails' instead of 'check permission and then do something'

“尝试做某事并检测它是否失败”而不是“检查许可然后做某事”的建议

I have found plenty of rationale for this e.g.:

我找到了很多理由,例如:

However, I have found very little clear information about how to implementing try/catch in bash. I would guess that it is too easy, except that what I have found seems rather complicated- using functions or other scripts:

但是,我几乎没有找到关于如何在 bash 中实现 try/catch 的明确信息。我想这太简单了,除了我发现的东西看起来相当复杂 - 使用函数或其他脚本:

I am relatively new to bash but confused that there is not a simple function similar to the tryfunction in other languages.

我对 bash 比较陌生,但对没有与try其他语言中的函数类似的简单函数感到困惑。

specifically, I would like to do the following:

具体来说,我想做以下事情:

CMD=`./path/to/script.sh`
if [ <echo $CMD | grep error is true> ]; then
        .. do this ..
else 
        .. do that ..
fi   

回答by Jonathan Leffler

if sudo chmod a-x /etc/shadow 2>/dev/null
then : Yes - I have root permissions
else : No - I do not have root permissions or /etc/shadow does not exist or ...
fi

This chooses an operation that does no damage if it succeeds (the shadow password file is not supposed to be executable; you could do something like chmod o-w /- remove public write permission from the root directory if you prefer), and check that it worked by looking at the exit status of the command. This throws away the error message - you have to decide whether that matters.

这将选择一个如果成功则不会造成损害的操作(影子密码文件不应该是可执行的;您可以执行类似的操作chmod o-w /- 如果您愿意,可以从根目录中删除公共写权限),并通过查看它来检查它是否有效命令的退出状态。这会丢弃错误消息 - 您必须决定这是否重要。

The 'sudo' is there to raise the privileges; if you think the user should already be 'root', then omit the 'sudo'.

'sudo' 是用来提升权限的;如果您认为用户应该已经是“root”,则省略“sudo”。

回答by Mu Qiao

Bash depends on exit statusso there isn't any try/catchequivalent. But it's still powerful to fit your needs.

Bash 取决于退出状态,因此没有任何try/catch等效项。但它仍然可以满足您的需求。

For simple cases, you can use

对于简单的情况,您可以使用

[[ your_test_expression ]] && commands

This is equivalent to

这相当于

if [[ your_test_expression ]]; then
    commands
fi

Ifuses the "exit status" of [[ ... ]] so actually you can use any command after if. Just make sure your control logic depends on the exit status of the command.

If使用 [[ ... ]] 的“退出状态”,因此实际上您可以在if. 只要确保您的控制逻辑取决于命令的退出状态。

For complicated cases, you still need ifor casestatements to express your logic.

对于复杂的情况,您仍然需要ifcase语句来表达您的逻辑。

回答by user916499

unless the script you are calling has an exit condition, there isn't much you can do. However look up "set" in the bash man page.

除非您调用的脚本具有退出条件,否则您无能为力。但是,请在 bash 手册页中查找“设置”。

set -e

will cause a script to exit if a simple command in it fails. You can add it to the top of script.sh in your example to cause it to exit if it fails.

如果其中的简单命令失败,将导致脚本退出。您可以将它添加到示例中 script.sh 的顶部,以使其在失败时退出。

also look at trap. I believe

还看陷阱。我相信

trap 'exit 2' ERR

is similar

类似