Search code examples
shmodulo

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!


Solution

  • You should use bc when doing math in shell

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