简体   繁体   English

如何指导sh脚本运行python脚本?

[英]how to direct a sh script to run a python script?

I wrote a python script that calls two files to perform some calculations and asks for a name for the new file to place the calculation in so my code runs like this: 我编写了一个python脚本,该脚本调用两个文件来执行一些计算,并要求输入新文件的名称以将计算结果放入其中,因此我的代码运行如下:

python code.py in_file1 in_file2 outfile

Now, I have several files that need the same calculation and their names only change by the last numbers, so I wanted to do a script that takes the different needed files in a folder performs the python script and name the outputs changing only the las number according to the given in_file1 (infield_2 actually does not change). 现在,我有几个文件需要相同的计算,并且它们的名称仅按最后一个数字更改,因此我想执行一个脚本,该脚本将文件夹中所需的不同文件进行执行,并执行python脚本并命名输出,仅更改las号根据给定的in_file1(infield_2实际上不变)。

I tried something simple but is not working 我尝试了一些简单的方法,但是没有用

#!/bin/bash
python code.py in_file[19]* infile_2 outfile[19]*

I get an error from the usage of python saying that usage: python code.py [-h] in in2 out unrecognized arguments 我从python的用法中得到了一个错误,说用法:python code.py [-h] in2 out in uncognized arguments

I know for sure that code.py works, I just wanted to spare to do it one file at a time. 我肯定知道code.py可以工作,我只是想节省一次将其做一个文件。 Thank you. 谢谢。 I am really new in python and linux, appreciate any help you can give. 我真的是python和linux的新手,感谢您能提供的任何帮助。

You can do the same in Python 您可以在Python中执行相同的操作

from subprocess import call

fname="in_file{} infile2 outfile{}"
for x in xrange(1,11):
    d=call(["python","code.py",fname.format(x,x)])
    if d:
        print "Error executing: {}".format(d)

If you execute the following 如果执行以下

fname="in_file{}"
for x in xrange(1,11):
    print ["python","code.py",fname.format(x)]

It will print the following 它将打印以下内容

['python', 'code.py', 'in_file1']
['python', 'code.py', 'in_file2']
['python', 'code.py', 'in_file3']
['python', 'code.py', 'in_file4']
['python', 'code.py', 'in_file5']
['python', 'code.py', 'in_file6']
['python', 'code.py', 'in_file7']
['python', 'code.py', 'in_file8']
['python', 'code.py', 'in_file9']
['python', 'code.py', 'in_file10']

The shell simply expands the wildcard in_file[19]* there and then into list of all matching files. Shell只是将通配符in_file[19]*在那里扩展,然后扩展到所有匹配文件的列表中。 There is no loop here. 这里没有循环。 If you want a loop, you will need an explicit loop, something like 如果需要循环,则需要一个显式循环,例如

#!/bin/bash
for file in in_file[19]*; do
    python code.py "$file" infile_2 "out${file#in}"
done

where the variable file gets assigned to each matching file in turn, and the variable substitution ${var#prefix} expands to the value of var with any string prefix removed from the beginning. 依次将变量file分配给每个匹配的文件,变量替换${var#prefix}扩展为var的值,并且从开头删除了任何字符串prefix

Incidentally, the python is redundant if you make code.py executable, and ensure it has a correct shebang line . 顺便说一句,如果您将code.py可执行文件,并确保它的shebang行正确,则python是多余的。

Note also that [19] matches a single character, so your wildcard matches any files whose name starts with in_file1 or in_file9 but no others. 另请注意, [19]匹配单个字符,因此通配符匹配名称以in_file1in_file9开头的任何文件,但不匹配其他文件。 I'm speculating maybe that's not what you mean. 我推测这可能不是您的意思。

You can do this in : 您可以在执行此操作:

import os

name = "in_file%d"
for i in range(1, 21):
    os.system("python code.py in_file{} in_file2 outfile{}".format(i, i))

This works as the following, with the os library calling the outputted strings: 如下所示, os库调用输出的字符串:

>>> for i in range(1, 21):
...     "python code.py in_file{} in_file2 outfile{}".format(i, i)
... 
'python code.py in_file1 in_file2 outfile1'
'python code.py in_file2 in_file2 outfile2'
'python code.py in_file3 in_file2 outfile3'
'python code.py in_file4 in_file2 outfile4'
'python code.py in_file5 in_file2 outfile5'
'python code.py in_file6 in_file2 outfile6'
'python code.py in_file7 in_file2 outfile7'
'python code.py in_file8 in_file2 outfile8'
'python code.py in_file9 in_file2 outfile9'
'python code.py in_file10 in_file2 outfile10'
'python code.py in_file11 in_file2 outfile11'
'python code.py in_file12 in_file2 outfile12'
'python code.py in_file13 in_file2 outfile13'
'python code.py in_file14 in_file2 outfile14'
'python code.py in_file15 in_file2 outfile15'
'python code.py in_file16 in_file2 outfile16'
'python code.py in_file17 in_file2 outfile17'
'python code.py in_file18 in_file2 outfile18'
'python code.py in_file19 in_file2 outfile19'
'python code.py in_file20 in_file2 outfile20'
>>> 

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

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