在shell函数中inotifywait内部休眠不起作用

问题描述 投票:1回答:1

我正在尝试运行以下功能

foo () {
    sleep 1
    echo "outside inotify"
    (inotifywait . -e create |
    while read path action file; do
        echo "test"
        sleep 1
    done)
    echo "end"
}

直到inotify等待它正常运行;我看到:

>> foo
outside inotify
Setting up watches.
Watches established.

但是一旦创建文件,我就会得到

>>> fooo
outside inotify
Setting up watches.
Watches established.
test
foo:6: command not found: sleep
end

知道为什么吗?另外,我是否需要在inotifywait周围生成子进程()?有什么好处?

谢谢。

编辑我意识到我正在zsh上运行

zsh sleep inotifywait
1个回答
3
投票

read path搞砸了您,因为与POSIX兼容的shell不同,后者保证仅对具有全大写名称的变量进行修改才能对shell本身产生有害的副作用-zsh还具有特殊情况下的行为几个小写的名称,包括path

尤其是,zsh将path表示为与PATH中的值相对应的数组。为该数组分配字符串也会覆盖您的PATH

© www.soinside.com 2019 - 2024. All rights reserved.