2017-04-17 89 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

相关问题