Automatically Get Modules and Themes

Download and Install for Drupal

I wrote this script to automatically get and install both themes and modules for drupal. Whenever you want to install a theme or a module, you have to wget the tar file, unzip it, and move it into its proper directory, I wrote a script that does just that.

This is my first script using ‘getopts’ in bash, and so far I like it. I noticed getopts produces some errors if you just supply “-” as the command line argument, so I hard coded the program to exit if it detects that (if anyone knows a better way let me know!)

The script is called ‘drupalget’ and can be called with -t to download a theme, -m to download a module, or -h for help (usage).

The script can accept an unlimited number of URLs after the -t or -m switch, so you can install more than one theme/module at a time.

Examples

drupalget -t website.com/theme1.tar website.com/theme2.tar ...
drupalget -m website.com/module1.tar website.com/module2.tar ...

The Code

#!/bin/bash
#
# drupalget
# Dave Eddy
# dave@daveeddy.com
# http://www.daveeddy.com
#
# this script will automatically download drupal themes/modules supplide
# via the command line, extract them, and move them to their correct destination

# variables
BASEURL='/var/www/sites/all/'

usage="Usage: $(basename $0) -m <moduleURL> OR $(basename $0) -t <themeURL>"

# print usage if no arguments at all
[[ -z $1 ]] && echo $usage && exit 1

TYPE=null
# command line arguments
while getopts "hmt" options; do
        case "$options" in
                m ) TYPE=modules;;
                t ) TYPE=themes;;
                h ) echo $usage;  exit 0;;
                * ) echo $usage;  exit 1;;
        esac
done

shift $(($OPTIND-1))

# iterate through command line arugments to wget and unpack files
for url in $@; do
        # do all work in a tmp directory
        TEMP="$(mktemp -d)" || exit 1
        cd $TEMP
        wget $url
        FILENAME=$(basename $url)
        tar xvvf $FILENAME 2>/dev/null
        rm -f $FILENAME 2>/dev/null
        mv * "$BASEURL"/"$TYPE"/
        rm -rf $TEMP
done

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>