以 qsub 启动的 shell 脚本的参数
如何参数化在网格上执行的 shell 脚本(以 qsub 开头)?我有一个 shell 脚本,我使用 getopts 来读取参数。
当我使用 qsub 启动 (qsub script.sh -r firstparam -s secondaryparam ..) 这个工作脚本时,我收到错误消息,
qsub:无效选项 -- s
qsub:非法的 -r 值
因为 qsub 认为该参数是针对其自身的。但我还没有找到任何解决方案。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 qsub -v 选项是正确的方法:
par_name 可以用作 shell 脚本中的变量。
Using the qsub -v option is the proper way:
par_name can be used as variable in the shell script.
除了 volk 的答案之外,为了引用列表中的变量(由 -v 指定),您只需使用您在调用中定义的名称即可。因此,假设您按如下方式调用了 qsub
-v foo='qux' myRunScript.sh
那么
myRunScript.sh
可能如下所示:qsub 希望
这有帮助!
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!
我刚刚想出了如何解决它:只需使用 echo 打印 shell scrip 的命令并将结果通过管道传输到 qsub:
echo "./script.sh var1=13 var2=24" | qsub
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
有更好的方法......
我真的很惊讶这个问题已经持续了多久没有一个好的答案。可能是没有指定
qsub
的具体版本。qsub
至少存在于 Torque 和 Sun Grid Engine 中,也许还存在于其他调度程序中。因此,了解您正在使用哪个非常重要。我将在这里讨论一些:扭矩:
qsub -F;命令
手册页
这是我通常如何使用它的示例。从这个示例脚本开始,它只回显传递给它的任何参数:
我将像这样提交作业:
这就是运行后输出文件的样子:
Sun Grid Engine:
qsub command [command_args]
手册页
您只需在命令后面添加参数,就像在 shell 中执行时一样。我没有在任何地方运行 SGE,因此没有这方面的示例。但Slurm也是一样,在
Slurm下面:
sbatch command [ command_args ]
man页面
在这里,我提交了与上面的 Torque 示例相同的脚本:
结果:
导出环境变量 != 传递参数
导出环境变量与将参数传递给命令有很大不同。
这里对差异进行了很好的讨论。
qsub
答案首先推荐-v
。需要明确的是,-v
导出环境变量,-F
将参数传递给命令。我通常更喜欢通过允许参数来参数化我的脚本。事实上,我想说,使用这样的脚本
process_data.sh --threads 8
比使用export THREADS=8; 这样的脚本更常见。 process_data.sh
。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
.