14 Jun 2005 ads   » (Journeyer)

Safe scripting

So, we all know that when writing a shell script, you start:

#! /bin/sh
#
# description/author/copyright/blah-blah-blah

set -e
....

What I didn't ever see mentioned is that if you're assuming set -e, command substitution — `cmd` or $(cmd) — with a non-zero exit status will not cause the script to abort.

There's a simple solution, fortunately:

tmp=`cmd`
do_stuff_with $tmp

Whilst I'm onto it, if you're reading a stream of words into the positional ($1 etc) values with set --, you really should check the number of parameters. Either:

tmp=`date +"%Y %m %d"`
set -- $tmp
[ $# = 3 ]

or

tmp=`date +"%Y %m %d"`
set -- $tmp
year=$1; month=$2; day=${3:?date went wrong}

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!