i=' expr $i+1 '
is not incrementing i
, it assigns i
a value of ' expr $i+1 '.
On the next iteration of the loop you run [ $i -lt 100 ]
. Since i
is not embraced in double quotes, this expands to [ expr '$i+1' -lt 100 ]
. [
is actually a command, and you have given it too many arguments due to the reason above.
If you replace i=' expr $i+1 '
with i=$(($i + 1))
, your code should work.
Edit:
It seems that at least Bash will have issues with the number 008, it interprets it as octal. You need to assign 00$i
, 0$i
and $i
to another variable (or echo "fla00$i"
etc) if you get some error after 008.
In this case, you'll need to do something when i >= 100.
This is what I would do:
i=1
while [ $i -lt 121 ]; do
if [ $i -ge 100 ]; then
echo fla$i
elif [ $i -ge 10 ]; then
echo fla0$i
else
echo fla00$i
fi
i=$(($i + 1))
done
[
and[[
– ilkkachu Mar 09 '17 at 10:25set -x
to the shebang line to enable verbose debug mode to see what exactly is happening while executing the script. – Erathiel Mar 09 '17 at 10:27for i in {1..128}; do printf "fla%03d\n" $i; done
– glenn jackman Mar 09 '17 at 17:44printf "fla%03d\n" {1..128}
– Gordon Davisson Mar 09 '17 at 21:58