0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

The first version you put up worked like a charm; I also tried the last one and got some issues, but that is something I can work out on my own after learning more about arrays. Out of curiosity, what do you think hosed me more: the spaces between VALUE and $line or the lack of a semi-colon on the first line? Or was it a conjunctive issue?
The real problem was that you did cat infile.txt << EOF and placed the commands within that here document block. VALUE = $line was just a syntax error problem.
Also, after removing the section that prints the value of each line, it still prints the name of each file before processing them. Not a big issue, but I would still like to know how to stop it from doing that. Does that have to do with the "while read line" command? Is there a way to input each line's data into the script without printing it onto the screen as well?
@user3827679 No other part of the script prints a message besides echo "$VALUE+huh". If it's the one you removed then it's likely that the python script fits_edit_head.py is already the one that is printing the names and should be examined.
Answers much appreciated, sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.