LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2012, 12:41 PM   #1
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Rep: Reputation: 1
Piping the output of a function


Hi All,
Please explain this behavior of shell script:
##code section

function myfunc
{
echo "Inside func:PID=$$"
echo "x=$x"
x=23
}

echo "before calling myfunc:PID=$$"
x=12
echo "x="$x
myfunc $x #|tee out.txt
echo "After calling myfunc PID=$$"
echo "x="$x

#### End of code #


runnning the script when not redirecting the output of function
$./script.sh

before calling myfunc:PID=1152
x=12
Inside func:PID=1152
x=12
After calling myfunc PID=1152
x=23
------------------------------------------------------------------------
runnning the script when redirecting the output of function
$./script.sh

before calling myfunc:PID=1152
x=12
Inside func:PID=1152
x=12
After calling myfunc PID=1152
x=9
------------------------------------------------------------------------
Somewhere I read that when o/p of function is redirected to a file using tee ,the function is called in a sub shell,so the value of x modified inside the function is not reflecting in the shell, but the PID
is showing same,so how come one can know that the function is called by a sub shell?
Please explain .
thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-15-2012, 01:35 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
$$ is not always the current PID. From the Bash Reference Manual: $$ "Expands to the process id of the shell. In a () subshell, it expands to the process id of the invoking shell, not the subshell".

It is for this reason that bash has $BASHPID.

This modified version of your script (to be called with or without the string tee as an argument) illustrates:
Code:
#!/bin/bash
function myfunc
{
    echo "Inside myfunc: \$BASHPID is $BASHPID, \$\$ is $$, x is $x"
    x=23
}

x=12
echo "Before calling myfunc: \$BASHPID is $BASHPID, \$\$ is $$, x is $x"
if [[ $1 = tee ]]; then
    myfunc | tee out.txt
else
    myfunc 
fi
echo "After calling myfunc: \$BASHPID is $BASHPID, \$\$ is $$, x is $x"
BTW, your script does not redirect when it uses tee, it uses a pipeline

Last edited by catkin; 01-15-2012 at 01:37 PM. Reason: sense
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having issues with piping output TwinReverb Programming 2 11-05-2011 08:19 PM
[SOLVED] piping date output qrange Linux - Newbie 1 07-21-2011 06:44 AM
piping output of list to cp andy.l Linux - General 3 05-16-2011 07:02 PM
Piping output fm cut into sed wtaicken Programming 7 12-09-2008 11:54 AM
Piping output between processes. C. banan Programming 1 11-13-2006 12:25 AM


All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration