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 10-03-2006, 05:28 PM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
For in; do script


Hello

I have an issue with the fonction for in ;do
here is my script
Code:
#!/usr/bin/bash
for name in $( cat namelist.txt ); do
   echo ${name}
done
the file namelist.txt look like this:
Code:
John O'connor
Tom Anderson
Problem:
This script give me the following output:
Code:
John
O'connor
Tom
Anderson
But i was expecting the following:
Code:
John O'connor
Tom Anderson
I want the variable name to take 1 line at each loop and not only 1 word.
any idea?

Thanks,
Angel.

Last edited by angel115; 10-04-2006 at 05:04 AM. Reason: change the title
 
Old 10-03-2006, 05:35 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
You could use the IFS . For example:
Code:
#!/bin/bash

IFS=$'\n'
for name in $( cat file.txt ); do
   echo $name
done
 
Old 10-04-2006, 02:50 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Original Poster
Rep: Reputation: 79
Hi Homey

Thanks for your interest to my case.

I've try your solution, and here is the result:
Code:
IFS=$'\n'
for name in $( cat file.txt ); do
   echo $name
done
Give me this
Code:
Joh
 O'co

or
Tom A
derso
At each n the script met, it take it as the end of the variable.

Note: If that can help, i'm using a bash script (#!/bin/bash)
Here is the version: GNU bash, version 3.1.17(1)-release

Last edited by angel115; 10-04-2006 at 02:54 AM.
 
Old 10-04-2006, 05:19 AM   #4
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,641

Rep: Reputation: 144Reputation: 144
while read name;
do
echo ${name}
done < file.txt
 
Old 10-04-2006, 04:42 PM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Original Poster
Rep: Reputation: 79
Thumbs up

Thanks it's just work fine.

Angel115
 
  


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



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07: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
Open Source Consulting | Domain Registration