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!

pipe command problem 1

Status
Not open for further replies.

vlz

IS-IT--Management
Aug 11, 2002
56
IL
Hi,
I have a following script:
--------------------------------------------------
#!/bin/ksh
function start{
var=abc
echo "in function var=$var"
}

start | tee log
echo "in program var=$var"
--------------------------------------------------
This is the output I get:
in function var=abc
in program var=
(i.e. get empty value for var)

When I remove "| tee log"
I get the proper output:

in function var=abc
in program var=abc

How can you explain and fix it?

Thanks in advance,
Vadim
 
I don't think this is a fix, but a workaround depends how complicate with your requirement:

start > log
cat log

I am also wondering why pipe could cause this issue.
 
This happens due you define your var 'globally' default, theres keyword named 'local' to define functions intern in functions

foo() { bar=hiya; echo $bar; }

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Heh sorry, foo() { local bar=hiya; echo $bar; }
 
Calling a function with I/O redirection spawns the function in a subshell and thus another instance of the var variable.
Here the proof of concept:
#!/bin/ksh
function start{
var=abc
echo "in function var=$var"
}
start | tee log
echo "in program var=$var should be empty"
(start
echo "in program var=$var shouldn't be empty"
) | tee log

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top