1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-24 12:31:31 +01:00

zsh: functions: add watch_file function

this can be used to watch a file for changes and execute the
given action on changes.
This commit is contained in:
bretello 2023-07-10 09:11:08 +02:00
parent e08ee08768
commit d72886b6c1
Signed by: brethil
GPG Key ID: 876AAC6290170FE7

View File

@ -284,3 +284,19 @@ function mangrep() {
MANPAGER="less -p \"$pattern\"" man "$cmd" 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
}