Last active
December 11, 2015 04:19
-
-
Save ohgyun/4544524 to your computer and use it in GitHub Desktop.
A bash file monitor.
Watch a bashfile and run if it changed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Watch a bashfile and run it if changed | |
function bash_monitor () { | |
if [[ -z "$1" ]]; then | |
echo "usage: bash_monitor <bashfilename>" | |
exit 1 | |
fi | |
local file="$1" | |
local tmp1="$HOME/.tmp_bash_monitor_1" | |
local tmp2="$HOME/.tmp_bash_monitor_2" | |
# create temporary files to compare status | |
touch "$tmp1" "$tmp2" | |
while true; do | |
ls -l "$file" > "$tmp1" | |
diff "$tmp1" "$tmp2" > /dev/null \ | |
|| { | |
echo | |
echo '[BASH MONITOR] File changed.' | |
echo '----------------------------' | |
# run bash with parameters from second if changed | |
source "$file" "${@:2}" | |
} | |
cp "$tmp1" "$tmp2" | |
sleep 1 | |
done | |
} | |
bash_monitor "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment