我怎样才能“尝试做某事然后检测它是否失败”?在bash中?
在回答上一个问题时:
我如何使用“我有 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.:
- Thorough use of 'if' statements or 'try/catch' blocks?
- What is the advantage of using try {} catch {} versus if {} else {}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会选择一个如果成功则不会造成任何损害的操作(影子密码文件不应该是可执行的;您可以执行类似
chmod ow /
的操作 - 如果您愿意,可以从根目录中删除公共写入权限),并通过查看命令的退出状态来检查它是否有效。这会丢弃错误消息 - 您必须决定这是否重要。“sudo”用于提升权限;如果您认为用户应该已经是“root”,则省略“sudo”。
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'.
Bash 取决于退出状态,因此没有任何
try/catch
等效项。但它仍然很强大,可以满足您的需求。对于简单的情况,您可以使用
这相当于
If
使用 [[ ... ]] 的“退出状态”,因此实际上您可以在if
之后使用任何命令。只需确保您的控制逻辑取决于命令的退出状态。对于复杂的情况,您仍然需要 if 或 case 语句来表达您的逻辑。
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
This is equivalent to
If
uses the "exit status" of [[ ... ]] so actually you can use any command afterif
. 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.
除非您调用的脚本有退出条件,否则您无能为力。不过,请在 bash 手册页中查找“set”。
如果脚本中的简单命令失败,将导致脚本退出。您可以将其添加到示例中 script.sh 的顶部,以使其在失败时退出。
还看陷阱。我相信
是相似的
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.
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
is similar