LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Joining variables - Odd behaviour (https://www.linuxquestions.org/questions/linux-newbie-8/joining-variables-odd-behaviour-777635/)

hippotonic 12-23-2009 03:30 AM

Joining variables - Odd behaviour
 
Okay, so this should be incredibly simple, but I keep getting an unexpected output and can't figure out why.

Essentially I have several text files that I refer to in loops, so a simple single line example would be:

text1.txt --------> /mydrive/nowhere/anywhere
text2.txt---------> pongo

I want to produce (to make directories, direct other processes that need file location etc.,):

/mydrive/nowhere/anywhere/pongo

My attempt (as there are actually many lines in the text files):

wotsit=text1.txt
frazzle=text2.txt

exec 1<$wotsit

while read -r targfile<&1

exec 2<$frazzle

while read -r pong<&2

echo "${targfile}/${pong}"

done
done

Now annoyingly my output is:

pongoive/nowhere/anywhere

With the EXCEPTION of the last line of the loop... Why???????????

Any help much appreciated as I need to get my batch cooking by tonight.

PS. I've tried lots of permutations including putting them into arrays; same result....

Agrouf 12-23-2009 03:33 AM

What about the paste command?
Code:

man paste

hippotonic 12-23-2009 04:17 AM

Hmmm, same thing seems to happen

Agrouf 12-23-2009 04:26 AM

Are you sure the file contains what you expect it to contain?

hippotonic 12-23-2009 04:34 AM

Yup (I recreated the file and renamed something completely different to be 100%). As I said it takes the variable I would expect to be second and puts it first, superimposing on the second (as in the above example). In my actual script it loops over 10 lines, and only the last one comes out correctly:

pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
pongoive/nowhere/anywhere
/mydrive/nowhere/anywhere/pongo


Got me stumped. The easy hack would be to create a large text file and bypass this step, but it's not particularly elegant and it "should" work

hippotonic 12-23-2009 04:35 AM

For clarity, the text files contain the same number of entries as loops

Agrouf 12-23-2009 04:50 AM

Can you please post the paste command you use?

hippotonic 12-23-2009 05:15 AM

Ah, may have found the source to the problem. I am working using bash on VMware but originally created the .txt files in Windows notepad. I've just recreated the txt files using textedit on the VMware and mysteriously the problem is solved and it all works beautifully.

Code is pretty much as given above.

Lesson learnt about windows I guess

Thanks

catkin 12-23-2009 07:25 AM

Note pad gave the file MS line ends of carriage return and line feed a.k.a CRLF. You script read up to the and including CR which the terminal treated as, er, carriage return.

You could have debugged this by writing to a file instead of the screen or piping to od:
Code:

echo "${targfile}/${pong}" > /tmp/trash
echo "${targfile}/${pong}" | od -c

Some text editors (vi family for one) would display the CRs. od would show the characters in octal making the CR visible.

ghostdog74 12-23-2009 07:58 AM

try this
Code:

EOL=false
until $EOL
do
IFS= read -r line || EOL=true
    echo "$line"
done < "file"



All times are GMT -5. The time now is 07:47 PM.