From d72886b6c1d63f98d28248c681f19d7092b35cca Mon Sep 17 00:00:00 2001 From: bretello Date: Mon, 10 Jul 2023 09:11:08 +0200 Subject: [PATCH] zsh: functions: add watch_file function this can be used to watch a file for changes and execute the given action on changes. --- functions/misc.zsh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/functions/misc.zsh b/functions/misc.zsh index 426b34d..eef0459 100755 --- a/functions/misc.zsh +++ b/functions/misc.zsh @@ -284,3 +284,19 @@ function mangrep() { MANPAGER="less -p \"$pattern\"" man "$cmd" } + + +# watches the given file and executes the given action whenever the file is changed. Usage: watch_file +function watch_file() { + if ! which inotifywait &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi + [[ -z "$1" ]] && "Usage: $0 " && return 1 + local file + file="$1" + shift + + [[ -z "$@" ]] && "Usage: $0 " && return 1 + + local action + action="$@" + while inotifywait -e close_write "$file"; do zsh -c "$action"; done +}