加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在||之后使用多个命令在bash中

发布时间:2020-12-16 01:08:18 所属栏目:安全 来源:网络整理
导读:想象一下代码: ls || echo "Unable to execute ls (returned non zero)" 如果我需要执行更多命令,如果: ls || echo "this is echo 1" some operator echo "this is echo 2" some operator exit 1 在C(假设我有功能ls)我可以做(即使它看起来很疯狂): ls()
想象一下代码:
ls || echo "Unable to execute ls (returned non zero)"

如果我需要执行更多命令,如果:

ls || echo "this is echo 1" <some operator> echo "this is echo 2" <some operator> exit 1

在C(假设我有功能ls)我可以做(即使它看起来很疯狂):

ls() || (command1() && command2());

但我怀疑我可以在bash中使用这样的括号

编辑:我知道我可以创建一个包含这些命令的bash函数,但是如果我需要将它与exit 1结合起来(在函数中退出将退出该函数,而不是整个脚本)

您可以在{}中执行 group多个命令.他说:
some_command || { command1; command2; }

如果some_command以非零返回码退出,则执行command1和command2.

{}

06001

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读