如果在 bash 中进行语句算术,我该怎么做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8304005/
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
How do I do if statement arithmetic in bash?
提问by jmasterx
I want to do something like this:
我想做这样的事情:
if [ % 4 == 0 ]; then
...
But this does not work.
但这不起作用。
What do I need to do instead?
我需要做什么?
回答by guns
read n
if ! ((n % 4)); then
echo "$n divisible by 4."
fi
The (( ))
operator evaluates expressions as C arithmetic, and has a boolean return.
该(( ))
运算符将表达式计算为 C 算术,并返回布尔值。
Hence, (( 0 ))
is false, and (( 1 ))
is true. [1]
因此,(( 0 ))
是假的,(( 1 ))
是真的。[1]
The $(( ))
operator also expands C arithmetic expressions, but instead of returning true/false, it returns the value instead. Because of this you can test the output if $(( ))
in this fashion: [2]
该$(( ))
运算符还扩展了 C 算术表达式,但它不返回真/假,而是返回值。因此,您可以$(( ))
以这种方式测试输出:[2]
[[ $(( n % 4 )) == 0 ]]
But this is tantamount to: if (function() == false)
. Thus the simpler and more idiomatic test is:
但这无异于:if (function() == false)
。因此,更简单和更惯用的测试是:
! (( n % 4 ))
[1]: Modern bash handles numbers up to your machine's intmax_t
size.
[1]:现代 bash 可以根据您的机器intmax_t
大小处理数字。
[2]: Note that you can drop $
inside of (( ))
, because it dereferences variables within.
[2]:请注意,您可以$
放入 of 内(( ))
,因为它会取消引用其中的变量。
回答by Jan Vorcak
a=4
if [ $(( $a % 4 )) -eq 0 ]; then
echo "I'm here"
fi
回答by chown
single brackets ([..]
) don't work for some tests, try with double brackets ([[...]]
) and enclose the mod in ((..))
to evaluate the %
operator properly:
单括号 ( [..]
) 不适用于某些测试,请尝试使用双括号 ( [[...]]
) 并将 mod 括起来((..))
以%
正确评估运算符:
if [[ $(( % 4 )) == 0 ]]; then
More details here:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html
更多细节在这里:http:
//tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html
回答by potong
This might work for you:
这可能对你有用:
((a%4==0)) && echo "$a is divisible by 4" || echo "$a is not divisible by 4"
or more succinctly:
或更简洁地说:
((a%4)) && echo "$a is not divisible by 4" || echo "$a is divisible by 4"
回答by bballdave025
If you want something a bit more portable - for example, something that works in sh
as well as in bash
- use
如果你想要一些更便携-例如,一些作品中sh
,以及在bash
-使用
if [ $(echo " % 4" | bc) -eq 0 ]; then
...
An example
一个例子
#!/bin/bash
#@file: trymod4.bash
if [ $(echo " % 4" | bc) -eq 0 ]; then
echo " is evenly divisible by 4"
else
echo " is NOT evenly divisible by 4"
fi
$ chmod +x trymod4.bash
$ ./trymod4.bash 224
224 is evenly divisible by 4
$ ./trymod4.bash 223
223 is NOT evenly divisible by 4
I put this in, because you used the single [ ... ]
conditional, which I usually associate with sh
-compatible programs.
我把它放进去,因为你使用了单个[ ... ]
条件,我通常将它与sh
兼容程序相关联。
Check that this works in sh
.
检查这在sh
.
#!/bin/sh
#@file: trymod4.sh
if [ $(echo " % 4" | bc) -eq 0 ]; then
echo " is evenly divisible by 4"
else
echo " is NOT evenly divisible by 4"
fi
$ chmod +x trymod4.sh
$ ./trymod4.sh 144
144 is evenly divisible by 4
$ ./trymod4.sh 19
19 is NOT evenly divisible by 4
All right, it works with sh
.
好吧,它适用于sh
.
Note the "theoretical" (but not always implemented as such) differences between [ ... ]
and [[ ... ]]
from this site(archived).