Skip to content

Instantly share code, notes, and snippets.

@gammy
Created April 20, 2012 09:53
Show Gist options
  • Save gammy/2427427 to your computer and use it in GitHub Desktop.
Save gammy/2427427 to your computer and use it in GitHub Desktop.
Poor man's watch
#!/bin/bash
# Poor man's "watch"
# Depends on bash, date, stty & head.
if [ ${#@} -lt 2 ]; then
echo "Usage: $(basename $0) <interval> <command>"
exit 1
fi
t=$1
shift
line="Every $t: $*"
clear
while true; do
# Calculate and draw top line with padding
ts=$(date)
dims=($(stty size))
let padlen="${dims[1]} - (${#ts} + ${#line})"
padding=$(printf "%${padlen}s")
echo -e "\e[1m$line$padding$ts\e[0m\n"
# Execute command, show top $height rows, then sleep
let height=${dims[0]}-3
$* | head -n $height
sleep $t
clear
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment