Calculate modulo in sh script

I am working on an sh script in which I am in a WHILE loop where a variable gets incremented every iteration, and I'd like to save a file every five iterations.

What I'd normally do (say in C) would be to do an if ( n % 5 == 0) test, but I don't know whether that's possible here..? If it isn't, does anyone have any ideas that would get the job done?

Cheers!


你应该在shell中进行数学运算时使用bc

if [ `echo "3 % 2" | bc` -eq 0 ]

If your sh really is sh and not just bash being run as sh then this will work just fine

if [ `expr $n % 5` -eq 0 ]
then
    # do something
fi

If your sh is really bash then put your test in (( )) like so

if (( $n % 5 == 0 ))
then
     # do something
fi
链接地址: http://www.djcxy.com/p/58284.html

上一篇: 为什么人们说在使用随机数发生器时存在模数偏差?

下一篇: 在sh脚本中计算模数