Bash:如何连接特定数量的参数

2024-10-01 19:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

很难回答这个问题。欢迎提出建议。你知道吗

我的总体目标是一次读入一行文件,并使用这些参数运行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行长。。。。你知道吗


Tags: 文件runpytxtbash参数bin错误
2条回答

如果我理解正确的话,这没那么棘手。你知道吗

while read -r -a p; do
    args=(
      "${p[0]}"
      "${p[1]}${p[2]}${p[3]}"
      "${p[4]}"
      "${p[5]}${p[6]}${p[7]}"
    )

    ./myprog.py "${args[@]}"
done < readparas.txt

或不带阵列

while read -r p1 p2 p3 p4 p5 p6 p7 p8; do
    ./myprog.py "$p1" "$p2$p3$p4" "$p5" "$p6$p7$p8"
done < readparas.txt

最后我不得不做一些list/string的hacks来让类型与我程序的其他部分所期望的匹配。我也没有意识到我仍然需要匹配参数的名称(我认为顺序会处理这个问题),所以我在这里为任何其他被卡在这一部分的人添加了标志。你知道吗

完整的bash脚本是:

#/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
    cp $a1 RUN$i
    cd RUN$i
    ../myprog.py  filename $a1  reps $b1  pop $c1  susc $d1  pl_trans $e1  inf_period $f1\ $g1\ $h1\ $i1\ $j1  eps $k1\ $m1\ $n1\ $o1\ $p1\ $q1\ $r1\ $s1 
    cd ..
    done < readparas.txt

以及myprog.py公司看起来像这样:

if __name__ == "__main__":


#Thanks to http://stackoverflow.com/questions/7605631/passing-a-list-to-python-from-command-line Thomas answer

#At the top, import argparse
#This allows arguments to be parsed from the command line

    parser = argparse.ArgumentParser()
    parser.add_argument(' filename')
    parser.add_argument(' reps')
    parser.add_argument(' pop')
    parser.add_argument(' susc')
    parser.add_argument(' pl_trans')
    parser.add_argument(' inf_period', nargs='*')
    parser.add_argument(' eps', nargs='*')

    args = parser.parse_args()

    pop_file = args.filename
    susc = float(args.susc)
    pl_trans = float(args.pl_trans)
    i_period = args.inf_period[0].split() 
    #args.inf_period is a list; args.inf_period[0] a string
    inf_period = [int(i_period[i]) for i in range(len(i_period))]
    n_eps = args.eps[0].split()
    eps = [float(n_eps[i]) for i in range(len(n_eps))]
    reps = int(args.reps)
    pop = int(args.pop)

    returned_answer = function_in_body(susc, pl_trans, inf_period, eps, reps, pop)

相关问题 更多 >

    热门问题