[英]Passing individual lines from files into a python script using a bash script
This might be a simple question, but I am new to bash scripting and have spent quite a bit of time on this with no luck; 这可能是一个简单的问题,但是我是bash脚本的新手,并且花了很多时间没有运气。 I hope I can get an answer here.
我希望我能在这里得到答案。
I am trying to write a bash script that reads individual lines from a text file and passes them along as argument for a python script. 我正在尝试编写一个bash脚本,该脚本从文本文件读取单独的行,并将它们作为python脚本的参数传递。 I have a list of files (which I have saved into a single text file, all on individual lines) that I need to be used as arguments in my python script, and I would like to use a bash script to send them all through.
我有一个文件列表(已保存到单个文本文件中,位于单独的行中),需要在我的python脚本中用作文件的参数,我想使用bash脚本将其全部发送出去。 Of course I can take the tedious way and copy/paste the rest of the python command to individual lines in the script, but I would think there is a way to do this with the "read line" command.
当然,我可以采取乏味的方法,将python命令的其余部分复制/粘贴到脚本中的各个行中,但是我认为可以使用“读取行”命令来做到这一点。 I have tried all sorts of combinations of commands, but here is the most recent one I have:
我已经尝试过各种命令组合,但是这是我最近使用的命令:
#!/bin/bash
# Command Output Test
cat infile.txt << EOF
while read line
do
VALUE = $line
python fits_edit_head.py $line $line NEW_PARA 5
echo VALUE+"huh"
done
EOF
When I do this, all I get returned is the individual lines from the input file. 执行此操作时,返回的只是输入文件中的各行。 I have the extra VALUE there to see if it will print that, but it does not.
我在那里有多余的VALUE,看它是否可以打印出来,但事实并非如此。 Clearly there is something simple about the "read line" command that I do not understand but after messing with it for quite a long time, I do not know what it is.
显然,我对“读取行”命令有一些简单的了解,但是在弄乱了很长时间之后,我不知道它是什么。 I admit I am still a rookie to this bash scripting game, and not a very good one at that.
我承认我仍然是这个bash脚本游戏的新手,并且不是一个很好的人。 Any help would certainly be appreciated.
任何帮助将不胜感激。
You probably meant: 您可能的意思是:
while read line; do
VALUE=$line ## No spaces allowed
python fits_edit_head.py "$line" "$line" NEW_PARA 5 ## Quote properly to isolate arguments well
echo "$VALUE+huh" ## You don't expand without $
done < infile.txt
Python may also read STDIN so that it could accidentally read input from infile.txt
so you can use another file descriptor: Python可能还会读取STDIN,因此它可能会意外读取
infile.txt
输入,因此您可以使用另一个文件描述符:
while read -u 4 line; do
...
done 4< infile.txt
Better yet if you're using Bash 4.0, it's safer and cleaner to use readarray
: 更好的是,如果您使用的是Bash 4.0,则使用
readarray
更安全,更干净:
readarray -t lines < infile.txt
for line in "${lines[@]}; do
...
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.