LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 03-02-2012, 04:09 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842

export passes variables to subprocesses, but that doesn't help getting the data back to the parent:
Code:
$ export x=fish
$ echo $x
fish
$ whoami | while :;do x=chips;break;done
$ echo $x
fish
The problem is explained in detail here: I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?

Last edited by ntubski; 03-02-2012 at 04:10 PM. Reason: used [quote] instead of [code]
 
Old 07-30-2014, 01:12 PM   #17
hatalbot
LQ Newbie
 
Registered: Mar 2013
Posts: 1

Rep: Reputation: Disabled
Another way to fix the problem is to place the input into a file,
then redirection to have the while loop process the file

i.e.

-----------------------------------------------------------
Doesn't work due to pipe causing subprocess being created:
-----------------------------------------------------------
export conflict=0
cat $filename | while read inline
do
conflict=1
done

echo $conflict # get 0

--------------------
WORKS!
--------------------
export conflict=0
while read inline
do
conflict=1
done < $filename

echo $conflict # get 1 !

Since no pipe, no subprocess created, values in while loop retained
 
  


Reply

Tags
bash, loop, management, package, variable


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
Define global variable maxmil Linux - Newbie 4 03-07-2006 10:36 AM
How can I define a global variable? kloss Linux - General 4 02-21-2006 04:30 AM
diff between #define and const defined variable b123coder Programming 7 06-27-2005 08:02 AM
how am i define an environment variable? yenonn Slackware 2 12-19-2003 07:44 AM
how to define a global variable Anniebaby Programming 1 11-09-2003 10:43 PM


All times are GMT -5. The time now is 12:36 PM.

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