2017-04-17 93 views
1

我想在當前沒有包含緩存插件的所有網站上安裝WordPress緩存插件。我正在循環所有cPanel用戶,但在運行時會得到一些奇怪的結果。一個內膽是:bash循環沒有給出預期的結果

for i in $(ls -I . -I .. /var/cpanel/users) ; do 
    WPPATH=$(find /home/$i/public_html/ -type f -name wp-config.php) 
    WPPLUGINPATH="$(echo "${WPPATH//wp-config.php/wp-content/plugins/}")" 
    cd $WPPLUGINPATH 
    [ -d $WPPLUGINPATH*cache* ] || \ 
    wp --allow-root plugin install cache-enabler --skip-plugins --skip-themes 
    sleep 3 
    chown -R $i: $WPPLUGINPATH 
    echo $WPPLUGINPATH 
done 

它正在爲最,但得到隨機點擊,如:

/home/userC/public_html/wp-content/plugins/ 
Error: This does not seem to be a WordPress install. 
Pass --path=`path/to/wordpress` or run `wp core download`. 
chown: missing operand after ‘userA:’ 
Try 'chown --help' for more information. 

你可以看到的東西是關閉其引用爲用戶C尚未chown將此路徑示數約用戶A

它也說明沒有WP安裝,但如果我手動cd到該目錄,我可以運行wp-cli命令並安裝插件而不會出現問題。

另一個錯誤:

/home/userA/public_html/wp-content/plugins/ 
-bash: [: /home/userB/public_html/wp/wp-content/plugins/: binary operator expected 
Warning: cache-enabler: Plugin already installed. 
Success: Plugin already installed. 

任何幫助,將不勝感激

+0

你不應該使用'爲(LS ...'見[**擊PItfalls **](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) –

+0

也相關:[爲什麼你不應該解析'ls']的輸出(http:/ /mywiki.wooledge.org/ParsingLs)和[不要使用'for'讀取行]](http://mywiki.wooledge.org/DontReadLinesWithFor) –

+0

此外,使用'set -x'來觀察腳本的內容一般來說,在實踐中這樣做會幫助你提出更好的問題。 –

回答

0

我最終採取了與此不同的方式,因爲即使它被@alvits改寫,我也無法正常工作,但確實有幫助,所以我想說聲謝謝。

我結束了剛剛收集來自幾個服務器的一些統計把緩存插件列表,一起想出了:

for wppath in $(find /home/*/ \(-path mail -o -path virtfs -o -path cache \) -prune -o -type f -name wp-config.php) ; do 
     wppluginpath="${wppath//wp-config.php/}wp-content/plugins" 
     if [ -d "$wppluginpath"/cache-enabler ] || [ -d "$wppluginpath"/comet-cache-pro ] || [ -d "$wppluginpath"/hyper-cache ] || [ -d "$wppluginpath"/quick-cache ] || [ -d "$wppluginpath"/zencache ] || [ -d "$wppluginpath"/comet-cache ] || [ -d "$wppluginpath"/wp-fastest-cache ] || [ -d "$wppluginpath"/w3-total-cache ] || [ -d "$wppluginpath"/wp-super-cache ] ; then 
       echo "Found caching plugin in $wppluginpath" ; else 
       echo "No caching plugin found in $wppluginpath" 
       cd "$wppluginpath" || return 
       wp --allow-root plugin install cache-enabler --activate --skip-plugins --skip-themes 
       chown -R $(stat -c '%U' .): cache-enabler 
     fi 
done 
3

錯誤-bash: [: /home/userB/public_html/wp/wp-content/plugins/: binary operator expected意味着該路徑名包含一個空格。

首先,不要分析ls的輸出。改用glob。

其次,總是引用變量名稱以避免分詞和無意識的通配。這是你問題的根源。

例子:

[ -d /path/name space/to/something.conf ] 
-bash: [: /path/name: binary operator expected 

你的新的腳本應該是這樣的:

for i in /var/cpanel/users/*; do 
     WPPATH=$(find "/home/${i##*/}/public_html/" -type f -name wp-config.php) 
     WPPLUGINPATH="${WPPATH%/*}/wp-content/plugins/" # Useless use of echo 
     if pushd "$WPPLUGINPATH"; then 
       compgen -G "${WPPLUGINPATH}*cache*" > /dev/null || wp --allow-root plugin install cache-enabler --skip-plugins --skip-themes 
       sleep 3 
       chown -R "${i##*/}" "$WPPLUGINPATH" 
       echo "$WPPLUGINPATH" 
       popd 
     fi 
done 

正如@Charles Duffy在下面的評論已經提到的,這find "/home/${i##*/}/public_html/" -type f -name wp-config.php可能會產生多個結果。使用for循環來處理它們中的每一個。

此行[ -d "$WPPLUGINPATH"*cache* ]只有在展開爲單個路徑名時纔有效。改爲使用bash內部compgencompgen將根據模式"${WPPLUGINPATH}*cache*"生成文件名完成。如果可以生成模式,它將返回true;如果模式沒有文件,則返回false。

最後但並非最不重要的一點,改變使用全部大寫變量的習慣以避免無意中覆蓋環境變量。

+0

不錯,命中必要的高點。 –

+0

大量的錯誤仍然存​​在。如果有多個匹配其名稱中的wp-config.php的文件,則不會表現良好 - 應該對結果進行迭代或只取第一個。 '* cache *'不會擴展,因爲它在引號中。 –

+0

@CharlesDuffy - 報價錯誤是由我打字得太快。全部大寫的名字對我來說太懶惰了,但應該像你在這個評論中所說的那樣提到。我會在有空的時候做一個快速編輯。 – alvits

相關問題