Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Watch Shell Script for Makefiles
#! /bin/zsh
# Setup empty variables
last_error_message=""
last_error_timestamp=""
last_error_time=""
last_message=""
time_since=0
while true;
do
# Run watch
result=$(make watch)
# If non-zero exit code (i.e. an error)
if [ $? -ne 0 ]
then
# Strip colour tags
message=$(echo "$result" | sed -E "s/\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
# Send message to notification centre
terminal-notifier -message "$message" -title "Watch Error"
# Output error in console
last_error_message=$result
last_error_time=$(date +"%T")
last_error_timestamp=$(date +%s)
last_message=""
else
last_message=$result
fi
# Clear the console
clear
# Right align the time
time=$(date +"%T")
title="Watching..."
let pos=$(tput cols)-${#title} # Work out how much to pad to right align
printf "\e[34m%s\e[33m%${pos}s\e[39m\n" $title "$time"
# If there was a previous error
if [ -n "$last_error_message" ]; then
# Work out how long ago the error occurred
let time_since=$(expr $(date +%s) - $last_error_timestamp)/60
# Print how long ago the error occurred
if [ $time_since -eq 0 ]; then time_since="Less than a minute ago"; else time_since="$time_since minutes ago"; fi
# Print the error
printf "\n\n\e[31mLast Error @ %s (%s)\n\e[39m\n%s\r\n\n" "$time_since" "$last_error_time" "$last_error_message"
# Print a line across the screen
printf '=%.0s' {1..$COLUMNS}
fi
# If there was a success message
if [ -n "$last_message" ]; then
printf "\n\n\e[32mLast Message\e[39m\n\n"
printf "%s\n" "$last_message"
fi
# Accept sleep time from command line, otherwise set to 0.5 seconds
if [ -n "$1" ]
then
sleep "$1"
else
sleep 0.5
fi
done
@smallhadroncollider

This comment has been minimized.

Copy link
Owner Author

commented Jul 22, 2014

A bash script for running make watch on a regular interval. Will output errors to the terminal and keep them there until the next error.

Can run with sh watch.sh or, if you want an interval other than 0.5s, sh watch.sh <interval in seconds> (e.g. sh watch.sh 2 to run every two seconds)

Uses Mac OS X terminal-notifier to log errors to Notification Centre. Comment out line 17 if you do not want this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.