30th July 2010

Archive for June, 2008

Handling temporary files in BASH shell scripts

Thursday, June 5th, 2008

Temporary files left over from shell scripts clutter up your /tmp directory and may result in information leakage. Below are a pair of functions we use to gracefully handle the creation and removal of temporary files in shell scripts.

The first function is used to create a temporary file:

function os_mktemp {
 [[ ! $1 ]] && echo "os_mktemp: required a handle name" && exit 99
 let OS_TEMPFILEHANDLE++;
 OS_TEMPFILE[$OS_TEMPFILEHANDLE]=`mktemp /tmp/ostmp.XXXXXXXX`
 eval F_$1=${OS_TEMPFILE[$OS_TEMPFILEHANDLE]}
}

It requires a single parameter, which is used to create a variable name containing the path to the temporary file. For example. os_mktemp FTPOUTPUT will return a variable $F_FTPOUTPUT.

The array OS_TEMPFILE is an array holding the names of all the temporary file names, this is used by the cleanup function to remove the temporary files.

os_mktemp OUTPUT; this results in a temporary file with a random name being created and the name being stored in the variable $F_OUTPUT.

The second function is used to remove all temporary files.


function os_cleanup {
 for FILE in ${OS_TEMPFILE[@]}; do
  [[ -e "$FILE" ]] && rm "$FILE" || echo "os_cleanup: couldn't remove temporary file $FILE."
 done
}

To ensure the os_cleanup code is executed whenever the shell script closes, we use the BASH trap command.


trap os_cleanup INT TERM EXIT

Automatically backing up files before making changes

Sunday, June 1st, 2008

It’s best practice (and common sense) to make a backup of a file before you edit it. Unfortunately it’s easy to forget to do this. We use this simple script below to make a time/date stamped copy of a file before launching the editor (in this case vim). We create it as /usr/local/bin/bvi.


#!/bin/bash
[[ -r $1 ]] && cp $1{,.`date +%Y%m%d-%H%M`} || echo "$1 is a new file"

vim $1

We then add the following two aliases to our ~/.bashrc file to make sure it’s run automatically when we call vim.

alias vi=/usr/local/bin/bvi
alias vim=/usr/local/bin/bvi

This isn’t a replacement for good version control of important files, but it’s a good safety net. It’s also worth noting that this can leave a lot of old copies of files laying about, so it’s work cleaning out old copies every now and again.