Skip to content

Instantly share code, notes, and snippets.

@drewfradette
Created May 6, 2012 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewfradette/2618197 to your computer and use it in GitHub Desktop.
Save drewfradette/2618197 to your computer and use it in GitHub Desktop.
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