Solved

incorrect file reading in bash

Posted on 2016-09-25
7
49 Views
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:

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?
0
Comment
Question by:David Sankovsky
7 Comments
 
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

0
 
LVL 23

Accepted Solution

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

Expert Comment

by:omarfarid
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
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 40

Expert Comment

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

Expert Comment

by:omarfarid
ID: 41836343
David,

Your feedback is awaited.
0
 
LVL 19

Expert Comment

by:simon3270
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
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
 
LVL 6

Author Closing Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now