Handy Scripts and Programs

Automatic Pocket-Drive Backups :: Automatic Digital Camera Unloader :: UMN Resnet Auto-Login Script :: Gnome Deskbar Google Search Plugin :: Gnuplot Script

You might also be interested in my Lightsaber Rotoscoping Scripts.


Automatic Pocket-Drive Backups

This script will automatically mount, copy, and archive the contents of any device, such as a USB pocket drive. Make sure that the device is in the fstab, and mountable by regular users. If the script needed to mount the device, it will unmount it when it is finished. It creates a directory from today's date in the specified directory, where it copies all the files, then archives the entire folder, preserving permissions and directory structure, to a gzipped tar archive. It then cleans up the directory, leaving only the archive file. For my use, I created a directory called 'pocket_drive_backups' in my home dir, and put this script in it. The important variables are at the top of the script so you can easily modify this script yourself.

#!/bin/bash
# Script to automatically back-up a pocket drive
# Created a folder based on today's date, then tar and gzip the folder
# to a file of the same name .tar.gz
#
# Written by Matthew Beckler ( matthew @ mbeckler . org )

MOUNTPOINT='/mnt/usb'
BASE="/home/matthew/pocket_drive_backups/"
FOLDER="$(date +%Y%m%d)"
DIRECTORY="$BASE$FOLDER"
ARCHIVE="$FOLDER.tar.gz"
HADTOMOUNT=0

echo "Checking if device is mounted"
mount | grep -i $MOUNTPOINT &> /dev/null
if [ $? -eq 0 ]
then
        echo "Device is already mounted"
else
        echo "Mounting device . . ."
        mount $MOUNTPOINT &> /dev/null
        if [ $? -ne 0 ]
        then
                echo "Could not mount device"
                exit 1
        fi
        HADTOMOUNT=1
fi

echo "Checking if directory exists"
if [ -e $DIRECTORY ]
then
        echo "The directory '$DIRECTORY' already exists"
        exit 1
else
        echo "Creating directory: '$DIRECTORY' . . ."
        mkdir $DIRECTORY
fi

cd $BASE

