calling vars in a for loop in a shell script
Hi, I have the following question, any help is much appreciated;
# variable declaration stt=10 enn=20 stp=1 # I want to do it in the following structure but not sure how to call # the stt, enn and stp in a for loop for (( i = $stt; i -le $enn; i=i + $stp )) do blah blah done |
How bout:
Code:
#!/bin/bash Code:
man seq |
nope, I have a strict variable selection. It MUST start from stt, end with enn and the increment should be stp. I could use it if there was a way to adjust the increment. Thanks suprstar!
|
ok, that sounds right. Thanks again !
|
Version 4 of Bash has a new increment option for brace expansion. eval is needed when using variables, however, as they don't get substituted naturally.
Code:
for i in $(eval echo {$stt..$enn..$stp} ); do |
How about:
Code:
for (( i = stt; i <= enn; i+=stp )) |
David, didn't know that I had to use "echo" there, I tried it w/o it to no avail, no wonder. Grail, I tried that like
for (( i = $stt; i <= $enn; i=i+stp )) it didn't work. I'll try it your way, it's an easy construct to remember. Thanks guys! |
You will notice the '$' are not needed but you should stay with one format or the other.
|
Quote:
You could clean it up a little by moving the eval into a separate variable... Code:
sequence=$( eval echo {$stt..$enn..$stp} ) |
David, that's interesting, haven't seen it like that before, Thanks again guys !
|
All times are GMT -5. The time now is 12:30 AM. |