Handling Shell Script Interrupts

A Real Screen Shot

A Real Screen Shot

If you do much shell scripting, then handling shell interrupts is something you should consider.  As a user is interacting with your script, they may decide to interrupt it by typing Ctrl-C, for example.  Typically this will interrupt your shell script execution, forcing it to exit.

Depending on what your shell script is doing, this could leave behind temporary files, or leave other files in a broken state.  It would be useful if you could trap the interrupt, and handle it safely, before exiting the script.
This can be achieved on most shells using the ‘trap’ command.  The trap command takes the following syntax:

trap [OPTIONS] [[ARG] SIGSPEC ... ]

The ARG is the command to be executed on signal delivery, while SIGSPEC is the name of the signal(s) to trap.  Options include -h for help, -l to list signal names, or -p to print all defined signal handlers.

For example, to always return a ‘user aborted’ error code, the following line in your script could be used.  Whatever value given to $exit_user_abort would be returned.

trap 'echo "`basename $0`: Ouch! User Aborted." 1>&2; exit $exit_user_abort' 1 2 15

The numbers 1, 2 and 15 at the end of this example define which interrupts we’re interested in trapping.  These numbers correspond to different kinds of interrupts.  A short list is given here, but you can use ‘trap -l’ for a complete list.

Signal Number Signal Name Explanation
0 EXIT exit command. Script has executed successfully.
1 HUP Hang Up. The user session has disconnected.
2 INT Interrupt.  Ctrl-C (or other shell interrupt signal) has been given.
3 QUIT Quit.  Ctrl-\ (or other shell quit signal) has been given.
6 ABRT Abort signal.
15 TERM Terminate.  Kill command has been issued against your script.

If your trap runs several commands, it’s possibly neater to call a shell function than list the commands in-line, as above.  For example:

trap funcname 1 2 15

funcname
# Function to handle interrupts
{
echo "`basename $0`: Ouch! User Aborted." 1>&2
exit $exit_user_abort
}

Pre-Merge Formatting

Merge Right

Merge Right

A problem with formatting code on a branch, is when you come to merge it back to the trunk, you end up having to work thorough lots of file changes which are really only formatting changes.  The larger your project size, the more work this involves.  I was faced with a similar task recently.

While editing file by file, I usually do the Ctrl-A Ctrl-Shift-F combo to ensure the file is formatted according to our corporate standards before saving any edits.  Since much of the code I touched on the branch was formatted under a different standard, and I touched a lot of files while introducing log4j logging, the end result was heading for a hairy merge.  What I really needed was a way to bulk format both the branch and the trunk versions of the code-base.

Luckily, I realised that that same formatter embedded in Eclipse can be used to bulk format Java source.  Here’s how …

Continue reading

Howto: Pause a Shell Script

Computer Data Output

Computer Data Output

File this one under “Another Nifty script-let”.  The problem this time is getting a shell script to pause for user input.  What I needed was a way to make a script stop, saying “Hit a key to continue..”, wait for the user to hit a key, then continue the script execution.  Here’s how:

Continue reading

Why Maven doesn’t work for me.


I’ve been meaning to do a follow up post on Maven for quite a while. I received several emails regarding my previous post from people asking how we’re using it. I must admit, it looked promising initially, but as we got into the specifics, it looked less workable for building commercial software. Here I hope to outline some of the reasons I suggest that Maven may not be for you.

Continue reading

A Brief History of XML

Evolution of XML

Extensible Markup Languages (XML) history begins with the development of Standardised Generalised Markup Language (SGML) by Charles Goldfarb, along with Ed Mosher and Ray Lorie in the 1970s while working at IBM (Anderson, 2004). SGML despite the name is not a mark-up language in it’s own right, but is a language used to specify mark-up languages. The purpose of SGML was to create vocabularies which could be used to mark up documents with structural tags. It was imagined at the time, that certain machine readable documents should remain machine readable for perhaps decades.

Continue reading

Transaction Management for JPox with Spring.


I was on the edge of writing some unkind things about JPox and Spring this week, as I dealt with the frustration of getting Spring to manage transactions for the JPox persistence code I was writing.

The issue I was dealing with, was the persistence of a org.springframework.orm.jdo.JdoUsageException. I decided to simplify, and I downloaded the JPox JDO Tutorial, and decided to reimplement it using Spring.

Read on for the cause, and solution to my problem. Continue reading

Java IDEs for x86_64

As I’ve mentioned before, I run Ubuntu on an AMD64 processor. I was recently surprised to see that running the new release of Eclipse (Europa) was causing me issues. Basically, Java (or the native parts of these projects) was throwing a wobbly when trying to run 32-bit code on a 64-bit JVM. I thought the goal of Java was write once, run anywhere!

It took me a while, but I managed to find a 64-bit install of Europa. You have to take the “Eclipse Classic” SDK version for Linux x86_64. This installed and ran using the default instructions provided (unzip and run).

Netbeans 6.0M9 gave me similar issues a while back. I never got around to fixing it, but I noticed yesterday that 6.0M10 has been released. This installed and ran straight away. I guess they’ve included x86_64 support in the installer now.

I got no excuses for not developing something useful now :-)

AddThis Social Bookmark Button

SQL Object Features

Object Features

SQL-1999 introduced object support into the SQL standard. The SQL-1999 standard had to be backward compatible with the existing SQL-1992 standard, so object support was implemented as an extension to the existing standard. The types defined by SQL-1992 were retained, and the standard modified to support user defined types (UDT) with object-like features.

Continue reading