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

Madhubala
  • 304

1 Answers1

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.

jesse_b
  • 37,005