admin管理员组

文章数量:1122832

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!

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!

Share Improve this question asked Aug 20, 2020 at 9:10 zamunda68zamunda68 1
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: pluginsBash script to check available updates