想象一下代码:
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多个命令.他说:
原文链接:https://www.f2er.com/bash/384620.htmlsome_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.