Bash 使用inotifywait监视多个目录并运行脚本
我有多个包含网站的git存储库。我想对其克隆的本地版本运行inotifywait,以便监视某些文件事件,并在检测到这些事件时自动运行git push和git pull脚本 到目前为止,我已经为每个目录创建了一个带有单独函数的脚本,但只调用了第一个函数Bash 使用inotifywait监视多个目录并运行脚本,bash,inotify,inotifywait,Bash,Inotify,Inotifywait,我有多个包含网站的git存储库。我想对其克隆的本地版本运行inotifywait,以便监视某些文件事件,并在检测到这些事件时自动运行git push和git pull脚本 到目前为止,我已经为每个目录创建了一个带有单独函数的脚本,但只调用了第一个函数 #!/usr/bin/env bash dbg() { inotifywait -mr -e ATTRIB /path/to/dbg/ | while read dir ev file; do cd /path/to/db
#!/usr/bin/env bash
dbg() {
inotifywait -mr -e ATTRIB /path/to/dbg/ |
while read dir ev file;
do
cd /path/to/dbg
git pull;
git add .;
git commit -m " something regarding this website has changed. check .... for more info";
git push;
ssh remote@server.com 'cd /path/to/web/root; git pull';
done;
}
dbg;
website2() {
same thing as above
}
website2;
website3() {
same thing as above
}
website3;
.......
website10() {
........
}
website10;
我如何构造这段代码,使其更高效、更重要、完全可操作,而不必创建和管理10个单独的脚本。
我真的很想把这个逻辑放在一个文件中,我想把它作为一个模块,用于一个更大的项目
请评论我的提问、语法、思考过程等,以便我能提高。
谢谢。如果逻辑相同,可以使用bash函数避免复制。此外,提交消息也可以作为参数传递。 试试这个
#!/usr/bin/env bash
dbg() {
dbg_dir=$1
webroot_dir=$2
inotifywait -mr -e ATTRIB $dbg_dir |
while read dir ev file;
do
cd /path/to/dbg
git pull;
git add .;
git commit -m " something regarding this website has changed. check .... for more info";
git push;
ssh remote@server.com 'cd $webroot_dir; git pull';
done;
}
dbg /path/to/dbg /path/to/webroot1 & # & will run the command in background
dbg /path/to/dbg2 /path/to/webroot2 &
dbg /path/to/dbg3 /path/to/webroot3 &