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
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:
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:
Your new script should look like:
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 builtincompgen
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.