bash 中的复杂条件,其中正在计算可执行文件的 return 值
Complex conidtions in bash where the return value of executables are being evaluated
我们可以在 [[ ]]
或 (( ))
类型的括号内创建稍微复杂的条件语句。
例如:
a=3;b=4;c=4
if [[ 1 == 1 && ( ( $a == $b && 1 == 1 ) || ( $b == $c && 2 == 2)) ]];
then echo yes;
else echo no;
fi
如果我们认为使用可执行命令的等效项 return 是一个错误,我不确定如何继续。在“与”链的实例中,这是微不足道的:
if echo hello | grep -q "h" && stat /etc/os-release 2>/dev/null;
then echo yes;
else echo no;
fi
如果我有一些分支怎么办?就像我给出的第一个例子。
问题本质上归结为如何创建命令组?我在第一个条件中使用的括号似乎只在 [[ ]]
块内起作用。
附带一个问题,我的做法是否愚蠢?
我知道我可以用一堆 'if' 语句来做我想做的事,但我认为那看起来很难看。
我正在尝试做的事情的本质是根据变量的评估执行一系列检查,这些检查将在一个分支中按顺序完成。
非常感谢
What if I have some branches though?
if [[ 1 == 1 && ( ( $a == $b && 1 == 1 ) || ($b == $c && 2 == 2) ) ]];
澄清一下,这里没有 (( ))
"type" 括号,即。这里没有发生算术扩展,并且 let
内置函数没有被执行。现在让我们将1 == 1
替换为true
,将$a == $b
替换为"cmd1",将$b == $c
替换为cmd2
,我们可以:
if true && { { cmd1 && true; } || { cmd2 && true; }; }; then
从 bash manual 开始,您有两种分组命令的方法:
()
( list )
Placing a list of commands between parentheses causes a
subshell environment to be created (see Command Execution
Environment), and each of the commands in list to be executed in that
subshell. Since the list is executed in a subshell, variable
assignments do not remain in effect after the subshell completes.
{}
{ list; }
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.
if 中的条件和 then .. fi
子句中的表达式在句法上没有区别 - 都是表达式并被解析为 bash 命令。所以你可以嵌套它们:
if
if true; then
cmd1
else
cmd2
fi
then
cmd3
fi
我们可以在 [[ ]]
或 (( ))
类型的括号内创建稍微复杂的条件语句。
例如:
a=3;b=4;c=4
if [[ 1 == 1 && ( ( $a == $b && 1 == 1 ) || ( $b == $c && 2 == 2)) ]];
then echo yes;
else echo no;
fi
如果我们认为使用可执行命令的等效项 return 是一个错误,我不确定如何继续。在“与”链的实例中,这是微不足道的:
if echo hello | grep -q "h" && stat /etc/os-release 2>/dev/null;
then echo yes;
else echo no;
fi
如果我有一些分支怎么办?就像我给出的第一个例子。
问题本质上归结为如何创建命令组?我在第一个条件中使用的括号似乎只在 [[ ]]
块内起作用。
附带一个问题,我的做法是否愚蠢? 我知道我可以用一堆 'if' 语句来做我想做的事,但我认为那看起来很难看。
我正在尝试做的事情的本质是根据变量的评估执行一系列检查,这些检查将在一个分支中按顺序完成。
非常感谢
What if I have some branches though?
if [[ 1 == 1 && ( ( $a == $b && 1 == 1 ) || ($b == $c && 2 == 2) ) ]];
澄清一下,这里没有 (( ))
"type" 括号,即。这里没有发生算术扩展,并且 let
内置函数没有被执行。现在让我们将1 == 1
替换为true
,将$a == $b
替换为"cmd1",将$b == $c
替换为cmd2
,我们可以:
if true && { { cmd1 && true; } || { cmd2 && true; }; }; then
从 bash manual 开始,您有两种分组命令的方法:
()
( list )
Placing a list of commands between parentheses causes a subshell environment to be created (see Command Execution Environment), and each of the commands in list to be executed in that subshell. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.{}
{ list; }
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.
if 中的条件和 then .. fi
子句中的表达式在句法上没有区别 - 都是表达式并被解析为 bash 命令。所以你可以嵌套它们:
if
if true; then
cmd1
else
cmd2
fi
then
cmd3
fi