Avatar of David Sankovsky
David Sankovsky
Flag for Israel asked on

incorrect file reading in bash

Hi Experts.

I'm writing an automated Script in Bash, and I've got into a bit of a block...
I have a file called /scripts/resources/nodes,txt
The file currently hold only three lines:

10.5.17.3
10.5.17.4
10.5.17.5

and I have the following code:
#!/usr/bin/bash
filename="/scripts/resources/nginx_nodes_list.txt"
while read -r line
do
     NodeIP=$line
     echo "server $NodeIP {" >> /scripts/testloop.txt
done < "$filename"

Open in new window


However, when I run the scripts and cat the resulting file, I only see 2 lines:

server 10.5.17.3 {
server 10.5.17.4 {
meaning the scipt skips the last one, any idea why?
Shell ScriptingLinux OS Dev

Avatar of undefined
Last Comment
David Sankovsky

8/22/2022 - Mon
Terry Woods

When I set up a test doing the same thing, it worked.

Try double checking your input data just to make sure everything is actually as you say:
cat /scripts/resources/nodes,txt

Open in new window

and your output (you may want to empty the output file first and rerun the script, since it's appending each time):
cat /scripts/testloop.txt

Open in new window

ASKER CERTIFIED SOLUTION
Dr. Klahn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
omarfarid

try read without -r option.

Also, try to run script with bash -x and see what you get. It will show you each command while executing the script
omarfarid

What happen if you add one more line to the file?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
omarfarid

David,

Your feedback is awaited.
simon3270

You get this if the last line of the file does not have a carriage return (lots of things seem to do this, including some IDEs and Windows editors).

One thing you could try is:
grep . "$filename" | while read -r line
do
     NodeIP=$line
     echo "server $NodeIP {" >> /scripts/testloop.txt
done

Open in new window

A drawback with this is that any variables set within the loop are no longer set outside the loop.  If, for example, you had a counter within the loop which you incremented for each line, that value would be lost when the loop finished.  If you need such a value, write it to a file within the loop, then read the file outside.
David Sankovsky

ASKER
I had hidden characters... rookie mistake.. Thanks for the help
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.