LinuxQuestions.org
Register a domain and help support LQ
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 02-01-2007, 03:33 PM   #1
whammack
LQ Newbie
 
Registered: Sep 2004
Distribution: Libranet 2.81
Posts: 24

Rep: Reputation: 15
Bash script while loop problems


I have a while loop in my script that doesn't work correctly. It is designed to read the lines from a file and concatanate them into one long string. It does this fine WITHIN the loop, but once outside the loop the concatinated variable is empty. What am I missing here? The section of code is

List=""
cat $InFile | while read line
do
List=$List" "$line
echo $List
done
echo "Here 1"
echo $List
echo "Here 2

Note that $List within the loop prints correct: It grows with each line read, but once outside the loop $List (just below echo "Here 1" is empty!
 
Old 02-01-2007, 04:22 PM   #2
Xaphoid
LQ Newbie
 
Registered: Jul 2006
Location: Saskatoon, SK
Distribution: Kubuntu
Posts: 20

Rep: Reputation: 0
Gee that looks like more work then needed!?

Couldn't you just do something simple like:

Code:
List=`cat FILENAME`
Note those are back quotes the one you get when you press ~ key on a US keyboard.

I think that will do it for you and in one line!
 
Old 02-01-2007, 05:36 PM   #3
whammack
LQ Newbie
 
Registered: Sep 2004
Distribution: Libranet 2.81
Posts: 24

Original Poster
Rep: Reputation: 15
My, oh, my that's just dandy! Works fine. Still ... I wonder what was happened with my while loop .... Thanks for your prompt reply.
 
Old 02-01-2007, 07:53 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi, whammack.

I haven't looked at the internals of the shells, but this how it seems to work.

Constructs like while will be run in a separate process, so the variables will not be available to the outside process, even though they look like they will be or should be.

I put a monitor statement outside and inside your loop:
Code:
 The outside process is:
   PID TTY          TIME CMD
 4894 pts/1    00:00:00 tcsh
18760 pts/1    00:00:00 sh
18762 pts/1    00:00:00 ps
 The loop process is:
   PID TTY          TIME CMD
 4894 pts/1    00:00:00 tcsh
18760 pts/1    00:00:00 sh
18764 pts/1    00:00:00 sh
18765 pts/1    00:00:00 ps
So process 18760 is the main shell process (ignore tcsh, I use that for interactive work), but as soon as you are in the loop, another shell process appears, 18764, to deal with the loop. You cannot pass contents of variables back up the process tree, so your original value is still there.

If it's critical that you save that, you would need to use some other method, such as writing it to a file. Note that just as you can pipe into a loop, you can re-direct out of the loop ... cheers, makyo
 
Old 02-01-2007, 08:22 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,517

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Here is one version that overcome the subprocess issue:

Code:
List=""
while read line
do
  List=$List" "$line
  echo $List
done < $InFile
echo "Here 1"
echo $List
echo "Here 2"
By the way, it's not the while that makes a separate process, but the pipe.
 
Old 02-01-2007, 08:26 PM   #6
whammack
LQ Newbie
 
Registered: Sep 2004
Distribution: Libranet 2.81
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks so much. Thought it was a separate process - but I often get confused by parent and child and so on -- these posts were very, very helpful.
 
Old 02-01-2007, 09:10 PM   #7
spicetrader
LQ Newbie
 
Registered: Nov 2004
Posts: 2

Rep: Reputation: 0
Is $InFile from a Windows system? The CR/LF line ends on Windows may contribute to the issue. Working on my Cygwin-on-XP rig, I got a pile of collided text in $List, when reading from a CR/LF file. Then I gave command

cat readme.txt | d2u > ureadme.txt

and ran with InFile=ureadme.txt

which worked fine. I used zsh.
 
Old 02-02-2007, 06:31 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

A thank you to jlliagre for clarifying the issue (and jogging my memory about the items in a pipeline) ... cheers, makyo
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 10:18 AM
Bash script Question while loop glennph93 Programming 6 05-25-2007 04:27 PM
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM
bash script - for loop question rignes Programming 3 10-06-2004 12:16 AM


All times are GMT -5. The time now is 12:08 AM.

Main Menu
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