Skip to content

Instantly share code, notes, and snippets.

@peterbe
Last active February 15, 2018 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/b66e93ffc4476f17e982f2c0e7daba74 to your computer and use it in GitHub Desktop.
Save peterbe/b66e93ffc4476f17e982f2c0e7daba74 to your computer and use it in GitHub Desktop.
#!/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
@lamby
Copy link

lamby commented Feb 13, 2018

This should be:

"$@"

.. by itself on a newline (without the eval) otherwise you expand spaces. Try, for example, running ls "foo bar" in a loop. With a raw eval $@ you would see "cannot open foo, cannot open bar` which is incorrect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment