Skip to main content

Bash Script: Install Drupal/Wordpress Packages with getopts

Printer-friendly version

I run a Drupal and a WordPress site off of a single remote server hosted by Site5. To simplify the process of decompressing and installing modules (Drupal), plugins (WordPress) and themes (both), I wrote a simple bash script using the getopts utility to allow me to decompress and send packages in one line. I am excited about this utility because it marks the first time I've successfully used getopts.

#!/bin/bash
#########################################################################
# Automatically decompress and upload the specified plugin(s) to a
# server specified by its namesack variable. Different operations are
# performed based on whether the module is for drupal (-d) or WordPress
# (-w). The plugin can be an actual plugin (-p) or a theme(-t). This
# is significant because it is my first shell script to inculde
# command-line parameters. The decompresses the files to the tempdir
# variable (/tmp), and then uses scp to send them to the server.
# Presently runs from the same directory as the package.
# AUTHOR: Brian Napoletano
# DATE: 2008-05-07
# VERSION: 0.0.1
# USAGE: installmodule [-d] [-w] [-p ] [-t ]
#########################################################################

# Declare variables
server=user@server.name;

# Usage: scriptname -options
dflag=
wflag=
pflag=
tflag=
# Loop through the options
while getopts ':dwp:t:' OPTION
do
        case $OPTION in
        d)      dflag=1
                ;;
        w)      wflag=1
                ;;
        p)      pflag=1
                modname="$OPTARG"
                ;;
        t)      tflag=1
                modname="$OPTARG"
                ;;
        \:)     printf "argument missing from -%s option\n" $OPTARG
                printf "Usage: %s [-d] [-w] [-t module] [-p module]
                " $(basename $0)
                exit 2
                ;;
        \?)     printf "unknown option -%s\n" $OPTARG
                printf "Usage: %s [-d] [-w] [-t module] [-p module]
                " $(basename $0)
                exit 2
                ;;
        esac >&2
done

# This moves us to the remaining options specified somehow. There
# shouldn't be any remaining options in this case, though...
shift $(($OPTIND -1))

# If the module is intended for drupal:
if [ "$dflag" ]
then
        servepath=public_html/front/sites/all;
        # I'm doing this here because the compressions are different
        $DEBUG tar -xzf ${modname}*.tar.gz;
        if [ "$pflag" ]
        then
                endpath=/modules;
        else
                endpath=/themes;
        fi

# Or WordPress
elif [ "$wflag" ]
then
        servepath=public_html/napword/wp-content;
        # I'm doing this here because the compressions are different
        $DEBUG unzip ${modname}*.zip
        if [ "$pflag" ]
        then
                endpath=/plugins;
        else
                endpath=/themes;
        fi

# Or the user enter something weird
else
        printf "missing options\n"
        printf "Usage: %s [-d] [-w] [-t module] [-p module]
        " $(basename $0)
        exit 2
fi

$DEBUG scp -r ${modname} ${server}:${servepath}${endpath};
$DEBUG rm -rf ${modname};

The $DEBUG variable is a trick I learned from someone at Purdue's Rosen Center for Advanced Computing. If you call the script in the usual manner, bash will ignore $DEBUG. To see what the script would do if it were being run, preface the call with DEBUG=echo.