Register
It is currently Sat Nov 01, 2014 1:12 am

Source a file and export all variables


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat May 09, 2009 8:50 am   

Joined: Sat May 09, 2009 8:41 am
Posts: 1
I have a file named default.conf like this
A=1
B=2
C=3

And I have a script that source defaults.conf via find
find . -name default.conf |\
while read file; do
for var in $(cat $file | cut -d = -f 1); do
export $var=undef;
done
source $file;
done;
echo $A

The command echo $A return nothing.

Anybody have a valid explanation to this?

PS: My problem is was simplifiquied here


Top
 Profile  
 PostPosted: Sun May 10, 2009 12:09 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
Hmmm...
Code:
find . -name default.conf -exec source '{}' \;
echo $A
should do what you want.


Top
 Profile  
 PostPosted: Mon May 11, 2009 10:23 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I haven't fully investigated but it seems this is a weird bashism down to how sub shells are managed.

Code:
cat file | while read var ; do stuff ; done


The above piece of code opens a subshell due to the pipe. This subshell can have weird effects on variables. I know i'm not explaining this very well but essentially there is a way around the issue by using the following format;

Code:
while read var ; do stuff ; done < file


The above format removes the need for the extra subshell so should work as expected.
It took me AGES to work this out when I first met it.
If you re-factor your code to work in that way you should be able to get around the issue.

Of course there's no reason why Watael's suggestion should not work :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP