zsh: functions: add watch_file function

this can be used to watch a file for changes and execute the
given action on changes.
fix-ci
bretello 2023-07-10 09:11:08 +02:00
parent e08ee08768
commit d72886b6c1
Signed by: brethil
GPG Key ID: 876AAC6290170FE7
1 changed files with 16 additions and 0 deletions

View File

@ -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 <file> <command>
function watch_file() {
if ! which inotifywait &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi
[[ -z "$1" ]] && "Usage: $0 <file> <action>" && return 1
local file
file="$1"
shift
[[ -z "$@" ]] && "Usage: $0 <file> <action>" && return 1
local action
action="$@"
while inotifywait -e close_write "$file"; do zsh -c "$action"; done
}