BASH loop inside a loop question

Hi all

Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !. unfortunately this utility is third party and I cant fix it.

The utility should generate a unique 4 digit number, e.g. "5677" but when it fails it generates "0000" (and it fails more often than not)

My question is this, i want to write a while loop that tests for "0000" (i.e. failed) and if "0000" re-run the program, but at the same time, I don't want the script to continue running indefinitely, because the program may conceivably return a correct value on the 5'000th re-run or never (unlikely but you get the idea) . So i want it to give up after 20 goes

so basically, I have been attempting and failing miserably to write a script that does the following...

while $OUTPUT ="0000"; do `rerun the binary` # when it returns something other than "0000" return true and jump out of the loop... but if it continues to fail more than 20 times then give up and jump out of the loop

is this possible ? do i have to put a loop inside a loop ?

any help or advice would be greatly appreciated

---------- Post updated at 05:49 AM ---------- Previous update was at 04:48 AM ----------

sorry, I forgot to put up my example code, unfortunately, I have no means to test this but would something like this work ?

RESULT=`/usr/local/bin/myutility`
COUNT=0

while [ $RESULT = "0000" ]; do
    COUNT=`expr $COUNT + 1`
    
    if [ $COUNT -lt 20 ]; then 
        RESULT=`/usr/local/bin/myutility`
    fi
done

Try this:

#!/usr/bin/ksh

RESULT=`/usr/local/bin/myutility`
COUNT=0

while [ "${RESULT}." = "0000." ]
do
    (( COUNT += 1 ))
   
    if (( COUNT <= 20 ))
    then 
        RESULT=`/usr/local/bin/myutility`
    else
       break
    fi
done

thanks Klashxx, will that work in BASH (as that is what my script is written in)

Yes , you only have to set the brake statement :

RESULT=`/usr/local/bin/myutility`
COUNT=0

while [ $RESULT = "0000" ]; do
    COUNT=`expr $COUNT + 1`
    
    if [ $COUNT -lt 20 ]; then 
        RESULT=`/usr/local/bin/myutility`
    else
        break
    fi
done

thanks