LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-08-2011, 09:54 PM   #1
ridwanfi
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
[bash] while loop ..always print the first variable.


Greetings,
it's look like i have a mistakes on my understanding of loop in bash shell. I write a simple script but, it always repeat the first variable and soon, in every text it made.

Code:
#!/bin/bash
username=/tmp/username.txt
emailadd=/tmp/emailadd.txt

        while read list; do
           while read name; do
            echo "Hello $read" >> /tmp/$list.txt
           done < $username
       echo " your email is $list" >> /tmp/$list.txt
       done < $emailadd
my goal is to create each .txt file with a name of a user, contains a name and an email address on his body text;

Hello user1
your email is email1

while i get with above code is, it creating each user email address.txt with every line of username printed.

Hello user1
Hello user2
your email is email1

I am really appreciate your help.
 
Old 06-08-2011, 10:15 PM   #2
adamben
Member
 
Registered: Feb 2007
Distribution: slackware,gentoo,ubuntu
Posts: 50

Rep: Reputation: 17
I think this article will essentially describe something close(ish) to what you are trying to do. Your output makes sense. For every single item in your first list, you are going to do the inner nested while loop.

http://www.linuxjournal.com/content/...multiple-files
 
Old 06-08-2011, 10:31 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,687

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Also you are implying that both the files being read from are in the correct sorted order.

As for your while loops, you have said for each line in emailadd process the entire file username.
 
Old 06-09-2011, 10:04 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
You might mean this one:
Code:
#!/bin/bash

username=/tmp/username.txt
emailadd=/tmp/emailadd.txt

while read -u 3 uname && read -u 4 email; do
	echo "Hello $uname your email is $email."
done 3< "$username" 4< "$emailadd" >> "/tmp/$list.txt"
Note: I changed $name to $uname. I have an uncertain memory of having a bug using that variable in my early experiences with bash. So just to make sure..

Last edited by konsolebox; 06-09-2011 at 10:13 AM.
 
Old 06-10-2011, 06:01 AM   #5
ridwanfi
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
thank's for your help konsolebox
it's work for me, i have try it.
Code:
#!/bin/bash

username=/tmp/username.txt
emailadd=/tmp/emailadd.txt

while read -u 3 uname && read -u 4 email; do
	echo "Hello $uname your email is $email."
done 3< "$username" 4< "$emailadd" >> "/tmp/$list.txt"
 
Old 06-10-2011, 09:12 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by ridwanfi View Post
thank's for your help konsolebox
wow it's been a while since i heard that.. i just feel thankful.
you're welcome
also, you can mark this thread as solved if you like..
 
  


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
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 09:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 12:13 PM
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 04:37 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 12:09 PM


All times are GMT -5. The time now is 02:30 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration