Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable scope 1

Status
Not open for further replies.

toolkit

Programmer
Aug 5, 2001
771
GB
A question for all you shell experts:
I have a simple config file like:
Code:
PEOPLE = fred bill ted
And I have a piece of code like:
Code:
while read GROUP DELIM MAILS
do
    eval $GROUP='$MAILS'
    echo $FRED
done < file

echo $FRED  # problem is here
The output is:
Code:
fred bill ted
<blank line>

What I would like is for '$FRED' to be visible outside the while loop. I have tried using export, set, xargs. I think it is a problem with 'while' running in a subshell. Anyone know of a simple way of getting the variable outside of the loop?

Thanks in advance, :) Neil
 
This problem is resolved with bash

The solution i can see is :

while read GROUP DELIM MAILS
do
eval $GROUP='$MAILS'
echo $fred
echo &quot;fred='$fred'&quot; > $$
done < file

. $$ ; rm $$
echo $fred #

Jean Pierre.
 
export FRED

while read GROUP DELIM MAILS
do
eval $GROUP='$MAILS'
echo $FRED
done < file

echo $FRED

I'm not sure where your variable $FRED is set though, sorry for being thick Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Oops, I meant $PEOPLE. Anyways, came up with a solution:
Code:
config:
FRED='a b c'
##
BILL='x y z'

script:
LINES=`grep -v '^#' config`
eval $LINES
echo $FRED
echo $BILL
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top