Skip to content

Instantly share code, notes, and snippets.

@mminer
Created July 10, 2018 16:54
Show Gist options
  • Select an option

  • Save mminer/c749ace6e22a12a01fbbb201449c9f73 to your computer and use it in GitHub Desktop.

Select an option

Save mminer/c749ace6e22a12a01fbbb201449c9f73 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Watches a file and prints when a specified variable value changes.
# We assume the file is a list of key=value pairs separated by newlines.
if [ $# -ne 2 ]; then
echo "Usage: $0 FILE KEY"
exit 1
fi
file="$1"
key="$2"
read_value() {
grep --only-match --perl-regexp "${key}=\K.*" "${file}"
}
value="$(read_value)"
while true; do
inotifywait --quiet --quiet "${file}"
new_value=$(read_value)
if [ "${new_value}" == "${value}" ]; then
continue
fi
value="${new_value}"
echo "${value}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment