Operating Systems > Linux and UNIX
shell variables
caveman_piet:
hi guys -
how do i get a shell variable data available
that is changed in a "while" loop?
eg.
#==
ipad="168.12"
export ipad
echo "$ipad before"
cat afile|while read thevar therest
do
ipad=$thevar
echo "$ipad inside"
done
echo "$ipad after"
#==
if the file has one line as follows
202.15 and the rest of the line
the first echo gives "168.12 before"
the 2nd gives "202.15 inside"
But the 3rd gives "168.12 after"
So how do I get the value for $ipad
that was set inside the loop?
Is this clear - or am I confusing?
Running redhat 7.3
2.4.18-17.7.x
Regards in advance!
voidmain:
Your while statement is after a pipe (|) symbol which means your "do" loop is done in a sub shell. The variable would be changed in the subshell but not passed back to the parent shell when control is returned. You are going to end up with what was the variable last was in the parent. Varible values can be transmitted from a parent to a subshell (if they are "export"ed) but variable values can not be returned to the parent from a sub shell.
Here is one solution. It's not a "while" loop but it accomplishes the same thing. If you absolutely need a while loop I can give you an example of that. The key is not to use the pipe before the while:
--- Code: ---
--- End code ---
[ October 20, 2002: Message edited by: void main ]
caveman_piet:
TX voidmain
That is a solution - but my previous script
omitted the obvious - ie. many params.
#====
ipad="168.12"
export ipad
echo "$ipad before"
dpad="thename"
export dpad
echo "$dpad before"
cat ctrl_1.pps|while read thevar thename therest
do
ipad=$thevar
dpad=$thename
echo "$ipad inside"
echo "$dpad inside"
done
echo "$ipad after"
echo "$dpad after"
#===
and the file afile as follows
202.45 aname some more params
If the solution you gave is the only answer
I'll have to use a counter and lotsa code
to get each parameter set.
Tx again in advance.
voidmain:
If you need the values after the loop, I assume there will only be one line in the file. If so what is the purpose of the while loop? Unless you only need the last line. But if there is only one line in the file why not just do this:
--- Code: ---
--- End code ---
But if you really want to do it in a loop, do it like this:
--- Code: ---
--- End code ---
That's ugly. I would probably switch to Perl at this point.
[ October 20, 2002: Message edited by: void main ]
caveman_piet:
TX again voidmain
Here is a homebrew solution
Primary 'cause I don't know before
hand which line to use
#===
ipad="168.12"
export ipad
echo "$ipad before"
dpad="thename"
export dpad
echo "$dpad before"
theline=`grep -v qqq afile`
read thevar thename therest <<THEEND
$theline
THEEND
ipad=$thevar
dpad=$thename
echo "$ipad inside"
echo "$dpad inside"
echo "$ipad after"
echo "$dpad after"
#===
so with a file with 3 lines - and somewhat as follows
this line to ignore qqq
202.456 para2 para3 and other paras
and ignore this line qqq
As I know I only need 1 line this'll work??
In any case multiple returns - and
only the first line is used..
TX for your time voidmain.
Navigation
[0] Message Index
[#] Next page
Go to full version