Varialble in pipelines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Varialble in pipelines
# 1  
Old 07-09-2007
Varialble in pipelines

Hi,
As per shell docs because commands in pipelines are run as separate processes,
variables set in a pipeline have no effect on the parent shell

In the sample below I am unable to set variable in Proc1 so that I can see it in the main program

Is there any work around ?

Thanks
Zam

#!/bin/ksh
Proc1()
{
echo $MYVAR in Proc1
MYVAR=TRUE
echo line1
echo line2
}
### main body ###
export MYVAR=FALSE
Proc1|
while read line;do
echo $line
echo $MYVAR
done
# 2  
Old 07-09-2007
Quote:
Originally Posted by zam
Is there any work around ?
Use temporarary files instead of pipes.
# 3  
Old 07-09-2007
I guess if I have no alternatives I will
Thank youSmilie
# 4  
Old 07-09-2007
Perhaps you could set the variable in the loop...
Code:
Proc1()
{
echo line1
echo line2
}
Proc2()
{
MYVAR=TRUE
}
### main body ###
export MYVAR=FALSE
Proc1|\
while read line;do
  Proc2
  echo $line
  echo $MYVAR
done

# 5  
Old 07-09-2007
MySQL

Thank you Ygor. I have just done it a 5 min ago Smilie. I don't know why I didn't thought of it earlier --- tunnel vision I guess.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read from file and split varialble

Hello Experts, Can anybody assist me in writing a code to do the following: I have a file present in the same directory from where this cod would run. The content of the file would be as below: Config Filename: config_details.txt Format: Server_prefix,IP_of_server,username, password,... (1 Reply)
Discussion started by: chetanojha
1 Replies

2. Shell Programming and Scripting

UNIX Pipelines

What if you want to have just one single pipeline that will create a file (let's say x) and we want all the content from another file (we can call it y), one word per line? (7 Replies)
Discussion started by: sarahahah
7 Replies

3. UNIX for Dummies Questions & Answers

Building Pipelines?

How would I combine commands in a pipeline to produce an alphabetized list of who's online at a particular moment? Then: How would I take that pipeline and turn it into a command called "whoison"? (1 Reply)
Discussion started by: greeky
1 Replies

4. UNIX for Dummies Questions & Answers

Pipelines

Now before this thread gets closed, please be aware this is not classwork or homework, I am trying to learn unix by myself, and have come stuck below. So it is a pointless to close this thread, as I am trying to improve my unix by asking advice from people. That is what I assume a forum is for! ... (1 Reply)
Discussion started by: Vn3050
1 Replies

5. UNIX for Dummies Questions & Answers

multiple pipelines

howdy all, i've been trying to find info on piping info between multiple commands from the command line, but i've been unable to find any examples of piping continuous data through a chain of commands. basically, i'm trying to parse data from the top command and send it out over udp to another... (1 Reply)
Discussion started by: ohhmyhead
1 Replies
Login or Register to Ask a Question