echo "Copying files . . ."
cp -av $MOUNTPOINT/* $DIRECTORY

echo "Creating archive '$ARCHIVE'. . ."
tar -cvzpf $ARCHIVE $FOLDER

echo "Removing directory . . ."
rm -rf $DIRECTORY

if [ $HADTOMOUNT -eq 1 ]
then
        echo "Unmounting device . . ."
        umount $MOUNTPOINT
        if [ $? -ne 0 ]
        then
                echo "Could not unmount device"
        fi
fi

exit 0

Automatic Digital Camera Unloader

This script will automatically unload your digital camera's pictures. It is very similar to the Automatic Pocket-Drive Backup script above, but it merely creates a dated directory and copies over the pictures. You will have to customize the top three variables (MOUNTPOINT, SUBFOLDER, and PICTURESDIR) which will be different for everybody's computer and camera.

#!/bin/bash
# Script to automatically unload (without removing) pictures from a camera.
# Created a folder based on today's date, and copy all files over.
#
# Written by Matthew Beckler ( matthew @ mbeckler . org )
MOUNTPOINT="/mnt/usb"
SUBFOLDER="$MOUNTPOINT/dcim/101msdcf/"
PICTURESDIR="/home/matthew/pictures/$(date +%Y%m%d)"

HADTOMOUNT=0

echo "Checking if device is mounted"
mount | grep -i $MOUNTPOINT &> /dev/null
if [ $? -eq 0 ]
then
	echo "Device is already mounted"
else
	echo "Mounting device . . ."
	mount $MOUNTPOINT &> /dev/null
	if [ $? -ne 0 ]
	then
		echo "Could not mount device"
		exit 1
	fi
	HADTOMOUNT=1
fi

echo "Checking if source directory exists"
if [ ! -e $SUBFOLDER ]
then
	echo "The source directory $SUBFOLDER does not exist."
	echo "Are you sure this is a digital camera?"
	exit 1
fi

echo "Checking if destination directory exists"
if [ -e $PICTURESDIR ]
then
	echo "The directory '$PICTURESDIR' already exists"
	exit 1
else
	echo "Creating directory: '$PICTURESDIR' . . ."
	mkdir $PICTURESDIR
fi

echo "Copying pictures . . ."
cp -av $SUBFOLDER* $PICTURESDIR

echo "Finished copying pictures"

if [ $HADTOMOUNT -eq 1 ]
then
	echo "Unmounting device . . ."
	umount $MOUNTPOINT
	if [ $? -ne 0 ]
	then
		echo "Could not unmount device"
	fi
fi
exit 0

UMN Resnet Auto-Login Script

The University of Minnesota has a pretty bad authentication system. For Linux and Mac users, who experience frequent boots from the network, the only official response is to clear your dhcp cache, and reboot. This script will automatically authenticate with the UMN Resnet system. You will need to customize the USERNAME and PASSWORD variable. You might have to change COOKIEJAR to something that works for your machine. All output has been eliminated, so you can use this script in your crontab either every minute or every hour.

#!/bin/bash
# Script to auto-login to UMN Resnet
#
# Written by Matthew Beckler ( matthew @ mbeckler . org )

USERNAME=beck0778
PASSWORD=password
COOKIEJAR='/tmp/resnet-cookie'

wget http://www.google.com/ -O /dev/null &> /dev/null
if [[ $? = 1 ]]; then
    #could not access google.com - time to login
    curl https://resnet.netsec.umn.edu/fall2005/?original=http://www.google.com/ -F username=$USERNAME -F password=$PASSWORD -c $COOKIEJAR -L -o /dev/null &> /dev/null
    #waiting 30-120 seconds - you might want to adjust this
    sleep 30
    #finalize the login
    curl https://resnet.netsec.umn.edu/fall2005/ -b $COOKIEJAR -L -o /dev/null &> /dev/null
fi

exit 0

Gnome Deskbar Google Search Plugin

So I tried to get the Gnome Deskbar to work. It seems pretty cool, but none of the web search plugins seemed to be loading from my browser's settings, and google has ended their support for SOAP API keys, so the existing solutions don't really work. I modified some existing deskbar handlers to create a new action "Search Google for xxxxx". I don't really know python, and don't know where the deskbar API is located at, so this is quite a hack. If someone has already solved this problem, or knows how to clean up my code, please let me know.

USAGE: Put this script into ~/.gnome2/deskbar-applet/handlers/. You'll probably have to restart your deskbar to get it to load.

from gettext import gettext as _
import gtk
import gnomevfs
import deskbar
from deskbar.Handler import Handler
from deskbar.Match import Match

HANDLERS = {
        "GoogleSearchHandler" : {
        "name": _("Google Search"),
        "description": _("Searches Google..."),
        }
}

class GoogleSearchMatch(Match):
        def __init__(self, backend, term=None, **args):
                Match.__init__(self, backend, **args)
                self.term = term
        def get_category(self):
                return "actions"
        def action(self, text=None):
                gnomevfs.url_show("http://www.google.com/search?q=" + self.term)
        def get_verb(self):
                return _("Search Google for <b>" + self.term + "</b>")
        def get_hash(self, text=None):
                return self.name

class GoogleSearchHandler(Handler):
        def __init__(self):
                deskbar.Handler.Handler.__init__(self, "google-search")
        def query(self, query):
                return [ GoogleSearchMatch(self, name=query, term=query) ]

Gnuplot Script

Gnuplot is a pretty impressive open-source data plotting package. It's much easier to work with it if you create an external script to run, instead of having to type in commands each time. Here is an example script file that I keep around as a template for my lab reports. Since I'm using these images in a Latex document, I export them as Encapsulated PostScript (EPS), so you might want to change the file format to suit your needs. You can also use log axes, by using the set logscale x command, which also works for the y-axis. A quick note regarding the data csv file. Make sure to have spaces after the commas. The 1:5 business in the last line indicates which columns to use for the x and y data values.

set autoscale
unset log
unset label
#set logscale x #uncomment this to make a log plot
set xtic auto
set ytic auto
set title "Chart Title"
set xlabel "Independent Variable (Units)"
set ylabel "Dependent Variable (Units)"
set term post eps
set out "output_filename.eps"
plot "data.csv" using 1:5 notitle

You could save this script as, say, plot.script, and generate the plot using gnuplot plot.script.


Back to Matthew Beckler's home page

This page was last updated March 2, 2007