0

I'm getting some very strange behaviour that I can't figure out when trying to read a txt file.

I have a txt file that looks like this:

98040520
98041022
98041024
...

And then I have a bash script with some basic debugging that looks like this:

#!/usr/bin/env bash
while read id; do
    test=123
    echo "${test}.png"
    echo $id
    echo "${id}.png"
    echo "test${id}.png"
    exit
done <myfile.txt

And the output is

123.png
98040520
.png
.png98040520

What on earth is going on?

It prints "98040520" just fine, but it won't print "98040520.png", it just prints the ".png" part and an attempt to print "test98040520.png" results in the id being appended creating ".png98040520" and the word "test" excluded altogether!!

I figure it's got to be something to do with line endings (the txt file comes from windows) but I haven't been able to find anything online that will help. I've been searching for how to trim whitespace or line endings which may mean i've been looking for the wrong thing.

In case it's not clear what i'd like to do is get "98040520.png" or more specifically filepath=../../path/to/my/dir/98040520.png but what i've posted above is a simpler example of the problem I seem to be having.

2 Answers 2

2

It's indeed a line-endings issue (see here for an explanation of what the \r is doing to your terminal output).

You can either fix the endings in the file or fix them in the script (tr -d "\r" will remove all instances of \r to yield unix-style line endings in the text passed to the while loop):

#!/usr/bin/env bash
cat myfile.txt | tr -d "\r" | while read id; do
    test=123
    echo "${test}.png"
    echo $id
    echo "${id}.png"
    echo "test${id}.png"
    exit
done

Using the fixed version gives the expected output.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "\r" does?
I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
-1

All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:

while read id; do
    test=123
    echo $test.png
    echo $id
    echo $id.png
    echo test$id.png
done <myfile.txt

2 Comments

The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.