很难回答这个问题。欢迎提出建议。你知道吗
我的总体目标是一次读入一行文件,并使用这些参数运行python程序,然后转到下一行。你知道吗
例如,假设我的程序是myprog.py公司它需要3个输入参数。如果我单独使用命令行,我会这样做:
chmod +x myprog.py
./myprog.py arg1 arg2 arg3
但是我想为arg1、arg2和arg3提供许多可能的值。所以我在readparas.txt文件地址:
arg1_1 arg2_1 arg3_1
arg1_2 arg2_2 arg3_2
arg1_3 arg2_3 arg3_3
...
我想读第一行并运行myprog。然后到第二行运行myprog,等等
问题是有些论点是一致的。例如,arg2和arg3可能是单个参数的不同设置。所以arg2和arg3需要作为一个参数来处理。你知道吗
为了使事情复杂化,我想使用bash脚本来实现这一点。以下主要是从谢尔盖·马什琴科(Sergey Mashchenko)在SharcNet的作品:
#!/bin/bash
i = 0
while read arg1 arg2 arg3
do
i = $(($i+1))
mkdir RUN$i
cd RUN$i
./myprog.py $arg1 $arg2 $arg3 #need groups of args here
cd ..
done < readparas.txt
不过,我有三个以上的论点,需要把它们结合起来。例如,假设我有8个参数。我想把参数2到4和参数6到8“分组”。我该怎么做?你知道吗
编辑
根据@chepner的建议,我尝试了以下方法(是的,实际上有19个参数,我想通过j连接f,通过s连接k)。你知道吗
#!/usr/bin/bash #with thanks to rici for pointing this out
i=0
while read a b c d e f g h i j k l m n o p q r s
do
i = $(($i+1))
mkdir RUN$i
cd RUN$i
./my_prog "$a" "$b" "$c" "$d" "$e" "$f$g$h$i$j" "$k$l$m$n$o$p$q$r$s"
cd ..
done < readparas.txt
它给了我这个错误:
-bash: ./epi_cmd.sh: usr/bin/bash: bad interpreter: No such file or directory
@redxef的评论建议也是如此,所以我不确定其中一个是否正确,或者我是否把其他事情搞砸了。我知道myprog.py公司当我通过命令行运行它时运行(没有bash,如我在顶部的示例中所示)。bash中一定有错误?你知道吗
编辑2
好的,我修复了bash错误,因为我意识到我确实有一个文件没有被正确读取,并且意识到我定义了两次“I”。你知道吗
这将运行,但返回另一个错误:
#!/usr/bin/bash
i=0
while read a1 b1 c1 d1 e1 f1 g1 h1 i1 j1 k1 l1 m1 n1 o1 p1 q1 r1 s1
do
i = $(($i+1))
mkdir RUN$i
cd RUN$i
../myprog.py $a1 $b1 $c1 $d1 $e1 $f1\ $g1\ $h1\ $i1\ $j1 $k1\ $m1\ $n1\ $o1\ $p1\ $q1\ $r1\ $s1
cd ..
done < readparas.txt
错误消息是:
./epi_cmd.sh: line 6: i: command not found
然后说所有的论点都不被认可。你知道吗
我知道有人要求输出,但这不适合这里。我正在编写一个.json文件和两个.txt文件myprog.py公司,和myprog.py公司它本身大约有200行长。。。。你知道吗
如果我理解正确的话,这没那么棘手。你知道吗
或不带阵列
最后我不得不做一些list/string的hacks来让类型与我程序的其他部分所期望的匹配。我也没有意识到我仍然需要匹配参数的名称(我认为顺序会处理这个问题),所以我在这里为任何其他被卡在这一部分的人添加了标志。你知道吗
完整的bash脚本是:
以及myprog.py公司看起来像这样:
相关问题 更多 >
编程相关推荐