5

When I am running two while loop and trying to concatenating two variable strings (from two loops), the final output (string) is weird. I have tried different way to concatenating but did not get the answer: Below is the code: [I need vfile="var1_Amon_My_model1*.nc"]

#!/bin/bash
jcount=1
extn="*.nc"
while read line
do
mname=$line
echo " Working on model - " $mname
echo " and model number = "$jcount
while read line
do
vname=$line
vfile="${vname}""_Anom_""${mname}""${extn}"
echo $vfile
done<varlists.txt
echo "******************************************"
jcount=$((jcount+1))
done<model_test1.txt

In varlists.txt

var1
var2

In model_test1.txt

My_model1
My_model2

After executing the script, it is showing

 Working on model -  My_model1
 and model number = 1
*.ncm_My_model1
*.ncm_My_model1
******************************************
2
  • 2
    Can you edit in a hexdump of varlists.txt and model_test1.txt, and of the script output? Use hexdump -C. Commented Sep 11, 2016 at 8:09
  • 1
    Yeah, that's the thing. Bash can also output a variable quoted, which might be a helpful debugging aid in cases like this: printf "%q\n" "$vfile"
    – ilkkachu
    Commented Sep 11, 2016 at 9:34

1 Answer 1

9

Your text files have Windows/DOS CRLF line endings.

The strings are concatenating correctly, but the carriage return byte is left at the end of vname and mname, because it's not part of a line ending on Unix and so wasn't used up by read. When you print the string out, the display is mangled instead. Carriage returns shift the cursor back to the start of the line (like a typewriter), letting text be overwritten.

When you print some text with a carriage return in it, the text up to the CR appears as normal, then the output cursor moves back to the start, and then later parts of the string are being printed over the top of earlier parts. That's why the "*.nc" appears at the start of the line in your reported output even though "${extn}" is last - mname ends with a carriage return.

You can run your text files through dos2unix to remove the extra carriage return bytes. You could also use ${vname%$'\r'} in the script to truncate trailing CRs if you don't want to or can't modify the files.

1
  • Hi Mike and Ilkkachu, Thanks a lot ! After doing dos2unix of those text the program is running perfectly!!! I appreciate your help. hexdump -C varlists.txt gives:00000000 76 61 72 31 0a 76 61 72 32 0a |var1.var2.| 0000000a (after dos2unix). You guessed correctly that those *.txt files were transferred from windows PC...I appreciate your help... Commented Sep 13, 2016 at 7:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .