markovuksanovic
markovuksanovic

Reputation: 15906

Puzzle: what does this bash command do?

Found this in https://github.com/RocketChat/Rocket.Chat/blob/master/.circleci/config.yml and I'm really puzzled with what this command actually does. Can anyone enlighten me?

for i in $(seq 1 5); do
    npm test && s=0 && break || s=$? && sleep 1
done
(exit $s)

Upvotes: 0

Views: 64

Answers (1)

anubhava
anubhava

Reputation: 784908

Converting my comment to an answer as suggested.

This script attempts to run npm test command maximum 5 times and exits with exit status of npm at the first success or 5 failures. For each failed attempt it sleeps for 1 second before next attempt.

This script may be rewritten as (for understanding):

for ((i=1; i<=5; i++)); do
    if npm test; then
       s=0
       break
    else
       s=$?
       sleep 1
    fi
done
exit $s

Upvotes: 1

Related Questions