Bash 多重';查找';对于一次观察者-仅执行第一个
我正在尝试运行一个脚本,它将监视不同的文件夹和文件,如果它们发生更改,则执行一个命令。其中一个文件夹路径接受动态变量,该变量将根据在CLI中运行脚本时输入的内容进行更改,而另一个不会更改 但是,根据文件路径,需要采取不同的操作Bash 多重';查找';对于一次观察者-仅执行第一个,bash,shell,find,watch,Bash,Shell,Find,Watch,我正在尝试运行一个脚本,它将监视不同的文件夹和文件,如果它们发生更改,则执行一个命令。其中一个文件夹路径接受动态变量,该变量将根据在CLI中运行脚本时输入的内容进行更改,而另一个不会更改 但是,根据文件路径,需要采取不同的操作 #!/usr/bin/env bash THEME=$1 while true; do echo "inside find" find code/themes/$THEME/src -name '*.html' -o -name '*.ts' -o -na
#!/usr/bin/env bash
THEME=$1
while true;
do
echo "inside find"
find code/themes/$THEME/src -name '*.html' -o -name '*.ts' -o -name '*.scss' | entr -d rsync -avh code/themes/$THEME/* ./
find code/base/src -name '*.html' -o -name '*.ts' -o -name '*.scss' | entr -d rsync -avh code/base/* ./
done
当我运行脚本时发生的情况是,它只执行并将手表放在第一个查找位置(在上面粘贴的代码示例中,仅动态路径)
目前我的问题是:
- 只调用第一个查找,并且只在代码/themes/$THEME/src路径上放置一个监视
- 第二个发现永远不会执行
如果您的任务只是确保两个(或1+个主题)目录的下游副本始终是最新的,则每隔n分钟同步一次的
cron
作业可能是最简单和最健壮的解决方案cron
有每分钟一次的粒度,尽管每五分钟一次的粒度对于您的用例来说已经足够了rsync
by design不会复制已经是最新的文件,因此在无操作的情况下,它应该可以相当快地执行
* * * * * bin/rsyncthemes
(这是每分钟一次;在Linux上,您可以在第一个字段中说每5分钟运行一次*/5
,但这并不适用于所有cron
方言。)。。。其中,rsyncthemes
包含如下内容
#!/bin/sh
# cron jobs always start in your home directory,
# but you want to put an absolute path here
# to run this in the directory where you want rsync to copy these files
cd somewhere
for dir in code/themes/one \
code/themes/other \
code/base
do
rsync -avh "$dir"/* ./
done
for dir in code/themes/one \
code/themes/other \
code/base
do
while true; do
find "$dir"/src -name '*.html' -o -name '*.ts' -o -name '*.scss' |
entr -d rsync -avh "$dir"/* ./
done & # <-- run each loop as a background job
done
显然,这需要标记为可执行(chmod a+x rsyncthemes
),并保存在我们在cron
作业中指定的目录中(即主目录中的bin
)
但是,如果您执意使用entr
来避免不必要地运行rsync
,我猜您需要类似的
#!/bin/sh
# cron jobs always start in your home directory,
# but you want to put an absolute path here
# to run this in the directory where you want rsync to copy these files
cd somewhere
for dir in code/themes/one \
code/themes/other \
code/base
do
rsync -avh "$dir"/* ./
done
for dir in code/themes/one \
code/themes/other \
code/base
do
while true; do
find "$dir"/src -name '*.html' -o -name '*.ts' -o -name '*.scss' |
entr -d rsync -avh "$dir"/* ./
done & # <-- run each loop as a background job
done
用于代码/主题/one中的目录\
代码/主题/其他\
代码/基
做
虽然真实;做
查找“$dir”/src-name'*.html'-o-name'*.ts'-o-name'*.scss'|
entr-d rsync-avh“$dir”/*/
完成entr
显然是您被引导到我不熟悉的entr
的地方,但如果我读对了,它会运行第一个find
和entr
管道,然后运行第二个find
并坐在那里等待列出的任何文件发生更改,您应该在$THEME
周围使用双引号,或者实际使用小写变量名(大写保留供系统使用)。即使您从未期望参数包含shell元字符,如果您尝试传入需要引号的参数,当您在不使用引号的情况下使用该参数时,错误消息也会令人困惑。进一步查看@tripleee是的,在您提到与我的另一个问题类似的问题后,我正在与entr玩。现在,使用基于路径的不同命令在多个查找中面临此问题。感谢您对语法约定的建议。我会实施的。也许你应该更详细地解释你希望发生什么。是否有理由按顺序执行这些操作?如果您有两个,而循环彼此独立运行会怎么样?我用entr实现了解决方案,它正在工作!非常感谢。我将研究你提到的一些东西,并在我玩过之后应用它们。再次感谢。