pcantalupo
pcantalupo

Reputation: 2226

immediately catching that not all environment variables have been defined on qsub command line

I'm using qsub to submit pbs jobs. My job file requires 5 environment variables be defined: qsub -v A=foo,B=bar,C=cat,D=dog,E=qux jobfile.sh. Sometimes, I might forget to define one of the variables on the command line when I submit only later to find out my error.

So, is there a way to catch the condition that not all environment variables are defined when submitting with qsub? Or am I going to have to accept that I have to be very careful when submitting job files with many required environment variables?

Thank you,

Paul

Upvotes: 2

Views: 126

Answers (2)

Digital Trauma
Digital Trauma

Reputation: 15996

I really like the brevity of @chepner's answer, but if you don't want to fiddle with toggling set -/+u, then you can use this shell parameter expansion in a similar manner:

: ${A?} ${B?} ${C?} ${D?} ${E?}

If any of the variables are not set, then you'll get an error like this and the script will quit immediately:

./jobfile.sh: line 3: A: parameter null or not set

Optionally, you may override the default error message with your own like this:

${A?Your message here}

From the manual:

${parameter:?word}

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

Upvotes: 3

chepner
chepner

Reputation: 531165

The best you can do is to add a runtime check to the beginning of jobfile.sh that will cause it to exit if any expected variables are not set.

set -u
: $A $B $C $D $E
set +u

set -u tells the shell to exit if an attempt is made to expand an unset parameter. The : command does nothing, but its arguments do undergo the standard expansions first, so if any of the five are unset, the shell will exit. set +u then disables the unset-parameter check for the remainder of the shell.

qsub itself cannot examine the given job to determine what variables it might need.

Upvotes: 3

Related Questions