Robust Bash Scripts - Part Two

This is part of a series of posts which should aid anyone writing Bash scripts to make them more robust.

This post shows you how to use Bash’s built-in error checking.

Sometimes you might write a script where one line of the script may fail, this failure could cause the rest of your script to fail horribly!

For example, you have two scripts: One which sets up the network on a machine, and another which sets up a naming service based on details given in the network script.  The second script calls the network setup script before it sets up the naming service.

If the network setup fails for some reason, the script shouldn’t attempt to set up the naming service as something has gone wrong.

You might try something like this:

bash setup_network.sh

if [ “$?”-ne 0]; then echo “command failed” exit 1 fi #start setting up naming service

Although this is fine, what happends if you forget to add it?  Or assume that the network will always be set up so decide not to add it?  If the network doesn’t get setup, you’ll be in a bit of a pickle!

Bash provides its own, built-in, error checking for you in the form of the “errexit” option, or “set -e”.  If a command fails in a script with this option enabled, the script will automatically stop with an error code 1.

If you need to turn off error checking (and you may need to if you are using something like grep which may fail, but you know you’ve handled it), then you could do this:

command1

command2 set +e command_that-may-fail set -e command4

“set +e” will turn off error checking.

In a later post I will look at the `trap` statement to catch the error this option throws.

Robust Bash Scripts - Part One

This is part of a series of posts which should aid anyone writing Bash scripts to make them more robust.

This post shows you how you can guard against unset variables.

Bash doesn’t provide any sort of checking for unset variables by default.  You may have something similar to the following piece of code: #!/bin/bash #Script is called delete.sh DEL=$1 rm -rf ~/$DEL You would use it similar to this: `bash delete.sh tmp`, which would delete ~/tmp.  Imagine if you forgot to add the “tmp” on the end…POOF!  That’s your home directory gone!

You should get into the habit of using the Bash option “nounset”, otherwise known as “set -u”.  If you put that at the top of your script and run the command without any arguments, you’ll get something like this:

$ bash delete.sh

delete.sh: line 3: $1: unbound variable

This is somewhat similar to a runtime error in Python or Perl and could be very annoying, but at least you’ve saved your home directory, or another critical location on your computer!

As a side note, if “set -u” or “set -o nounset” is specified at the top of the script and the script exists because of an error, it will exit with an exit status of 1.  In a later post I will explain how to use the `trap` statement to catch this error.

May the Fourth be with you!

Happy Star Wars day everybody!  May the Fourth be with you!

Exhibition in London

Faye and some others from the Aberystwyth university are putting on an exhibition in London between 2nd April and 16th April.  The exhibition is taking place in the Proud Central Gallery (32 John Adam Street - WC2N 6BP), just off the Strand and literally a minutes walk from Charing Cross.

The exhibition is the display of 19 artists work, all in the medium of photography.  It’s well worth a visit, especially as it’s FREE to get in!  Support the artists by buying a catalogue (a bargain at £4) and if you see a piece of work you like, please buy it!

Next generation Sun Ray hardware is out!

Oracle have uncovered a new Sun Ray (ultra) thin client recently.  They seem to have been quite quiet as I haven’t heard any news about it.

The client is called the Oracle Sun Ray 3 Plus and boasts the following features:

  • Low power consumption (typically around 14W)
  • 4 x USB 2 ports
  • Gigabit Ethernet
  • SFP (all Form Pluggable) fibre optic module for use with 1000 SX, 1000 LX, or 100 FX fibre optic connectors
  • 2 x DVI connectors
  • 2560 x 1600 resolution for a single display or 5120 x 1600 for two displays
  • Smart card reader One big jump from the older Sun Ray 2 hardware is the gigabit Ethernet, with it’s greater transfer speeds, this makes the four USB 2 ports possible.  Clients around the globe will be very happy with that.

The standard Sun Ray 2 supported a 1600 x 1200 resolution, even the Sun Ray 2FS client only supported 1920 x 1200 or 3840 x 1200 resolutions.  The Sun Ray 3 plus has a massively higher resolution with a single monitor supporting 2560 x 1600 or a dual monitor setup supporting 5120 x 1600 resolution thanks to the more powerful graphics card and the two DVI ports.

The fibre connectors are nothing new, they were present on the old Sun Ray 2FS and, of course, the standard smart card reader.

