Patch for Modules to use shell functions with BASH, not aliases

Whilst the Modules system is awesome in making life easy to maintain multiple versions of packages and their dependencies (and is heavily used in HPC centres like VLSCI) it can have some annoyances (and seems to be fairly half-heartedly maintained looking at the bugtracker on SourceForge). One thing that’s bitten us from time to time is that you can’t really use its “set-alias” functionality as the bash shell does not expand aliases in non-interactive shells and that includes jobs that are launched from an HPC queuing system like Torque, PBSPro, etc.

It does have the compile time option “--disable-shell-alias” but annoyingly the condition is only applied when your shell is “sh“, not “bash“, so I’ve ended up having to patch Modules to make this work for bash as well. This patch is against 3.2.9c:

--- utility.c.orig      2011-11-29 08:27:13.000000000 +1100
+++ utility.c   2012-05-16 15:08:34.012038000 +1000
@@ -1422,7 +1422,7 @@
         **  Shells supporting extended bourne shell syntax ....
         **/
        if( (!strcmp( shell_name, "sh") && bourne_alias)
-               ||  !strcmp( shell_name, "bash")
+               || ( !strcmp( shell_name, "bash") && bourne_alias )
                 ||  !strcmp( shell_name, "zsh" )
                 ||  !strcmp( shell_name, "ksh")) {
            /**
@@ -1471,7 +1471,7 @@
 
            fprintf( aliasfile, "'%c", alias_separator);
 
-        } else if( !strcmp( shell_name, "sh")
+        } else if( ( !strcmp( shell_name, "sh") || !strcmp( shell_name, "bash") )
                &&   bourne_funcs) {
        /**

Hopefully this patch will be of use to people..

Exclusive locks in Bourne shell scripts

Been writing some stuff in shell that needs to be able to ensure it doesn’t start itself twice, so a bit of Googling for exclusive locks in shell scripts brought me to this page on “advanced shell scripting” which had something that looked good.

Problem is that when you put it into a script you’ll find it doesn’t actually work because of some trivial coding issues.

So here’s the fixed version of that function..

# Function to do atomic locking.
# Original found at http://members.toast.net/art.ross/rute/node24.html
# Fixed by Chris Samuel http://www.csamuel.org/

function my_lockfile ()
 {
         TEMPFILE="$1.$$"
         LOCKFILE="$1.lock"
         ( echo $$ > $TEMPFILE ) >& /dev/null || (
                 echo "You don't have permission to access `dirname $TEMPFILE`"
                 return 1
         )
         ln $TEMPFILE $LOCKFILE >& /dev/null && {
                 rm -f $TEMPFILE
                 return 0
         }
         kill -0 `cat $LOCKFILE` >& /dev/null && {
                 rm -f $TEMPFILE
                 return 1
         }
         echo "Removing stale lock file"
         rm -f $LOCKFILE
         ln $TEMPFILE $LOCKFILE >& /dev/null && {
                 rm -f $TEMPFILE
                 return 0
         }
         rm -f $TEMPFILE
         return 1
 }