how can I parametrize a shell script that is executed on a grid (started with qsub) ? I have a shell script, where I use getopts to read the parameters.
When I start (qsub script.sh -r firstparam -s secondparam ..) this working script with qsub I receive error messages,
qsub: invalid option -- s
qsub: illegal -r value
as qsub thinks the parameter are for itself. Yet I have not found any solution.
Thanks
I just figured out how to solve it: just print the commands of the shell scrip with echo and pipe the result to qsub:
echo "./script.sh var1=13 var2=24" | qsub
There is a better way...
I'm really surprised at how long this question has gone without a good answer. It may be that the specific version of
qsub
wasn't specified.qsub
exists in at least Torque and also Sun Grid Engine, maybe other schedulers. So, it's important to know which you're using. I'll talk about a few here:TORQUE:
qsub -F <arguments> command
man page
Here's an example of how I normally use it. Starting with this example script which just echoes any arguments passed to it:
I would submit the job like this:
And this is what the output file looks like after it runs:
Sun Grid Engine:
qsub command [ command_args ]
man page
You just add the arguments after the command, same as you would when executing in the shell. I don't have SGE running anywhere, so no example for this one. But it's the same with Slurm, which is below
Slurm:
sbatch command [ command_args ]
man page
Here I submit the same script I used with the Torque example above:
And the results:
Exporting environment variables != passing arguments
Exporting environment variables is very different from passing arguments to a command.
Here is a good discussion on the differences.
The
qsub
answers above all recommend-v
. To be clear,-v
exports environment variables,-F
passes arguments to the command.I generally prefer to parameterize my scripts by allowing for arguments. In fact, I would say it's much more common to use scripts like this
process_data.sh --threads 8
than doing something likeexport THREADS=8; process_data.sh
.In addition to volk's answer, in order to reference the variables in the list (designated by -v) you simply use the name you define in your call. So, say you made a call to qsub as follows
qsub -v foo='qux' myRunScript.sh
Then
myRunScript.sh
could look something like this:Where the output would be
Hope this helps!
Using the qsub -v option is the proper way:
par_name can be used as variable in the shell script.