简体   繁体   English

使用bash脚本将参数传递给python脚本

[英]use bash script to pass arguments to a python script

I have a very basic problem, but my knowledge about python is very limited. 我有一个非常基本的问题,但是我对python的了解非常有限。 I have a python script that takes several arguments in order to run ( https://github.com/raphael-group/hotnet2/blob/master/bin/createPPRMat.py ). 我有一个Python脚本,该脚本需要几个参数才能运行( https://github.com/raphael-group/hotnet2/blob/master/bin/createPPRMat.py )。

I would like to use a file with some filenames (one per line) as the first argument to be passed to the python script. 我想使用带有一些文件名的文件(每行一个)作为要传递给python脚本的第一个参数。

My first try to do it was creating a bash script ( mat.sh ) like this: 我的第一个尝试是创建一个bash脚本( mat.sh ),如下所示:

#!/bin/bash
for net in $(cat /home/hotnet2-1.0.0/iref/iref.list);
do
export
python createPPRMat.py -e `$net` -i /home/jfertaj/hotnet2-1.0.0/iref/iref_index_genes -o /home/jfertaj/Broad_Stay/hotnet2-1.0.0/iref_influence_matrices
done

However I got an error, the python script seems to not parse the $net variable: 但是我遇到了一个错误,python脚本似乎无法解析$net变量:

createPPRMat_1.py: error: argument -e/--edgelist_file: expected one argument
mat.sh: line 6: /home/jfertaj/Broad_Stay/hotnet2-1.0.0/iref/iref_edgelist_139: No such file or directory

When I double quote the variable net in the bash script ( "$net" ) the error I got is different, pointing out that something is wrong with the filename 当我在bash脚本( "$net" )中双引号变量net ,我得到的错误有所不同,指出文件名有问题

Traceback (most recent call last):
File "/home/jfertaj/Broad_Stay/hotnet2-1.0.0/bin/createPPRMat_1.py", line 96, in <module>
run(get_parser().parse_args(sys.argv[1:]))
File "/home/hotnet2-1.0.0/bin/createPPRMat_1.py", line 38, in run
edges = [map(int, l.rstrip().split()[:2]) for l in open(args.edgelist_file)]
IOError: [Errno 2] No such file or directory: '\x1b[01;00m/home/hotnet2-1.0.0/iref/iref_edgelist_164\x1b[0m'

The content of iref.list looks like this: iref.list的内容如下所示:

/home/hotnet2-1.0.0/iref/iref_edgelist_1
/home/hotnet2-1.0.0/iref/iref_edgelist_10
/home/hotnet2-1.0.0/iref/iref_edgelist_100

And the iref.list file was created using cat -1 ... < iref.list 然后使用cat -1 ... < iref.list创建了iref.list文件cat -1 ... < iref.list

Any help would be very appreciated 任何帮助将不胜感激

Thanks 谢谢

The python traceback shows you the problem (as you noticed). python追溯显示了问题(如您所注意到的)。

Traceback (most recent call last):
File "/home/jfertaj/Broad_Stay/hotnet2-1.0.0/bin/createPPRMat_1.py", line 96, in <module>
run(get_parser().parse_args(sys.argv[1:]))
File "/home/hotnet2-1.0.0/bin/createPPRMat_1.py", line 38, in run
edges = [map(int, l.rstrip().split()[:2]) for l in open(args.edgelist_file)]
IOError: [Errno 2] No such file or directory: '\x1b[01;00m/home/hotnet2-1.0.0/iref/iref_edgelist_164\x1b[0m'

The file is not a text file. 该文件不是文本文件。 It is a binary file. 它是一个二进制文件。 It contains file names and shell color codes. 它包含文件名和外壳颜色代码。 You need to remove (or filter out) those color codes before you can use the filenames verbatim (alternatively get a clean copy of the file and fix whatever process spits out color codes to files to stop doing that). 您需要先删除(或过滤掉)这些颜色代码,然后才能逐字使用文件名(或者获取文件的完整副本,并修复任何将颜色代码吐出到文件中的过程,以停止这样做)。

The different error (missing argument) you get when you use the backticks is because the backticks run their contents as a command. 使用反引号时遇到的另一个错误(缺少参数)是因为反引号是作为命令运行其内容的。 So `$net` takes the value of the $net variable and tries to run it as a shell command and then replaces the entire backtick quoted string with the output of that command. 因此, `$net`获取$net变量的值,并尝试将其作为shell命令运行,然后用该命令的输出替换整个反引号引起来的字符串。

That's why you get the "no such file or directory" error there (because the filename, with codes, is not valid) and, subsequently, why the -e flag doesn't have an argument (the backtick string evaluated to the empty string so you end up with -e -i and no argument to -e ). 这就是为什么在那里出现“没有这样的文件或目录”错误的原因(因为带有代码的文件名无效),随后,为什么-e标志没有参数(反引号字符串评估为空字符串)所以你最终-e -i并没有参数-e )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM