Last active
February 15, 2018 09:42
-
-
Save peterbe/b66e93ffc4476f17e982f2c0e7daba74 to your computer and use it in GitHub Desktop.
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 | |
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 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