linux - Creating a nested for loop in bash script -
i trying create nested loop count 1 10 , second or nested loop count 1 5.
for ((i=1;i<11;i++)); ((a=1;a<6;a++)); echo $i:$a done done
what though output of loop going was:
1:1 2:2 3:3 4:4 5:5 6:1 7:2 8:3 9:4 10:5
but instead output was
1:1 1:2 1:3 1:4 1:5 2:1 ... 2:5 3:1 ... , same thing till 10:5
how can modify loop result want! thanks
your logic wrong. don't want nested loop @ all. try this:
for ((i=1;i<11;++i)); echo "$i:$((i-5*((i-1)/5)))" done
this uses integer division subtract right number of multiples of 5 value of $i
:
- when
$i
between 1 , 5,(i-1)/5
0
, nothing subtracted - when
$i
between 6 , 10,(i-1)/5
1
, 5 subtracted
etc.
Comments
Post a Comment