Skip to content

Instantly share code, notes, and snippets.

@dhhdev
Last active March 29, 2016 22:30
Show Gist options
  • Save dhhdev/e650f1497cb05ab11f08 to your computer and use it in GitHub Desktop.
Save dhhdev/e650f1497cb05ab11f08 to your computer and use it in GitHub Desktop.
I wanted a simple way to repeat a command purely in Bash: /usr/local/bin/live
#!/bin/bash
# A simple script to "watch" a command in pure Bash.
help() {
echo "live is a simple bash script, that keeps looping a specified command, just like the well known watch command. But made in Bash."
echo
echo " Usage:"
echo " live [arguments...] command"
echo
echo " Arguments:"
echo " -h This helpy helper. :)"
echo " -dX 'X' being a valid integer/number. 2 by default."
echo
echo " Examples:"
echo " live -d5 tree /path/to/directory"
echo " live -d 5 tree /path/to/directory"
echo " live tree /path/to/directory"
exit 3
}
usage() {
echo "Usage: $0 [-d <seconds>] commandtorun [arguments...]" 1>&2;
echo "Example: $0 -d 5 tree /path/to/directory" 1>&2;
exit 2;
}
# Defaults
delay=2
while getopts ":d:h?" opt; do
case "${opt}" in
h)
help
;;
d)
delay=${OPTARG}
[[ $delay =~ ^-?[0-9]+$ ]] || usage
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
while :; do
clear
$*
sleep $delay
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment