Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
watchc - Watch with colour
#!/bin/bash
# Filename watchc
# Description execute a program periodically,showing fullscreen, colored output
# Author Drew Fradette <http://www.github.com/drewfradette>
# Version 0.5
# Last Updated 2012-01-31
# Usage watchc -n 5 ls -lah /tmp
# bash_version 4.2.10(1)-release
#######################################################
usage() {
echo -e "Usage: watchc [-t] [-n <interval>] <command>\n"
echo -e "-h\tPrint a summary of the available options."
echo -e "-n\tSeconds to wait between updates. Defaults to 2 seconds."
echo -e "-t\tTurns off showing the header."
}
# Initialize arguments to default values.
SHOW_TITLE=1
RECHECK=2
# Parse arguments
while getopts "htn:" OPTION; do
case $OPTION in
h) usage; exit;;
t) SHOW_TITLE=0;;
n) RECHECK=$OPTARG;;
*) usage; exit;;
esac
done
# Fetch the command to run
shift $((OPTIND-1))
COMMAND="$@"
if [ "$COMMAND" == "" ]; then
echo -e "Error: You must specify a command\n"
usage; exit 1;
fi
# The Business
while true; do
TITLE=$(printf "Every %.1fs: %s" $RECHECK "$COMMAND")
if [ $SHOW_TITLE -eq 1 ]; then
PADDING=$(($(tput cols)-${#TITLE}))
TITLE=$(printf "${TITLE}%${PADDING}s" "$(date)")
fi
clear; echo -e "\e[0;33m$TITLE\e[0m";
$COMMAND; sleep $RECHECK;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment