The BICsuite Run Program Part 3: Backticks

In the Bourne shell it is possible to assign a variable the output (stdout) of a program. Expressions like

A=`expr $A + 1`

used to be very common. (Since many environments now use the Bash instead of the Bourne shell, this kind
of expressions is gradually disappearing).

The good news is that the scheduling server knows about the backticks. It treats them as if they were double
quotes, but it doesn’t remove them. Parameter substitution is still performed and the argument doesn’t
break at a white space.

So let’s try and see how it works. First of all we’ll need something to execute, so let’s calculate the primes less than 100. A shell script could look like this:

A=2;
primes="";
while [ "$A" -lt 100 ]; do 
    f=0;
    for P in $primes; do
        if [ `expr $A % $P -eq 0` ]; then 
            f=1;
            break;
        fi;
     done;
     if [ $f - eq 0 ]; then 
         primes="$primes $A"; 
         echo $A;
     fi;
     A=`expr $A + 1`;
done

It’s easy to create a run program out of this, because no parameters from the scheduling system are used.
It’ll be sufficient to enclose the entire code within single quotes and to put a sh -c in front of it.

That doesn’t sound very challenging, so let’s make the upper bound (100) configurable by using a parameter
that is specified at submit time.

We start with a rough design of the run program that looks like this:

run program = sh -c ’
    A=2;
    primes="";
    while [ "$A" -lt $TESTPARM ]; do 
        f=0;
        for P in $primes; do
            if [ `expr $A % $P` -eq 0 ]; then 
                f=1;
                break;
            fi;
         done;
         if [ $f -eq 0 ]; then 
             primes="$primes $A"; echo $A;
         fi;
         A=`expr $A + 1`; 
    done
,

It is basically the same as the script, except that it has been enclosed in quotes and the value 100 has been
replaced by $TESTPARM. It is evident that the run program in this form won’t print a list of primes. Since the parameter TESTPARM only occurs within single quotes, it won’t be touched by the server.

We’ll have to terminate the single quoted area, start a double quoted one and use TESTPARM, and then terminate the double quoted area and start a single quoted area again. In other words:

run program = sh -c ’
    A=2;
    primes="";
    while [ "$A" -lt '"$TESTPARM"' ]; do 
        f=0;
        for P in $primes; do
            if [ `expr $A % $P` -eq 0 ]; then 
                f=1;
                break;
            fi;
         done;
         if [ $f -eq 0 ]; then 
             primes="$primes $A"; echo $A;
         fi;
         A=`expr $A + 1`; 
    done
,

Indeed, the output looks good:

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Now we didn’t use the backticks in a way that the scheduling server would see them. In order to create an
example that uses backticks visible to the scheduling server, we’ll multiply TESTPARM by 4 and use the result
as an upper bound. So let’s try:

run program = sh -c "
    M=`expr $TESTPARM \* 4`"’
    A=2;
    primes="";
    while [ "$A" -lt $M ]; do
        f=0;
        for P in $primes; do
            if [ `expr $A % $P` -eq 0 ]; then
                f=1;
                break;
            fi;
        done;
        if [ $f -eq 0 ]; then
            primes="$primes $A";
            echo $A;
        fi;
        A=`expr $A + 1`;
    done
 ’

Note the added double quotes just after the -c and which ends just after the backtick.


Part 1: Introduction and simple usage

Part 2: Advanced usage

Part 3: Backticks

Part 4: Other interpreters

Part 5: Circumventing limitations

Part 6: Security considerations and conclusion