Loop a command until it exits with a specified exit code or a pattern is matched in its output.
Loop the specified command until it exits with one of the specified exit codes or a pattern is matched in its output.
Additionally, a timeout value may be supplied. Setting the timeout to '0' has the same effect as not supplying it.
The sleep option may be used to make the script wait before executing the command again.
The pattern matching mode disables checking the exit code. The command is looped UNTIL the pattern matches its output; if the matching is inverted, the command is looped WHILE the pattern matches its output. The pattern may be a fixed string or a regular expression. The pattern matching's case-insensitivity can be toggled.
Requirements: bash 4.x+, sleep, grep, mktemp
Copy the loop
script to your $PATH
.
Display the help message to check the script's availability:
loop -h
Call curl
until it exits with 0:
loop -c "curl https://www.google.com"
Call wget
until it exits with 0, 4, 5 or 6:
loop -c "wget https://www.google.com" -e "4 5 6"
Call traceroute
until it exits with 0, "sleeping" 3 seconds between each call:
loop -c "traceroute google.com" -s 3
Call ping
until it exits with 0 or 1, or 20 seconds has passed:
loop -c "ping https://www.google.com" -t 20 -e 1
Call script.sh
until its output contains "ok" case-insensitively:
loop -c "./script.sh" -g ok -i
Call script.sh
while its output contains a line that matches the regexp:
loop -c "./script.sh" -g "^a.*z$" -E -v
The automated tests make use of the shUnit2 test framework. shUnit2 is licensed under the Apache License version 2.0.
To run the tests change to the test
directory and execute:
./loop_test
The tests will work only if you are running them from the test
directory,
and if a copy of loop
is available in the repo's root directory.
Static code analysis was made with shellcheck.
Code coverage was measured with bashcov.
- Gábor Némethi - nemethi
This project is licensed under the MIT License - see the LICENSE file for details