LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-23-2009, 04:30 AM   #1
hippotonic
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
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....
 
Old 12-23-2009, 04:33 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
What about the paste command?
Code:
man paste
 
Old 12-23-2009, 05:17 AM   #3
hippotonic
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Hmmm, same thing seems to happen
 
Old 12-23-2009, 05:26 AM   #4
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Are you sure the file contains what you expect it to contain?
 
Old 12-23-2009, 05:34 AM   #5
hippotonic
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 12-23-2009, 05:35 AM   #6
hippotonic
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
For clarity, the text files contain the same number of entries as loops
 
Old 12-23-2009, 05:50 AM   #7
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Can you please post the paste command you use?
 
Old 12-23-2009, 06:15 AM   #8
hippotonic
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 12-23-2009, 08:25 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
 
Old 12-23-2009, 08:58 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
try this
Code:
EOL=false
until $EOL
do
IFS= read -r line || EOL=true
    echo "$line"
done < "file"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd behaviour of tty richard64 Fedora 1 10-20-2006 07:20 AM
spamassassin odd behaviour fedora_user Linux - Software 8 04-12-2006 06:22 PM
odd behaviour of wvdial NNP Linux - General 4 08-05-2005 03:58 PM
Odd behaviour on desktop... user00265 Slackware 19 02-26-2005 07:06 PM
Odd Ogg behaviour Misel Linux - Software 3 05-04-2003 04:18 PM


All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration