Question

Is it possible to expand variables in comments inside a bash script?

I want to write a script to feed into SGE. The qsub syntax allows me to pass additional parameters to the grid engine using lines inside the bash script which begin with #$. For example,

#$ -q all.q
#$ -S /bin/bash
#$ -V
#$ -m beas
#$ -o run_20120103.out

What I want is that the -o parameter is dynamically set to a variable, say $1. So I would naively write

#$ -o run_${1}.out

However, since the line starts with a #, bash ignores it and the variable $1 is not expanded.

Any ideas? Some bash preprocessor? Some other way?

EDIT I just chose $1 as an example. It could just as well be $FOO or $BAR.

Was it helpful?

Solution

Variable expansion happens in shell memory, it doesn't affect the file. Therefore, it doesn't matter what bash expands.

Instead, you can probably generate the script to be run on the fly, with everything expanded in place:

cat << EOF | qsub [options] -
#$ -o run_$1.out
cmds
EOF

OTHER TIPS

To be a bit more SGE specific than the accepted answer, just redirect the standard output of qsub and parse the job id. You can then use this to alter the job name via qalter.

Here's a sample of a script I used to submit and modify jobs like this.

 for a in $args;
 do 
    qsub submission_script $a 1> job.txt
    jid=$(cat job.txt | tr -dc '0-9' | cut -c -6)
    qalter $jid -N $a
 done

I'm certain that there are more idiomatic ways to do this, but this is a first go.

This approach allows submission_script to have it's own arguments. It also allows you to programmatically alter other job characteristics, not just the name.

I am also using the qsub command in SGE to generate different output or error logs. For example, if I would like to include the TASK_ID in the script, instead of doing

#$ -o run_${TASK_ID}.out

Write it without the paretheses will do the trick:

#$ -o run_$TASK_ID.out
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top