#!/usr/bin/env bash | |
set -eo pipefail | |
count=0 | |
_stopnow() { | |
count="$(($count+1))" | |
test -f stopnow && \ | |
echo "Stopping after $count iterations!" && \ | |
rm stopnow && exit 0 || return 0 | |
} | |
control_c() | |
# run if user hits control-c | |
{ | |
echo "Managed to do $count iterations" | |
exit $? | |
} | |
# trap keyboard interrupt (control-c) | |
trap control_c SIGINT | |
echo "To stop this forever loop created a file called stopnow." | |
echo "E.g: touch stopnow" | |
echo "" | |
echo "Now going to run '$@' forever" | |
echo "" | |
while true | |
do | |
_stopnow | |
eval $@ | |
# Do this in case you accidentally pass an argument | |
# that finishes too quickly. | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This should be:
.. by itself on a newline (without the
eval
) otherwise you expand spaces. Try, for example, runningls "foo bar"
in a loop. With a raweval $@
you would see "cannot open foo, cannot open bar` which is incorrect