79586857

Date: 2025-04-22 15:43:48
Score: 0.5
Natty:
Report link

I know that this is a very old question, but I was searching for some bash syntax and this came up. The problem is awkard, but calls for an elegant solution.

The fact that the starting and ending ranges are different from the rest poses a particular problem. I wanted to create a for loop that is systematic, flexible in terms of what the step and stop points, while using a repetitive pattern for the looping. It looks complicated, but really has three parts: the for loop, the stopping if statement, and the echo line.

The for loop initiates all the variables, the breaking evaluation uses variable stop and step values, and the incrementing statement encapsulates the logic of the stepping.

I think this is the most elegant of the solutions presented here and it gives precisely the desired output.

for ((i=1,s=1,step=19,e=step,stop=773; e<stop+step; i++, s=e+1, e=s+step)); do if (($e > $stop)); then e=$stop; fi; echo $i. \$start = $s and \$end = $e; done
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Nate