Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Check Drupal Module Status Using Bash

Parent Feed: 

When you run a lot of Drupal sites it can be annoying to keep track of all of the modules contained in a platform and ensure all of them are up to date. One option is to setup a dummy site setup with all the modules installed and email notifications enabled, this is OK, but then you need to make sure you enable the additional modules every time you add something to your platform.

I wanted to be able to check the status of all of the modules in a given platform using the command line. I started scratching the itch by writing a simple shell script to use the Drupal updates server to check for the status of all the modules. I kept on polishing it until I was happy with it, there are some bits of which are a little bit ugly, but that is mostly due to the limitations of bash. If I had to rewrite the it I would do it in PHP or some other language which understands arrays/lists and has http client and XML libraries.

The script supports excluding modules by using an extended grep regular expression pattern and nominating a major version of Drupal. When there is a version mismatch it will be shown in bolded red, while modules where the versions match will be shown in green. The script filters out all dev and alpha releases, after all the script is designed for checking production sites. Adding support for per module update servers should be pretty easy to do, but I don’t have modules to test this with.

To use the script, download it, save it somewhere handy, such as ~/bin/check-module-status.sh, make it executable (run chmod +x ~/bin/check-module-status.sh). Now it is ready for you to run it - ~/bin/check-module-status.sh /path/to/drupal and wait for the output.

#
# Check the latest version of a drupal module / template
#
# Written by Dave Hall http://davehall.com.au
# Copyright (c) 2010 Dave Hall Consulting http://davehall.com.au
# Licensed under the terms of the GPLv3 http://www.gnu.org/licenses/gpl.html
#

BASE_URL='http://updates.drupal.org/release-history/'

if [[ 0 -eq $# || 3 -lt $# ]]; then
  echo Usage: $(basename $0) /path/to/drupal-instance [[exclude-pattern] drupal-version]
  exit 1
fi

DRUPAL_PATH=$1

if [ ! -d "$DRUPAL_PATH" ]; then
  echo ERROR: Invalid path - $DRUPAL_PATH
  exit 2
fi

EXCLUDE_PATTERN='(drupal)'
if [ 2 -lt $# ]; then
  EXCLUDE_PATTERN=$2
fi

DRUPAL_VERSION=6.x
if [ 3 -eq $# ]; then
  DRUPAL_VERSION=$3
fi

INFO_FILES=$(find $DRUPAL_PATH -name *.info)

modules=( )
for info in $INFO_FILES; do
  project=$(sed -e '/project =/!d' -e 's/project = "\(.*\)"/\1/g' < $info | head -n1)
  if [[ "" == "$project" || 0 -eq $(echo $project | egrep -cv $EXCLUDE_PATTERN) ]]; then
    continue
  fi
  cur_ver=$(sed -e '/version =/!d' -e 's/version = "\(.*\)"/\1/g' < $info | tail -n1)
  modules=( "${modules[@]}" "$project|$cur_ver##" )
done

modules=$(echo ${modules[@]} | tr '##' '\n' | sort -u)

echo -e "module\t\tlocal\tcurrent"
for mod in $modules; do
  color='\e[0;32m'
  new_ver=$(wget -O - http://updates.drupal.org/release-history/`echo $mod | cut -d\| -f1`/$DRUPAL_VERSION 2> /dev/null | sed -e '/<version>.*<\/version>/!d' -e '/\(dev\|alpha\)/d' -e 's/.*<version>\(.*\)<\/version>/\1/g' | head -n1 );
  if [ "$(echo $mod | cut -d\| -f2)" != "$new_ver" ]; then
    color='\e[1;31m'
  fi
  echo -e $color$mod\|$new_ver | tr '|' '\t'
done
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web