0

I am trying loop control. while loop then a for loop and inside it there are couple of if loops, if 3 of the if loop satisfies it should exit the for loop to continue with while loop else it should exit the if loop to continue rest of for loop iterations

LIMIT=100
while [ "count" -le "$LIMIT" ]
do
    for i in 1 2 3 4 5
    do
        var a
        var b
        var c
        var d
        if [ $d -eq 1 ] && [ $a == Done ] && [ $b -eq 0 ]
        then
         echo "$c" | tr '\n' '\t'
         echo "Successful"
         break 2        # Need to exit entire for loop
        elif [ $d -eq 0 ] && [ $a == Done ] && [ $b -eq 0 ]
        then
         break          # Need to go to next iteration of for
        elif [ $a == Active ]
        then
         echo "Active"  # Need to exit entire for loop
         break 2
        elif [ $d -eq 1 ] && [ $a == Done ] && [ $b -gt 0 ]
        then
         echo "Fail"    # Need to exit entire for loop
         break 2
        elif [ $a == Queued ]
        then
         echo "Queued"  # Need to exit entire for loop
         break 2
        else
         echo "Nothing"
        fi
    done
done

For some reason it exits everything when first if loop is true.

Sid
  • 161
  • 1
  • 10

1 Answers1

0

Got it, actually I was using break 2 which was exiting for as well as while as if is already being exited so using single break would let me exit for loop entirely, and to continue for next iteration for any of the if loop needed to use continue.

so the code should be

LIMIT=100
while [ "count" -le "$LIMIT" ]
do
    for i in 1 2 3 4 5
    do
        var a
        var b
        var c
        var d
        if [ $d -eq 1 ] && [ $a == Done ] && [ $b -eq 0 ]
        then
         echo "$c" | tr '\n' '\t'
         echo "Successful"
         break             # will exit entire for loop
        elif [ $d -eq 0 ] && [ $a == Done ] && [ $b -eq 0 ]
        then
         continue          # will go to next iteration of for
        elif [ $a == Active ]
        then
         echo "Active" 
         break             # will exit entire for loop
        elif [ $d -eq 1 ] && [ $a == Done ] && [ $b -gt 0 ]
        then
         echo "Fail"   
         break             # will exit entire for loop
        elif [ $a == Queued ]
        then
         echo "Queued"  
         break             # will exit entire for loop
        else
         echo "Nothing"
        fi
    done
done
Sid
  • 161
  • 1
  • 10