我怎样才能“尝试做某事然后检测它是否失败”?在bash中?

发布于 2024-12-01 23:42:20 字数 1144 浏览 1 评论 0原文

在回答上一个问题时:

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

建议“尝试做某事并检测是否失败”而不是“检查权限然后做某事”

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

但是,我发现关于如何在 bash 中实现 try/catch 的明确信息很少。我猜想这太简单了,除了我发现的似乎相当复杂 - 使用函数或其他脚本:

我对 bash 比较陌生,但很困惑没有一个类似于其他语言中的 try 函数的简单函数。

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

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

In an answer to a previous question:

How can I use 'do I have root access?' as a conditional in 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:

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

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   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2024-12-08 23:42:20
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

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

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

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.

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

月下伊人醉 2024-12-08 23:42:20

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

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

[[ your_test_expression ]] && commands

这相当于

if [[ your_test_expression ]]; then
    commands
fi

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

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

Bash depends on exit status so there isn't any try/catch equivalent. But it's still powerful to fit your needs.

For simple cases, you can use

[[ your_test_expression ]] && commands

This is equivalent to

if [[ your_test_expression ]]; then
    commands
fi

If uses 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.

For complicated cases, you still need if or case statements to express your logic.

心如狂蝶 2024-12-08 23:42:20

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

set -e

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

还看陷阱。我相信

trap 'exit 2' ERR

是相似的

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.

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.

also look at trap. I believe

trap 'exit 2' ERR

is similar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文