incorrect file reading in bash

Posted on 2016-09-25
Last Modified: 2016-10-24
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:

and I have the following code:
while read -r 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 {
server {
meaning the scipt skips the last one, any idea why?
Question by:David Sankovsky
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Expert Comment

by:Terry Woods
ID: 41814595
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

LVL 27

Accepted Solution

Dr. Klahn earned 500 total points
ID: 41814613
Suggest:  Dump the file in hex and ASCII to confirm there are no hidden control characters.
LVL 40

Expert Comment

ID: 41815276
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Expert Comment

ID: 41815277
What happen if you add one more line to the file?
LVL 40

Expert Comment

ID: 41836343

Your feedback is awaited.
LVL 19

Expert Comment

ID: 41836595
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
     echo "server $NodeIP {" >> /scripts/testloop.txt

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.

Author Closing Comment

by:David Sankovsky
ID: 41856735
I had hidden characters... rookie mistake.. Thanks for the help

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sed question 2 122
Remote install of MSI file 4 53
how to pick specific file from ftp 13 71
Unix Command (AIX) 3 40
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question