2012-12-04 94 views
1

我有多個包含網站的git存儲庫。我想針對克隆的本地版本運行inotifywait以監視某些文件事件,並在檢測到這些事件時自動運行git push和git pull腳本。使用inotifywait監視多個目錄並運行腳本

到目前爲止,我已經創建了每個目錄的獨立功能的腳本,但只有第一個函數被調用。

#!/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 [email protected] 'cd /path/to/web/root; git pull'; 
    done; 
    } 
    dbg; 

    website2() { 
    same thing as above 
    } 
    website2; 

    website3() { 
    same thing as above 
    } 
    website3; 

    ....... 

    website10() { 
    ........ 
    } 
    website10; 

如何構建這一代碼更高效,更重要,完全可操作,而無需創建和管理10個單獨的腳本。 我真的很想將這個邏輯放在一個文件中,我希望這是一個模塊,以實現更大的項目。

請批評我的追問下,我的語法,思維過程等等等等,所以,我可以改進。 謝謝。

回答

1

如果你的邏輯是一樣的,你可以使用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 [email protected] '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 &