Note: I am NOT asking this question
I looked for information on how to loop over a range of discontiguous numbers such as 0,1,2,4,5,6,7,9,11 without having to put the numbers in by hand and still use the ranges.
The obvious way would be to do this:
for i in 0 1 2 4 5 6 7 9 11; do echo $i; done
for i in {0..2} {4..6} {7..11..2}; do echo $i; done
See the documentation of bash Brace Expansion.
Edit: oops, didn't realise there were missing numbers.
Updated: Define ranges and then use a while loop for each range. e.g.
ranges="0-2 4-7 9 11"
for range in $ranges
do
min=${range%-*}
max=${range#*-}
i=$min
while [ $i -le $max ]
do
echo $i
i=$(( $i + 1 ))
done
done
http://ideone.com/T80tfB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With