Reputation: 4084
So I want to pass a for loop in my bash script, and I want it to stop depending on two parameters:
for (( x=1; x<= 50 -a $array_position -lt ${#array[@]}; x++ ))
do
echo ${array[$array_position]}
array_position=$((array_position+1))
done
My intention is to have this for loop echo 50 consecutive array values [0] -- [50], but stop if $array_position reaches the end of the array before all 50 loop iterations complete.
Any help is appreciated, as always!
Upvotes: 0
Views: 588
Reputation: 274562
The problem is the -a
or -lt
in your for
statement. Change it to this:
for (( x=1; x<= 50 && $array_position < ${#array[@]}; x++ ))
Or to simplify the whole thing further:
for (( x=0; x < 50 && x < ${#array[@]}; x++ ))
do
echo "${array[$x]}"
done
Upvotes: 1
Reputation: 535
You should only need to specify the array size as your test, and use a break statement if you reach 50:
for (( x=1; x<=${#array[@]}; x++ ))
do
echo ${array[$array_position]}
array_position=$((array_position+1))
[ $x -eq 50 ] && break
done
Upvotes: 0