Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 脚本以查看文件夹中的新文件,找到后,根据文件名调用不同的脚本_Unix - Fatal编程技术网

Unix 脚本以查看文件夹中的新文件,找到后,根据文件名调用不同的脚本

Unix 脚本以查看文件夹中的新文件,找到后,根据文件名调用不同的脚本,unix,Unix,我正在尝试设计一个文件监视程序解决方案,在该解决方案中,我需要每天监视一个特定文件夹中的不同文件名,一旦找到文件名,我需要调用一个特定于该文件名的脚本 例如: Watch Folder - file1.txt file2.txt file3.txt call script.sh abc file1 call script.sh abc file2 call script.sh abc file3 我试图利用inotifywait,但未能使其发挥作用。任何帮助都将不胜感激 sftp_home=

我正在尝试设计一个文件监视程序解决方案,在该解决方案中,我需要每天监视一个特定文件夹中的不同文件名,一旦找到文件名,我需要调用一个特定于该文件名的脚本

例如:

Watch Folder -
file1.txt
file2.txt
file3.txt

call script.sh abc file1
call script.sh abc file2
call script.sh abc file3
我试图利用inotifywait,但未能使其发挥作用。任何帮助都将不胜感激

sftp_home=/app/public/ent_sftp
script=/app/public/bin
curr_date=$(TZ=":US/Eastern" date '+%Y%m%d')

inotifywait -m $sftp_home -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
if [ "$file" = "file1${curr_date}*.txt" ]; then
echo "file1${curr_date}*.txt was found and process will be initiated"
cd $script
./script.sh file1
elif [ "$file" = "file2${curr_date}*.txt" ]; then
echo "file2${curr_date}*.txtwas found today and process will be initiated"
cd $script
./script.sh file2
fi
done
谢谢,
Kavin

如果您想在比赛中进行glob扩展,可以使用case语句:

unset arg
case $file in
file1${curr_date}*.txt)
        arg=file1
        ;;
file2${curr_date}*.txt)
        arg=file2
        ;;
*)
        echo No file found >&2
        ;;
esac
if test -n "$arg"; then
        echo "${arg}${curr_date}*.txt was found and process will be initiated"
        cd $script
        ./script.sh "$arg"
fi

如果[“$file”=“file1${curr\u date}*.txt”]
将尝试匹配文本字符串,并且不会进行任何形式的全局扩展。您的姓名是否包含文字
*
?@williampersell您好,谢谢您的回复。啊,明白了,不,他们没有。实际上,它们最终会随机生成一个数字,即文件_06_07_2021_021.txt。非常感谢,这很有魅力!非常感谢你的帮助。实际上,我必须为每种情况向脚本传递两个参数,这将使最后的脚本调用类似于./script.sh“$arg1”$arg2。如何使用此脚本实现此目的?请在案例的每个部分中指定参数。在
之间可以有任意多个命令在每个部分。再次感谢!谢谢。