Linux 为什么这个inotifywait shell脚本会有两个PID?

Linux 为什么这个inotifywait shell脚本会有两个PID?,linux,bash,unix,inotifywait,Linux,Bash,Unix,Inotifywait,我正在学习使用inotifywait,特别是使用以下位置的脚本:。我不明白的是,为什么我的脚本在使用pid x时总是出现两次 36285 pts/1 S+ 0:00 /bin/bash ./observe2.sh /home/user1/testfolder 36286 pts/1 S+ 0:00 inotifywait -m /home/user1/testfolder -e create -e moved_to 36287 pts/1 S+ 0:00

我正在学习使用inotifywait,特别是使用以下位置的脚本:。我不明白的是,为什么我的脚本在使用
pid x
时总是出现两次

36285 pts/1    S+     0:00 /bin/bash ./observe2.sh /home/user1/testfolder
36286 pts/1    S+     0:00 inotifywait -m /home/user1/testfolder -e create -e moved_to
36287 pts/1    S+     0:00 /bin/bash ./observe2.sh /home/user1/testfolder
为了更快地进行测试,我更改了链接脚本,以便您可以通过$1传递任何文件夹进行观察,并保存为
observe2.sh

#!/bin/bash
inotifywait -m $1 -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        # do something with the file
    done

为什么脚本过程显示两次?在这个过程中有没有叉子?有人能解释为什么这两个进程的行为会发生吗?

因为它有一个管道

管道分叉子shell(第二个进程)并连接它们。在
foo | bar
的情况下——这些都是外部的非shell命令——子shell
exec
会删除实际的命令,从而丢失它们的进程树条目。当在shell中写入该管道的元素时,执行它的shell将保留在进程树中


也就是说,我建议[1]写得有点不同:

while read -r path action file; do
    echo "The file '$file' appeared in directory '$path' via '$action'"
    # do something with the file
done < <(exec inotifywait -m "$1" -e create -e moved_to)
读取-r路径动作文件时;做
echo“文件“$file”通过“$action”出现在目录“$path”中”
#对这个文件做些什么

完成,因为它有一个管道

管道分叉子shell(第二个进程)并连接它们。在
foo | bar
的情况下——这些都是外部的非shell命令——子shell
exec
会删除实际的命令,从而丢失它们的进程树条目。当在shell中写入该管道的元素时,执行它的shell将保留在进程树中


也就是说,我建议[1]写得有点不同:

while read -r path action file; do
    echo "The file '$file' appeared in directory '$path' via '$action'"
    # do something with the file
done < <(exec inotifywait -m "$1" -e create -e moved_to)
读取-r路径动作文件时;做
echo“文件“$file”通过“$action”出现在目录“$path”中”
#对这个文件做些什么
完成<