什么命令在 Bash 的条件中意味着“什么都不做”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17583578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What command means "do nothing" in a conditional in Bash?
提问by Village
Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when $a
is greater than "10", print "1" if $a
is less than "5", otherwise, print "2":
有时在制作条件时,我需要代码什么都不做,例如,在这里,我希望 Bash 在$a
大于“10”时什么都不做,如果$a
小于“5”则打印“1 ”,否则打印“2”:
if [ "$a" -ge 10 ]
then
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
This makes an error though. Is there a command which will do nothing and also not slow down my script?
但这会导致错误。是否有一个什么都不做也不会减慢我的脚本的命令?
回答by Barmar
The no-op command in shell is :
(colon).
shell 中的 no-op 命令是:
(colon)。
if [ "$a" -ge 10 ]
then
:
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
From the bash manual:
从bash 手册:
:
(a colon)
Do nothing beyond expanding arguments and performing redirections. The return status is zero.
:
(冒号)
除了扩展参数和执行重定向之外什么都不做。返回状态为零。
回答by Flimzy
You can probably just use the true
command:
您可能只需使用以下true
命令:
if [ "$a" -ge 10 ]; then
true
elif [ "$a" -le 5 ]; then
echo "1"
else
echo "2"
fi
An alternative, in your example case (but not necessarily everywhere) is to re-order your if/else:
另一种选择,在您的示例情况下(但不一定在所有地方)是重新订购您的 if/else:
if [ "$a" -le 5 ]; then
echo "1"
elif [ "$a" -lt 10 ]; then
echo "2"
fi
回答by Basti M
Although I'm not answering the original question concering the no-op command, many (if not most) problems when one may think "in this branch I have to do nothing" can be bypassed by simply restructuring the logicso that this branch won't occur.
虽然我没有回答关于 no-op 命令的原始问题,但当人们可能认为“在这个分支我什么都不做”时,许多(如果不是大多数)问题可以通过简单地重构逻辑来绕过,以便这个分支获胜不发生。
I try to give a general rule by using the OPs example
我尝试通过使用 OP 示例给出一般规则
do nothing when $a is greater than "10", print "1" if $a is less than "5", otherwise, print "2"
当 $a 大于“10”时什么也不做,如果 $a 小于“5”则打印“1”,否则打印“2”
we have to avoid a branch where $a
gets more than 10, so $a < 10
as a general condition can be applied to every other, following condition.
我们必须避免一个分支$a
超过 10 个,因此$a < 10
一般条件可以应用于每个其他条件,以下条件。
In general terms, when you say do nothing when X, then rephrase it as avoid a branch where X. Usually you can make the avoidance happen by simply negating X and applying it to all other conditions.
一般而言,当您说X 什么都不做时,请将其改写为避免 X 所在的分支。通常,您可以通过简单地否定 X 并将其应用于所有其他条件来避免发生。
So the OPs example with the rule applied may be restructured as:
因此,应用规则的 OP 示例可以重构为:
if [ "$a" -lt 10 ] && [ "$a" -le 5 ]
then
echo "1"
elif [ "$a" -lt 10 ]
then
echo "2"
fi
Just a variation of the above, enclosing everything in the $a < 10
condition:
只是上述的变体,将所有内容都包含在$a < 10
条件中:
if [ "$a" -lt 10 ]
then
if [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
fi
(For this specific example @Flimzys restructuringis certainly better, but I wanted to give a general rule for all the people searching how to do nothing.)
(对于这个特定的例子,@Flimzys 重组当然更好,但我想为所有寻求如何无所作为的人提供一个一般规则。)