Wiki - README‎ > ‎Programming‎ > ‎BASH‎ > ‎

Rerun

re-runs a command whenever given watchfile changes, playing sounds according to state changes.

not fully implemented for linux.

note: NOT SAFE!  use at your own risk!

#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: ${0} filetowatch command"
    exit 1
fi

# get script's path
# http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null

if [ `uname` == "Darwin" ]; then
    HASHFN="md5"
    SND_START="afplay -v .8 \"${SCRIPTPATH}/sounds/start.wav\""
    SND_SUCCESS="afplay -v .8 \"${SCRIPTPATH}/sounds/success.wav\""
    SND_FAILED="afplay -v .8 \"${SCRIPTPATH}/sounds/failed.wav\""
else
    HASHFN="md5sum"
    SND_START=""
    SND_SUCCESS=""
    SND_FAILED=""
fi


WATCHFILE="$1"
REEXECUTE="$2"

while [ 1 ]; do
    if [ -a "${WATCHFILE}" ]; then
        NHASH=`${HASHFN} ${WATCHFILE}`
    else
        NHASH="${HASH}"
    fi
    
    if [ "${HASH}" != "${NHASH}" ]; then
        echo "Rebuilding"
        eval "${SND_START}"
        eval "${REEXECUTE} && ${SND_SUCCESS} || ${SND_FAILED}"
        #if [ $? -eq 0 ]; then
        #    eval ${SND_SUCCESS}
        #else
        #    eval ${SND_FAILED}
        #fi
        HASH="${NHASH}"
    fi
    
    if [ -a "${WATCHFILE}" ]; then
        sleep 1
    else
        sleep 0.1
    fi
done



Comments