简体   繁体   中英

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; 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. 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. 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. 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. 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. 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:

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 :

readarray -t lines < infile.txt
for line in "${lines[@]}; do
    ...
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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