awesome-bashrc icon indicating copy to clipboard operation
awesome-bashrc copied to clipboard

Run a command n number of times.

Open mohitkyadav opened this issue 2 years ago • 0 comments

Usecase: Run a flaky test n number of times to test it if fails. But you can run any command.

#!/bin/sh

if [[ "$1" == "-h" ]]
then
    echo "
        Usage: repeat-cmd.sh <number of times to repeat> <command to repeat>
        Example: repeat-cmd.sh 5 "CI=true npm run test -- SomeComponent.test" -s

        -s: Silent mode, does not show any output from the command.
        "
    exit 0
fi

passed=0
totalTests=$1
i=0
while [ $i -ne $totalTests ]
do
    i=$(($i+1))
    if [[ "$3" == "-s" ]]
    then
        eval $2 > /dev/null 2>&1
    else
        echo "----------------------------------------------"
        echo "Test $i/$totalTests"
        echo "----------------------------------------------"
        eval $2
    fi


    if [ $? -eq 0 ]
    then
        passed=$(($passed+1))
        echo "Test $i: PASSED"
    else
        echo "Test $i: FAILED"
    fi
done

echo "Passed: $passed / $totalTests"

mohitkyadav avatar Jun 10 '22 16:06 mohitkyadav