Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Bash quickie: loop N times, without doing math
Posted 10-22-2012 at 09:07 PM by the dsc
Updated 03-10-2013 at 12:25 AM by the dsc (improving, varible version)
Updated 03-10-2013 at 12:25 AM by the dsc (improving, varible version)
I thought that the only way to do such type of loop in Bash would require something like "n=$(($n+1))" within the loop, with the loop being conditioned to "n" adding up to whatever you want, such as "until ((n==30)) ; do". But you can actually do "for i in {1..20} ; do <whatever> ; done". That's somewhat similar to Basic's "for i=1 to 20 ; whatever ; next i".
But it won't work with variables for the starting and ending numbers. If you want to use variables, you must use $(seq): "for n in $(seq $startnum $endnum) ; do <uptoyou> ; done.
But it won't work with variables for the starting and ending numbers. If you want to use variables, you must use $(seq): "for n in $(seq $startnum $endnum) ; do <uptoyou> ; done.
Total Comments 0