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?
LVL 8
David SankovskySenior SysAdminAsked:
Who is Participating?
 
Dr. KlahnConnect With a Mentor Principal Software EngineerCommented:
Suggest:  Dump the file in hex and ASCII to confirm there are no hidden control characters.
0
 
Terry WoodsIT GuruCommented:
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

0
 
omarfaridCommented:
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
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
omarfaridCommented:
What happen if you add one more line to the file?
0
 
omarfaridCommented:
David,

Your feedback is awaited.
0
 
simon3270Commented:
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.
0
 
David SankovskySenior SysAdminAuthor Commented:
I had hidden characters... rookie mistake.. Thanks for the help
0
All Courses

From novice to tech pro — start learning today.