watch.sh (672B)
1 #!/bin/sh 2 3 die() { 4 printf '%s: error: %s\n' "${0%%*/}" "$1" >&2 5 exit 1 6 } 7 8 usage() { 9 die "usage: ${0##*/} [-h] [-n SECONDS] COMMAND" 10 } 11 12 wait=5 13 while :; do 14 case "$1" in 15 -h) 16 usage;; 17 -n) 18 wait="$2" 19 shift;; 20 --) # end all options 21 shift 22 break;; 23 -?*) 24 die 'unknown option specified' 25 break;; 26 *) 27 break;; 28 esac 29 shift 30 done 31 32 if test $# -lt 1; then 33 usage 34 fi 35 36 if test -f ~/.profile; then 37 . ~/.profile 38 fi 39 40 host="${USER}@$(hostname):${PWD}" 41 while :; do 42 clear 43 printf '%s (%ds) %s $ %s\n' \ 44 "$(date '+%F %T')" "${wait}" "${host}" "$*" 45 eval "$@" 46 stat="$?" 47 if test "${stat}" -ne 0; then 48 printf 'return: %d\n' "${stat}" 49 fi 50 sleep "${wait}" 51 done