Bash – Defining the start and end point of a loop in bash

bash

I have a directory with folders sequentially numbered 1-1000, however I want to perform an action for some of the folders in this directory. i.e folders 1-50. How do I define the start and end point of the loop?

So far I have a script along these lines:

a=1
b=1

for i in ~/PATH/*/ ;do

(cd $i/ && action)

echo "completed"
a=`expr $a + $b`

echo "Next Folder"
done

I can define the start of the loop by changing a= but I am not sure how to define the end to the loop? As it stands, it will keep going through all the folders in that directory. Thanks 🙂

Best Answer

#!/bin/bash
a=1
b=1

cd ~/PATH/
for i in $(ls -v) ;do
  (cd $i/ && action) && echo "completed" || echo "error"
  a=$((a+b)); [ "$a" -gt 50 ] && break 
  echo "Next Folder"
done
Related Question