The only negative thing I have to say about this new hardware is that it takes up around 10W more than it’s predecessor.

I like the fact that Oracle have released this, it shows that they are committed (at least in the immediate future) to Sun Ray and the Sun Ray software.  I’d love to see one of these things, they should really help push Sun Rays into the workplace as a very viable alternative to those lumps of metal on everyones desks!

My Mac knows about the iPad

I was just browsing through the /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources folder on my MacBook, just to see what was there, and I suddenly spied the iPad icon:

I’m not sure what update brought this, over the past few weeks we’ve had the 10.6.3 update, though I suspect it was the recent iTunes 9.1 update, I’ll check with my Mac Pro back in Aber, I haven’t updated the iTunes on that in a couple of weeks…

Image is copyright Apple.

Weekend in London

I’ve spent my Easter weekend in London with Faye. She has a exhibition at the Proud Central Gallery (Open for the next two weeks!). I traveled from Aberystwyth to Faye’s home in Carmarthen on Friday, then from Carmarthen to Cardiff by car on Saturday, then Cardiff to London on the MegaBus. Saturday was the opening, and it went very well indeed!

I spent Sunday in London too, walking around shops and generally seeing the London sites. I still marvel at the London Underground, I think it is an awesome service, I love it! I’m also marvelling at the state of modern technology. I love that I can hook my phone up to my laptop for internet use, which is what I’m doing at the moment, whinging my way down some train tracks out of London Paddington while writing this blog post. I think its brilliant!

Anyhoo, off home to Melksham now. Will be seeing family and friends for the next week or so, as well as trying to tidy my dissertation code.

Dropbox

Ever wanted to store stuff online but couldn’t afford web hosting?  If so, Dropbox is your answer!

Dropbox offers both free and paid for storage online, the free storage has a limit of 2GB, whereas paid sotrage has a limits of 50GB and 100GB depending on how much you want to pay.

If you use the free option, you’ll start off with so much storage, and if you invite friends to join the sservice, Dropbox will add more storage to your account!

If you want to help me out, click here :)

Dropbox also integrates really nicely with Mac OS X, Linux and Windows, it feels just like another folder on your computer.  Drag and drop stuff into the folder (or cp/mv on the command line ;) ) and Dropbox will sync it to it’s online servers.  If you go to another computer that you’ve installed Dropbox onto, it’ll download any new content!  They also have a nice web interface incase you don’t have permission to install the software on your computer.  Lastly, Dropbox allows you to share files with other Dropbox users, which is really quite handy!

Whence and Whenceman

On the UNIX command line, the `which` command is great, it tells you where a command is in the system.  However, if your system has two versions of the `ls` command, it will only tell you which `ls` command you are going to use when tap it in and press enter.  To find all copies of any command, we need something Liam called `whence` (I inherited Liam’s bashrc file when working at Sun, and this little gem was right inside it).  The .bashrc function for `whence` looks something like this:

It’s nice and straighforward, nothing complicated about it at all.  In fact, to let everyone on your system use it, you could just stick “#!/bin/sh” at the top and stick in a file in /usr/bin !

Today, I wanted to find a man page, the sysidcfg man page to be precise.  Instead of doing the usual trick (`find / | grep sysidcfg”), I thought I’d modify `whence` to look for it for me, and seeing as it’s no longer `whence`, I called it `whenceman`:

As you can see, it’s very similar, and you could do the same thing by putting it in /usr/bin so everyone could use it.

Gruml

I get mildly annoyed when using applications that are hosted on the web.  For example, I’ll use web mail if I have to, but I’d much rather use Apple Mail or Thunderbird… Anyway, I’ve been using Google Reader for the past year or so with some satisfaction, but then my browser crashed and for some reason, all of my feeds are telling me I’ve read everything…

Anyhoo, I’ve found an answer, and I think it’s rather good.  It’s called Gruml.  It basically replicates Google Reader functionality using a local app.  It’s in beta at the moment and it does feel quite “beta-ish”, but I’m hoping for a Mail type solution where I can view my old feeds and entries offline if I want to.

Gruml has some nice features that should be really good when it matures:

  • Tweeting articles
  • Post articles to Twitter, Facebook, MySpace, Bebo and more
  • With the help of another blogging app (like MarsEdit) post articles to your blog
  • Post notes
  • Organise articles into folders