在bash脚本使用循环变量来传递不同的命令行参数 [英] Using a loop variable in a bash script to pass different command-line args

查看:576
本文介绍了在bash脚本使用循环变量来传递不同的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++程序,而我通过两个双打作为使用命令行输入

  INT主(INT ARGC,CHAR *的argv []){
    双A,B;
    A = ATOF(的argv [1]);
    B = ATOF(的argv [2]);
    进一步code .....

我在运行使用的qsub 实用集群上的code和我有一个名为bash脚本'jobsub.sh`提交看起来像这样的作业

 #!/ bin中/ CSH -f
主机名
CD /家庭/罗伊/ codeS / 3D#更改目录第一 - 取代Mysubdir
设置STARTDIR =`pwd`#记得我们在目录
如果(!-d /刮/ $ USER),那么
    的mkdir /刮/ $ USER#创建临时目录
如果它不存在ENDIF#
用#cp infile12 /刮/ $ USER#输入文件拷贝到临时目录
CD /刮/ $ USER#切换到临时目录
#rm *。*
$ HOME / codeS / 3D / autoA100.out 2.1 2.2#运行程序
CP * $#STARTDIR复制outputfiles回到我们开始的地方

在终端我的qsub jobsub.sh

不过,我想运行在不同的内核并行 A B 的不同值相同的可执行文件。是否有可能在bash脚本编写一个for循环,这样我可以做
类似,

 对于i = 1; I< = 10;我+ + {
   $ HOME / codeS / 3D / autoA100.out 2 + I * 0.1 2 +我* 0.2
}


解决方案

如果您提交的执行脚本批量加载,那么有没有办法有一个简单的循环也执行像你想要的,因为的整个脚本的是上​​运行的的每个节点的。然而,大多数批次装载机向用户提供环境变量

PBS,例如,具有 $ PBS_ARRAYID ,它指定因为它是运行作业的唯一ID。因此,而不是使用一个循环,你的脚本可以有:

  A = $(回声2 +($ PBS_ARRAYID + 1)* 0.1| BC -l)
B = $(回声2 +($ PBS_ARRAYID + 1)* 0.2| BC -l)
$ HOME / codeS / 3D / autoA100.out $ A $ B

注意我已经添加 1 $ PBS_ARRAYID 上面,因为ID从开始0 ,而你的循环从 1 开始。另外值得一提的是,虽然庆典可本地做一些运算,它不能处理实数;这就是为什么我不得不引用 BC

I have a c++ program to which I pass two doubles as inputs from the command line using

int main(int argc, char *argv[]){
    double a,b;
    a = atof(argv[1]);
    b = atof(argv[2]);
    further code.....

I run the code on a cluster using the qsub utility and I have a bash script named 'jobsub.sh` to submit the jobs which looks like this

#!/bin/csh -f    
hostname                           
cd /home/roy/codes/3D             # change directory first -- replace Mysubdir
set startdir = `pwd`               # remember the directory we're in
if( ! -d /scratch/$USER ) then
    mkdir /scratch/$USER       # create scratch directory
endif                              # if it does not exist
#cp infile12 /scratch/$USER     # copy input file to scratch directory
cd /scratch/$USER                  # change to scratch directory
#rm *.*
$HOME/codes/3D/autoA100.out 2.1 2.2          # run a program
cp * $startdir         # copy outputfiles back to where we started

At the terminal I do qsub jobsub.sh

However I want to run the same executable for different values of a and b in parallel on different cores. Is it possible to write a for loop in the bash script so that I can do something like,

for i=1;i<=10;i++ {
   $HOME/codes/3D/autoA100.out 2+i*0.1 2+i*0.2
}

解决方案

If you are submitting the execution script to a batch loader, then there is no way to have a simple loop do the execution like you want because the entire script is run on each node. However, most batch loaders provide environment variables to the user.

PBS, for example, has $PBS_ARRAYID, which specifies the unique ID for the job as it is running. So instead of using a loop, your script can have:

a=$(echo "2+($PBS_ARRAYID+1)*0.1" | bc -l)
b=$(echo "2+($PBS_ARRAYID+1)*0.2" | bc -l)
$HOME/codes/3D/autoA100.out $a $b

Notice I've added 1 to $PBS_ARRAYID above because the ID begins from 0 while your loop begins from 1. It's also worth mentioning that while bash can do some arithmetic natively, it cannot handle real numbers; that's why I had to invoke bc.

这篇关于在bash脚本使用循环变量来传递不同的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