3

I'm writing a script that must work in bash and sh and it runs on a miniscule platform and some things appear to be missing such as seq. All these limitations/requirements is making writing this script VERY difficult. I need to write a for loop that works under all these requirements.

This only works for bash:

for (( i = 0; i <= 4; i++ ))
do
  echo $i
done

This should work in sh, but the platform I'm using is apparently missing seq.

for i in $(seq 1 $INPUT); do

$INPUT is the max defined by the user.

Atomiklan
  • 5,164
  • 11
  • 40
  • 62
  • @Joel, just check what is your /bin/sh stands for. E.g. on debian systems it is mostly linked to `dash` and `dash` doesn't support `for (())` contrsuction. – rush Jul 22 '14 at 21:08

1 Answers1

16

Kids these days are so spoiled with their newfangled shell builtins.

i=1
while [ $i -le $INPUT ]; do
  echo $i
  i=$(expr $i + 1)
done
Sammitch
  • 30,782
  • 7
  • 50
  • 77