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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-23-2009, 03:30 AM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Rep:
|
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....
|
|
|
12-23-2009, 03:33 AM
|
#2
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596
Rep:
|
What about the paste command?
|
|
|
12-23-2009, 04:17 AM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
Hmmm, same thing seems to happen
|
|
|
12-23-2009, 04:26 AM
|
#4
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596
Rep:
|
Are you sure the file contains what you expect it to contain?
|
|
|
12-23-2009, 04:34 AM
|
#5
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
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
|
|
|
12-23-2009, 04:35 AM
|
#6
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
For clarity, the text files contain the same number of entries as loops
|
|
|
12-23-2009, 04:50 AM
|
#7
|
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596
Rep:
|
Can you please post the paste command you use?
|
|
|
12-23-2009, 05:15 AM
|
#8
|
LQ Newbie
Registered: Dec 2009
Posts: 7
Original Poster
Rep:
|
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
|
|
|
12-23-2009, 07:25 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
|
|
12-23-2009, 07:58 AM
|
#10
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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 01:04 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|