1

I'm having issues with the following bash script trying to parse a version number from a WordPress readme file.

cat readme.txt | { while read -r a b c d; do if [ ${a} == "Stable" ] && [ ${b} == "tag:" ]; then VERSION="$c" fi done out="Updated to version $VERSION thanks" echo $out } 

The output I expect is

Updated to version 1.15 thanks 

but the actual output is

 thanks to version 1.15 

as though the 'thanks' is replacing the front of the string, not being appended to the end. Any clues?

2 Answers 2

4

readme.txt and/or your script has DOS line endings; the value of VERSION has a trailing carriage return which affects the output.

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

3 Comments

It must be the file; if it were the script he would be getting syntax errors.
Ah, right. I couldn't see how $c could get a carriage return from the file, but that just requires a line with only 3 fields so that d isn't set.
Thanks modified code is VERSION=$(echo $c|tr -d '\n')
1

If you pipe the output to cat -A you will likely find out that $VERSION contains a carriage return.

You can get rid of the CRs with tr:

$ echo $'foo\rb' boo $ echo $'foo\rb' | tr -d '\r' foob 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.