0

These are my very first steps with bash scripts and I have an issue.

I want to list all plugins and themes in the directory of the website, which have available updates:

1 #!/bin/bash
 2 
 3 #check for updates of plugins, themes and core version in a WordPress installations
 4 
 5 str1="available"
 6 str2="none"
 7 
 8 wp plugin list | grep available | awk '{print $1, $2, $3, $4}'
 9 
10 if [ "$str1" == "$str2" ]; then
11     echo "Updates available"
12     else
13         echo "No updates available"
14         fi
15 
16 
17 wp theme list | grep available | awk '{print $1, $2, $3, $4}'
18 
19 if [ "$str1" == "$str2" ]; then
20 11     echo "Updates available"
21 12     else
22 13         echo "No updates available"
23 14         fi

However, if I execute the script, the following output appears:

No updates available updates_check: line 26: syntax error: unexpected end of file

I've tried to find a solution over the web. Can you please share how the script should be ended? Thank you!

0

WP-CLI and AWK

One way is to do the counting of the available string in the third column via AWK:

wp plugin list | \
 awk 'BEGIN{N=0;} \
  ($3=="available"){N++; print $0;} \
  END{print(N>0)?"Plugin updates available: "N:"No plugin updates available";}'

where N is the number of available updates, in case you need the number, $0 is the whole row and $3 the third column.

Example output:

Foo-plugin    active  available       1.2.3
Plugin updates available: 1

Similar for themes.

WP-CLI and PHP/WordPress API

Another way is to do this via a PHP script, that includes the relevant WordPress API calls and run it like:

wp eval-file script.php

or as a custom WP-CLI command.

See e.g. my answer here and here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.