如果在bash中使用语句算术,我该怎么办?
我想要做这样的事情:
if [ $1 % 4 == 0 ]; then ... 但是这不起作用。 我需要做些什么呢?
谢谢
 read n if ! ((n % 4)); then echo "$n divisible by 4." fi 
  (( ))运算符将expression式评估为C运算,并且具有布尔返回值。 
 因此, (( 0 ))是假的, (( 1 ))是真的。  [1] 
  $(( ))运算符也扩展了C运算expression式,但不是返回true / false,而是返回值。 正因为如此,你可以用这种方式testing输出$(( )) :[2] 
 [[ $(( n % 4 )) == 0 ]] 
 但是这等于: if (function() == false) 。 因此,更简单和更习惯的testing是: 
 ! (( n % 4 )) 
  [1]:现代bash处理数字,直到你的机器的intmax_t大小。 
  [2]:请注意,您可以在(( ))内放置$ ,因为它将取消引用内部的variables。 
 单个括号( [..] )对于某些testing不起作用,请尝试使用双括号( [[...]] ),并将mod放在((..))以正确评估%运算符: 
 if [[ $(( $1 % 4 )) == 0 ]]; then 
 更多细节在这里: 
  http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html 
 a=4 if [ $(( $a % 4 )) -eq 0 ]; then echo "I'm here" fi 
这可能适合你:
 ((a%4==0)) && echo "$a is divisible by 4" || echo "$a is not divisible by 4" 
或者更简洁:
 ((a%4)) && echo "$a is not divisible by 4" || echo "$a is divisible by 4"