If you are die hard Bourne Shell (/bin/sh) scripter, it can be a challenge not to be enticed by the syntax niceties of the Born Again Borne Shell (/bin/bash).
One example is the {..} syntax
#!/bin/bash for I in {0..5} do echo $I done
0 1 2 3 4 5
This syntax is not valid in /bin/sh
on Linux.
#!/bin/sh for I in {0..5} do echo $I done
{0..5}
NOTE: However apparently it does work in Mac OS X, which is derived from BSD, not Linux.
/bin/sh
gives you a for loop but it requires the full list of iterated values instead of a range.
#!/bin/sh for I in 0 1 2 3 4 5 do echo $I done
Note: Passing a string does not work by default.
#!/bin/sh for I in "0 1 2 3 4 5" do echo $I done
The approach to product the same result requires some format management.
#!/bin/sh OIFS=$IFS IFS=" " for I in `echo "0 1 2 3 4 5"` do echo $I done IFS=$OIFS
You can use while
#!/bin/sh I=0 while [ $I -le 5 ] do echo $I I=`expr $I + 1` done
You can use one of several other shell commands, in this example awk
#!/bin/sh for I in `awk 'BEGIN{for (i=0;i<=5;i++) print i}'` do echo $I done
Or, the function specifically design for sequences of numbers seq
#!/bin/sh for I in `seq 0 5` do echo $I done
And for these few examples, there will be more possibilities to achieve close to feature parity of the /bin/bash
syntax.
An example found on BSD is jot - 0 5
. This is not available Ubuntu by default but installed with the athena-jot
package. However the syntax is then different for correct usage.