aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncaen <mail@duncano.de>2016-09-12 20:27:27 +0200
committerDuncaen <mail@duncano.de>2016-09-12 20:27:27 +0200
commit91edde26634358d8994def797e8ce6340c458459 (patch)
tree872d8c5c30000cb4f3129f9f431a0f851fca2e83
parent5f9e1f1e801481e77a7ee547d97eb955e634bdd8 (diff)
downloaddotfiles-91edde26634358d8994def797e8ce6340c458459.tar.gz
bin/timer: add
-rwxr-xr-xbin/timer39
1 files changed, 39 insertions, 0 deletions
diff --git a/bin/timer b/bin/timer
new file mode 100755
index 0000000..d72d8d9
--- /dev/null
+++ b/bin/timer
@@ -0,0 +1,39 @@
+#!/bin/sh
+# timer [hh:mm[:ss]]... [[0-9]*[h|m|s]]... -- prog [args...]
+# Examples:
+# 2 hours
+# timer 2h -- echo "hi"
+# timer 2:0 -- echo "hi"
+# 1 minute and 10 seconds
+# timer 1m 10s -- echo "hi"
+# timer 0:1:10 -- echo "hi"
+
+err() {
+ printf 'Error: %s\n' "$1"
+ exit 1
+}
+usage() {
+ sed -n '2,9p' $0 | sed -e 's/^# //'
+ exit 1
+}
+
+for t; do
+ case "$t" in
+ -h|--help|help|usage) usage ;;
+ *:*:*) ;;
+ *:*) th=${t%:*}; tm=${t#*:}; h=$(( h + th )); m=$(( m + tm )) ;;
+ [0-9]*h*) t=${t%h*}; h=$(( h + t )) ;;
+ [0-9]*m*) t=${t%m*}; m=$(( m + t )) ;;
+ [0-9]*s*) t=${t%s*}; s=$(( s + t )) ;;
+ --) shift 1; break ;;
+ *) break ;;
+ esac
+ shift 1
+done
+
+[ $(command -v "$1") ] || err "couldn't find '$1'"
+prog=$1; shift 1; args=$@
+
+t=$(( s + (m * 60) + (h * 60 * 60) ))
+# echo "sleep $(( s + (m * 60) + (h * 60 * 60) )) && $prog $args"
+sleep $t && $prog $args