Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment in bash loop by set amount

Tags:

bash

I know how to do a loop in bash that increases by one each time, but say I have a range 1 to 773 and I want to output a range from a loop so that I get two variables in each iteration. The first will be 1 and the second will be say 19. In the second iteration the first would be 20 and the second 39.

Ive been playing with something like:

for start in {1..773}
do    
start=$(($start+20))
end=$(($start+20))
echo $start ## 
echo $end
done

Desired loop outcome:

 1. $start = 1 and $end = 19
 2. $start = 20 and $end = 39
 3. $start = 40 and $end = 59 
etc

But it's not right. I want to output these two variables to a series of scripts to make R run faster, so if non bash (eg awk) solutions are easier then that's cool too if a simple > will send it the file.

Thanks!

like image 224
cianius Avatar asked Sep 16 '13 12:09

cianius


2 Answers

If you want to print the ranges within 773, you can do like this

#!env bash
start=1
end=19
for counter in {1..773}
do
   echo $counter. "\$start = " $start " and \$end = " $end
   if [[ $start -eq 1 ]];
   then
      start=0
   fi
   start=$(($start+20))
   end=$(($end+20))
   if [[ $end -ge 773 ]];
   then
      break
   fi
done

Output

1. $start =  1  and $end =  19
2. $start =  20  and $end =  39
3. $start =  40  and $end =  59
4. $start =  60  and $end =  79
5. $start =  80  and $end =  99
6. $start =  100  and $end =  119
7. $start =  120  and $end =  139
8. $start =  140  and $end =  159
9. $start =  160  and $end =  179
10. $start =  180  and $end =  199
11. $start =  200  and $end =  219
12. $start =  220  and $end =  239
13. $start =  240  and $end =  259
14. $start =  260  and $end =  279
15. $start =  280  and $end =  299
16. $start =  300  and $end =  319
17. $start =  320  and $end =  339
18. $start =  340  and $end =  359
19. $start =  360  and $end =  379
20. $start =  380  and $end =  399
21. $start =  400  and $end =  419
22. $start =  420  and $end =  439
23. $start =  440  and $end =  459
24. $start =  460  and $end =  479
25. $start =  480  and $end =  499
26. $start =  500  and $end =  519
27. $start =  520  and $end =  539
28. $start =  540  and $end =  559
29. $start =  560  and $end =  579
30. $start =  580  and $end =  599
31. $start =  600  and $end =  619
32. $start =  620  and $end =  639
33. $start =  640  and $end =  659
34. $start =  660  and $end =  679
35. $start =  680  and $end =  699
36. $start =  700  and $end =  719
37. $start =  720  and $end =  739
38. $start =  740  and $end =  759
like image 179
thefourtheye Avatar answered Oct 11 '22 14:10

thefourtheye


your requirement are not perfectly clear, but you are re-using variable names.

if i do this:

for index in {1..773}
do    
  start=$(($index+20))
  end=$(($start+20))
  echo $start ## 
  echo $end
done

i get something that resembles your desired result. observe how i renamed the loop variable from start to index.

PS: if you want to change the step size (a.k.a. "increment") in your loop, simply do it like this:

#!/bin/bash
for i in {0..10..2}
  do
     echo "Welcome $i times"
done

This will increment in steps of 2, you would want to use a 20 here. That would give you 1, 21, 41, ... as value. See http://www.cyberciti.biz/faq/bash-for-loop/ for more details.

like image 44
mnagel Avatar answered Oct 11 '22 14:10

mnagel