Skip to content

Run a command n number of times. #45

@mohitkyadav

Description

@mohitkyadav

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions