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.... |
What about the paste command?
Code:
man paste |
Hmmm, same thing seems to happen
|
Are you sure the file contains what you expect it to contain?
|
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 |
For clarity, the text files contain the same number of entries as loops
|
Can you please post the paste command you use?
|
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 |
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 |
try this
Code:
EOL=false |
All times are GMT -5. The time now is 07:47 PM. |