Question Mark
Question Mark

Reputation: 25

Retry a command only once : when a command fails (in bash)

 for ( i=3; i<5; i++)

 do

   execute some command 1

   if command 2 is successful then do not run the command 1 (the for loop should continue)

   if command 2 is not successful then run command 1 only once (like retry command 1 only once, after this the for loop should continue)    

done

This is to note that command 2 is dependent on command 1 and command 2 can only be executed after command 1

for example:

 for ( i=3; i<5; i++)
 do
    echo "i" >> mytext.txt   ---> command 1 
    if "check the content of mytext.txt file to see if the value of i is  actually added" ---> command 2  
    if it is not added then execute echo "i" >> mytext.txt (command 1) again and only once.
    if i value is added to the file .. then exit and continue the loop
  done

Since the "command 1" is quite big and not just an example echo statement here.I do not want to add "command 1" twice .. once outside and once inside the if condition. I want this logic in an optimized way with no redundancy of code.

Upvotes: -1

Views: 1433

Answers (3)

Payam
Payam

Reputation: 1209

You could do it much easier like this:

command1 && (command2 || command1) || true

Upvotes: 0

markp-fuso
markp-fuso

Reputation: 34324

Per a comment it sounds like the OP may need to invoke command 1 up to 2 times for a given $i value, but only wants to type command 1 once in the script.

Siddhartha's suggestion to use a function is probably good enough but depending on the actual command 1 (OP mentions that it's 'quite big') I'm going to play devil's advocate and assume there could be additional issues with passing some args to the function (eg, a need to escape some characters ... ??).

The general idea is to have an internal loop that can be executed at most 2 times, with logic in the loop that will allow for an 'early' exit (eg, after just one pass through the loop).

Since we're using pseudo-code I'll use the same ...

for ( i=3; i<5; i++ )
do
    pass=1                                    # reset internal loop counter

    while ( pass -le 2 )
    do
        echo "i" >> mytext.txt                # command 1

        if ( pass -eq 1 )                     # after first 'command 1' execution
        && ( value of 'i' is in mytext.txt )  # command 2
        then
            break                             # break out of inner loop; alternatively ...
        #   pass=10                           # ensure pass >= 2 to force loop to exit on this pass
        fi

        pass=pass+1                           # on 1st pass set pass=2 => allows another pass through loop
                                              # on 2nd pass set pass=3 => will force loop to exit
    done
done

Upvotes: 2

Siddhartha Guerrero
Siddhartha Guerrero

Reputation: 69

you can declare functions like

function command
{
  your_command -f params

}

for ( i=3; i<5; i++)

 do
   if command ; then 
      echo "success"
   else
      echo "retry"
      command
    fi

done

Upvotes: 1

Related Questions