0
 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.

  • what does the loop, and the loop variable `$i`, have to do with this code (ie, `$i` is not referenced anywhere in the body of the loop so you're going to run through the loop 3x times executing the same `command 1` and `command 2` each time through the loop? shouldn't the `if` tests be referencing the success/failure of `command 1` (currently referencing `command 2` which hasn't been executed, yet)? and shouldn't the actions of the `if` clauses be to (not) run `command 2`? since this looks like pseudo code ... what's the actual code you've written to implement this logic? – markp-fuso May 29 '20 at 17:52
  • $i will be used in command 1 and command 2 which I have not specified here. – Question Mark May 29 '20 at 17:54
  • [Conditional evaluation](https://www.gnu.org/software/bash/manual/html_node/Lists.html)? `cmd1 && cmd2` (Even a basic if/then/elif/else would do.) – Paul Hodges May 29 '20 at 18:42
  • so ... run command #1 and regardless of the result you then want to run command #2; if command #2 is 'successful' then continue to the next pass through the loop, but if command #2 'failed' then run command #1 again (and then proceed to the next pass through the loop) ? How do you define success and failure, eg, are you looking the return code (eg, $?) or some value/string in the response (eg, have to `grep` the command's stdout)? – markp-fuso May 29 '20 at 19:27
  • yes correct. i am grepping the command 2 's out put . i can run the 2nd command only after the 1st command is executed. my 1st command does not return anything or does not give any stdout and hence I won't know whether it was a success or failure . my 2nd command determines whether the 1st command has done its part. if that is not done .. i will have to run the 1st command again – Question Mark May 29 '20 at 20:19
  • for ( i=3; i<5; i++) do echo "i" >> text.txt check that text.txt actually has the i value.. if i value is not in text.txt then execute echo again (only once and continue the loop) if text.txt has the i value ..then exit continue the loop – Question Mark May 29 '20 at 20:22

2 Answers2

2

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
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
0

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