0

I am trying to create a bash script in which I want to execute a command x only if command y has been executed n times successfully. Thanks for your help in advance!!! And no question isn't duplicate of

2
  • Do you want it to retry command y unlimited times until it is successful 3 times or only try 3 times total? – jesse_b Feb 9 '20 at 16:22
  • No I don't want it to retry , just 3 times total – Madhubala Feb 9 '20 at 16:25
2

Using a loop:

#!/bin/bash

s=0

for ((i=1;i<=3;i++)); do
    if command y; then
        ((s++))
    fi
done

if ((s==3)); then
    command x
fi

We set the s parameter to 0 to keep track of our successful command y attempts. (not really necessary but I prefer to do it)
The for loop will then run 3 times, running command y each time, if command y is successful it will add 1 to s.
After the loop ends, if s equals 3 it will run command x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.