skip to Main Content

I am trying to install a WordPress caching plugin on all sites that do not currently contain a caching plugin. I am looping through all cPanel users but getting some odd results while running it. One liner is:

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

It is working for most but getting random hits like:

/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.

You can see something is off as its referencing the path for userC yet chown is erroring about userA

It also states there is no WP install but if I manually cd to that directory, I can run the wp-cli command and install the plugin without issue.

Another error:

/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.

Any help will be greatly appreciated

2

Answers


  1. Chosen as BEST ANSWER

    I ended up going a different way with this as I couldn't get it working properly even with it being rewritten by @alvits but it did certaintly help so I wanted to say thank you.

    I ended up just gathering some stats from several servers to put a list of caching plugins together and came up with:

    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
    

  2. The error -bash: [: /home/userB/public_html/wp/wp-content/plugins/: binary operator expected means that the pathname contains a whitespace.

    First and foremost, don’t parse the output of ls. Use glob instead.

    Second, always quote variable names to avoid word splitting and unintentional globbing. This is the root cause of your issue.

    Example:

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

    Your new script should look like:

    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
    

    As @Charles Duffy has mentioned in the comment below, this find "/home/${i##*/}/public_html/" -type f -name wp-config.php could yield multiple result. Use a for loop to work on each of them.

    This line [ -d "$WPPLUGINPATH"*cache* ] will only work if it expands to a single pathname. Use bash builtin compgen instead. compgen will generate filename completion based on the pattern "${WPPLUGINPATH}*cache*". It will return true if patterns can be generated or false when there’s no file with the pattern.

    Last but not least, change your habit of using all-caps variables to avoid inadvertently overwriting an environment variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search