0

我在用:

inotifywait -m -q -e close_write --format %f . | while IFS= read -r file; do
cp -p "$file" /path/to/other/directory
done

监视文件夹以完成文件,然后将其移动到另一个文件夹。

文件成对但在不同的时间制作,即 File1_001.txt 在下午 3 点制作,File1_002.txt 在晚上 9 点制作。我想监视两个文件的完成情况,然后启动一个脚本。

script.sh File1_001.txt File1_002.txt

所以我需要另一个 inotifywait 命令或不同的实用程序,它也可以识别两个文件都存在并完成,然后启动脚本。

有谁知道如何解决这个问题?

4

1 回答 1

1

我找到了一个安装了 Linux 的inotifywait机器,所以现在我了解了它的作用和工作原理。:)

这是你需要的吗?

#!/bin/bash

if [ "$1" = "-v" ]; then
        Verbose=true
        shift
else
        Verbose=false
fi

file1="$1"
file2="$2"

$Verbose && printf 'Waiting for %s and %s.\n' "$file1" "$file2"

got1=false
got2=false
while read thisfile; do
        $Verbose && printf ">> $thisfile"
        case "$thisfile" in
                $file1) got1=true; $Verbose && printf "... it's a match!" ;;
                $file2) got2=true; $Verbose && printf "... it's a match!" ;;
        esac
        $Verbose && printf '\n'
        if $got1 && $got2; then
                $Verbose && printf 'Saw both files.\n'
                break
        fi
done < <(inotifywait -m -q -e close_write --format %f .)

这会运行一个inotifywait,但会在一个循环中解析其输出,当看到命令行 ($1$2) 上的两个文件都已更新时,该循环会退出。

请注意,如果一个文件已关闭,然后在第二个文件关闭时重新打开,则此脚本显然不会检测到打开的文件。但这在您的用例中可能不是问题。

请注意,构建解决方案的方法有很多种——我只向您展示了一种。

于 2016-03-18T12:11:20.250 回答