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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

piped function does not pass variable value

Status
Not open for further replies.

fcic1

Technical User
Oct 21, 2003
4
US
Hi! I am a beginer in shell scripting and I have the following problem: I define a variable inside a function, but when i use the function with a pipe, i can't get the variable's value in main program.
How can i do to have the variable value after it exits the function? or any other idea how can i do this work ?
Thanks!

---------------------------------------------
#! /bin/ksh

user1="aaaaa"
pass1="bbbbb"

function1()
{
sleep 2
echo $user1
sleep 2
echo $pass1
sleep 1
var_1=`ls -l test1.out`
sleep 1
echo "echo $var_1"
sleep 1
}

function1 | telnet host

echo $var_1
------------------------------------------
 
HAve you tried to export var_1 before calling the function ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I tried but doesn't work.
Of course if I do: export var_1=`ls -l test1.out`
before calling the function will always show the file from the host I'm running from the script, not fom the one I want to telnet to.
 
Have you tried this ?
var_1=[highlight]'[/highlight]`ls -l test1.out`[highlight]'[/highlight]

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
If found this in an AIX documentation
pipeline
A sequence of one or more commands separated by pipe (|). [highlight]Each command in the
pipeline, except possibly the last command, is run as a separate process[/highlight]. However, the
standard output of each command that is connected by a pipe becomes the standard
input of the next command in the sequence. If a list is enclosed with parentheses, it is
carried out as a simple command that operates in a separate

If the function is run in a separate process, the modifications of the environment are lost at the exit of the function.

I have written the following script to verify that :
[tt]
fct() {
Var="set by function ($$)"
export Var
echo "Inside function ($$), Var='$Var'"
ps
}
Var="set by main ($$), before function/pipe"
export Var
echo "Before function/pipe, Var='$Var'"
fct | cat
echo "After function/pipe, Var='$Var'"
[/tt]

The result is :
[tt]
Before function/pipe, Var='set by main (59590), before function/pipe'
Inside function (59590), Var='set by function (59590)'
PID TTY TIME CMD
19348 pts/14 0:00 ps
32090 pts/14 0:00 cat
49402 pts/14 0:00 -ksh
59590 pts/14 0:00 ksh fct_pipe.shl
After function/pipe, Var='set by main (59590), before function/pipe'
[/tt]

Inside the function, the special variable $$ doesn't change.
The 'ps' command doesn't show a supplemental process executing the function. Why ?
The modification of the exported variable is lost.
The result is the same when i run the script with bourne shell.



Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top