简体   繁体   English

如何通过使用bash脚本传递参数来运行python文件?

[英]How to run python file by passing argument using bash script?

I am very new to bash scripts.我对 bash 脚本很陌生。 I have a .txt file with string names separated by lines (have space between each string).我有一个 .txt 文件,字符串名称由行分隔(每个字符串之间有空格)。

my.txt is:我的.txt是:

my name 
my class
my room

When I run my python script using terminal.当我使用终端运行我的 python 脚本时。 I need to pass arguments one by one.我需要一一传递参数。

python3 python_file.py -f 'my name'
python3 python_file.py -f 'my class'
python3 python_file.py -f 'my room'

It works fine.它工作正常。 I want to use bash script for each string (my name, my class, and my room) individually and pass as an argument for a python script.我想为每个字符串(我的名字、我的班级和我的房间)单独使用 bash 脚本,并作为 python 脚本的参数传递。

#!/bin/bash
while read LINE; do
    #echo ${LINE}
    python3 pythonfile.py -f $LINE 
done < my.txt

It doesn't work as each string has a space between them (my name), python assumes as string and display error message它不起作用,因为每个字符串之间都有一个空格(我的名字),python 假定为字符串并显示错误消息

error: unrecognized arguments: name

When I ma trying to put quotes in bash script, it is not working.当我尝试在 bash 脚本中添加引号时,它不起作用。

#!/bin/bash
while read LINE; do
    echo \'${LINE}\'
    #python3 pythonfile.py -f $LINE 
done < my.txt 

output:
'my name
'my class
'my room

with same error message.具有相同的错误消息。

When I tried to put quotes inside .txt file, it doesn't even work then.当我尝试将引号放入 .txt 文件时,它甚至不起作用。

new: my.txt新:我的.txt

'my name'
'my class'
'my room'

same error message:同样的错误信息:

error: unrecognized arguments: name

I do not want to do it with one python script by reading names one by one from my.txt file.我不想通过从 my.txt 文件中一一读取名称来使用一个 python 脚本来完成此操作。 I have some internal coding in python script that is not suitable for this.我在 python 脚本中有一些不适合这个的内部编码。 Hence I want to use bash.因此我想使用 bash。

Please guide.请指导。

My Mac appears to give the expected output when I run this shell script:当我运行这个 shell 脚本时,我的 Mac 似乎给出了预期的输出:

#!/bin/bash
while read LINE; do
    echo python3 pythonfile.py -f \'${LINE}\'
    #python3 pythonfile.py -f $LINE
done < my.txt

Noting that you don't see the final quote in your output, and neither do you strip anything from the input, I suspect that your data file was generated in a Windows environment and contains <CR><LF> terminations for each line.请注意,您没有在输出中看到最后的引号,也没有从输入中删除任何内容,我怀疑您的数据文件是在 Windows 环境中生成的,并且每行都包含<CR><LF>终止符。

So what your script is outputting (the shell strips the terminating line feed) for each line of input is因此,您的脚本为每一行输入输出的内容(shell 去除了终止换行符)是

'some name<CR>'<LF>

The effect of the carriage return is to overprint the first quote with the second one, making it "disappear".回车的作用是将第一个引号与第二个引号重叠,使其“消失”。 There's often a dos2unix or similar utility that will help you convert such data files.通常有一个dos2unix或类似的实用程序可以帮助您转换此类数据文件。

The answer is given by Inian .答案由Inian给出。 Can not accept that comment as answer so posting it here.不能接受该评论作为答案,所以张贴在这里。

It should have been python3 pythonfile.py -f "$LINE"它应该是python3 pythonfile.py -f "$LINE"

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

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