Daniel Lubarov

Simple "watch" script for OSX

Here's a fork of this watch script with some improvements:

#!/usr/bin/env bash
# usage: watch [command] [sleep duration]
while :; do 
  clear
  date
  bash -c "$1"
  sleep ${2:-1}
done

Example usage:

watch 'fortune | cowsay' 5

2 Comments

user avatar
Greg T on

Following modifications change the syntax to match Unix watch command, and don't flicker the screen when the command takes time to compute (e.g. du -s on a large tree):

#!/usr/bin/env bash
# usage: watch [command] [sleep duration]
tmpfile="`mktemp`"
trap "rm -f \"$tmpfile\"" EXIT # Automatically delete temp file on exit

sec=2
case "$1" in
-n) sec="$2"
shift 2 ;;
esac

clear
date
while :; do
bash -c "$1" > "$tmpfile" 2>&1
clear
date
cat "$tmpfile"
sleep "$sec"
done

user avatar
Ben on

#!/usr/bin/env bash
# usage: watch [command] [sleep duration]
while :; do
clear
date
bash -c "$1" | head -n $(( $(tput lines) - 1 ))
sleep ${2:-1}
done