Created
March 24, 2024 23:40
-
-
Save thingsiplay/985de2f8c61ea0dc47375f606b5abed7 to your computer and use it in GitHub Desktop.
Run a command n-times or infinite times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Run a command n-times or infinite times. | |
# by Tuncay D. (thingsiplay) | |
# | |
# Usage: loop [N] COMMAND | |
# | |
# If the first argument N is a number, then it will run the command that many | |
# times. If N is not a number, then it will be interpreted as part of the | |
# actual command and run indefinitely. | |
# | |
# Examples: | |
# | |
# # Run command 3 times. | |
# loop 3 echo hello wonderful person \&\& sleep 1 | |
# | |
# # Benchmark a command by running it 10 times without output. | |
# time loop 10 grep -F [ ~/.* 1>/dev/null 2>/dev/null | |
# | |
# # Watch until a file containing "abc" is created in home then stop. | |
# loop 'sleep 1 ; ls ~/ | grep abc && echo found && exit' | |
int='^[0-9]+$' | |
if ! [[ ${1} =~ ${int} ]]; then | |
while :; do | |
eval ${*} | |
done | |
else | |
n="${1}" | |
shift | |
for _ in $(seq 1 "${n}"); do | |
eval ${*} | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment