[SOLVED] [Bash] Process lines from file one at a time.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Currently, I have code which reads from a file. That bit works. I'd like to process each line on the file one at a time.
Currently my code (edited for secuirty) is
Code:
while read LINE
do
if [ my file exists ]
then
echo "Do action A or B"
read ANSWER
if [ $ANSWER = "A" ]
then
do whatever
elif [ $ANSWER = "B" ]; then
do something else
fi
fi
done < "$FILENAME"
However, it checks all lines at once.
So I can't do A or B to the lines that need different options.
I need one line to be processed at a time.
Sorry if I am unclear! If you have any questions, I will try my best to answer them!
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251
Rep:
if [ my file exists ]
then
for l in `cat file`
echo "Do action A or B"
read ANSWER
if [ $ANSWER = "A" ]
then
do whatever
elif [ $ANSWER = "B" ]; then
do something else
fi
if [ my file exists ]
then
for l in `cat file`
echo "Do action A or B"
read ANSWER
if [ $ANSWER = "A" ]
then
do whatever
elif [ $ANSWER = "B" ]; then
do something else
fi
I have to check if the file exists using the line from the file. (It's a list of possible directory names.)
I would point out that a little gotcha is in your original code, which is that if you call read on stdin (look inside your "if") then it will draw the answer from the
open file descriptor which is already being read from, ie $FILENAME
I would point out that a little gotcha is in your original code, which is that if you call read on stdin (look inside your "if") then it will draw the answer from the
open file descriptor which is already being read from, ie $FILENAME
Good catch, grail!
In this case, what do you think about redirecting the input from the file using an unused file descriptor and feed the while read loop from there? Example:
Code:
exec 3< $FILENAME
while read line <&3
do
read answer
echo $((++c)): $line $answer
done
exec 3>&-
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.