commit 3ef3b8e4e2ee7c9e060fdd163752a71fbba33c4b
parent 5c7e0664d5e4d5f337127f14c84bcb03c28d7929
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Mon, 29 Jul 2019 08:34:26 +0200
Improve watch.sh
Diffstat:
1 file changed, 57 insertions(+), 7 deletions(-)
diff --git a/.local/bin/watch.sh b/.local/bin/watch.sh
@@ -1,19 +1,69 @@
-#!/usr/bin/env bash
-set -e
+#!/bin/sh
+
+version=1.0
+waitsecs=5
+
+show_help() {
+ echo "usage: ${0##*/} [OPTIONS] COMMAND"
+ echo "will repeatedly run COMMAND every $waitsecs seconds."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -n, --interval SECONDS interval between command invocations"
+ echo " -- do not consider any following args as options"
+}
+
+show_version() {
+ echo "${0##*/} version $version"
+ echo "Licensed under the GNU Public License, v3+"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://gitlab.com/admesg/dotfiles"
+}
+
+die() {
+ printf '%s\n' "$1" >&2
+ exit 1
+}
[ -f ~/.commands.sh ] && . ~/.commands.sh
-waitsecs=5
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -n|--interval)
+ waitsecs="$2"
+ shift
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'Error: Unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
if [ $# -lt 1 ]; then
- (>&2 echo "Error: $0 needs to be passed a command");
- exit 1
+ show_help
+ exit 1
fi
while : ; do
clear
- echo -n "Every ${waitsecs}s: $*"
- echo -e "\t $(hostname): $(date)\n"
+ printf "%s" "Every ${waitsecs}s: $*"
+ printf "\t %s\n" "$(hostname): $(date)"
"$@"
sleep "$waitsecs"
done