LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-27-2016, 10:00 AM   #1
app4dxh
LQ Newbie
 
Registered: Jan 2016
Posts: 2

Rep: Reputation: Disabled
Smile How differentiate functions with same name in separate shells


Hi, here is my question.
I have a parent shell that will call a function in subshell and return back to the parent.
The next line of code will call a function in the parent (This function name ALSO exits in the subshell)
It seems the function in the subshell is getting executed.
For simplicity, I created small test shells to play around with:
1. Parent shell - calling.sh
DUPL_FUNC()
{
echo "In DUPL_FUNC in calling.sh"
}

. func.sh FUNC_FUNC abc

DUPL_FUNC
exit 0

2. subshell - func.sh:
DUPL_FUNC()
{
echo "In DUPL_FUNC in func.sh"
}

FUNC_FUNC()
{
ARG1=$1
echo "in FUNC_FUNC in func.sh"
}

FUNC_NAME=$*
$FUNC_NAME
return 0

3. Execute:
./calling.sh
in FUNC_FUNC in func.sh
In DUPL_FUNC in func.sh

I want the echo from DUPL_FUNC to come from the parent, calling.sh like such:
In DUPL_FUNC in calling.sh

Thanks if you can help :-)
 
Old 01-27-2016, 10:43 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Basically you can't, a subshell can't know anything about a parent process except for any environment variable's exported from the parent and if the child alters a variable the parent can't know the new value, if the child exports a variable the parent can't know the value, though any children of the child will know about the newly exported variable.

If you need to share functions use the "." or include statements, if you then need to do different things in the function depending on the calling process just pass an argument to the function, either the pid of the calling process or something similar.

Please also use code tags to surround your code as it make it easier to read.
 
Old 01-27-2016, 10:45 AM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Small example, two file called proc1 and proc2 in the same folder, proc1
Code:
#©keithhedger Wed 27 Jan 16:33:58 GMT 2016 kdhedger68713@gmail.com

testfuncproc1 ()
{
	echo "testfunc from proc1"
}

testfuncproc1
./proc2
proc2
Code:
#!/bin/bash -e

#©keithhedger Wed 27 Jan 16:34:09 GMT 2016 kdhedger68713@gmail.com
testfuncproc2 ()
{
	echo "testfunc from proc2"
}

testfuncproc1
./proc2
Execute proc1
Code:
keithhedger@LFSHal:/tmp-> ./proc1
testfunc from proc1
./proc2: line 9: testfuncproc1: command not found
As you can see proc2 ( the child ) cant see the parents function defs.
 
Old 01-27-2016, 10:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
I have a parent shell that will call a function in subshell and return back to the parent.
This is not what you did.
Code:
. func.sh FUNC_FUNC abc
Here you are sourcing (by the use of '.') func.sh, which if it has the same named function as the calling script, will overwrite it, so now only DUPL_FUNC from func.sh exists.

To make it run in a subshell, simply call it:
Code:
func.sh FUNC_FUNC abc
Of course if not in your PATH you will need to give the path to the script.

I didn't test this, but i am pretty sure
 
1 members found this post helpful.
Old 01-27-2016, 04:48 PM   #5
app4dxh
LQ Newbie
 
Registered: Jan 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank You! Made suggested changes, working as I expected.
 
Old 01-28-2016, 12:46 PM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by Keith Hedger View Post
Code:
keithhedger@LFSHal:/tmp-> ./proc1
testfunc from proc1
./proc2: line 9: testfuncproc1: command not found
As you can see proc2 ( the child ) cant see the parents function defs.

Though in bash you can export functions and they're passed to the children through the environment (which was what led to the 'Shellshock' vulnerability thingy a while back).

Code:
test@ws2:/tmp$ cat proc1
#!/bin/bash

testfunc1() {
  echo "testfunc1 from proc1"
}

testfunc1

export -f testfunc1

./proc2
test@ws2:/tmp$ cat proc2
#!/bin/bash

testfunc1

test@ws2:/tmp$ ./proc1
testfunc1 from proc1
testfunc1 from proc1
test@ws2:/tmp$
Exporting functions is a bash specific extension however. Traditional bourne shell and ksh had the good sense not to provide this "feature". When I remember, I make a point of using "#!/bin/bash -p" to disable it.

Last edited by GazL; 01-28-2016 at 12:48 PM.
 
Old 01-28-2016, 02:09 PM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by GazL View Post
Though in bash you can export functions and they're passed to the children through the environment (which was what led to the 'Shellshock' vulnerability thingy a while back).
...
Exporting functions is a bash specific extension however. Traditional bourne shell and ksh had the good sense not to provide this "feature". When I remember, I make a point of using "#!/bin/bash -p" to disable it.
You are of course quite correct I forgot about the -f switch to export, but as you say is non statndard.
 
  


Reply



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
How to run kernel functions in a separate core? reksss Ubuntu 4 10-03-2012 10:24 AM
[SOLVED] Booting LFS on separate USB HDD + separate /boot parition nivwusquorum Linux From Scratch 33 12-14-2011 06:38 AM
[SOLVED] Can all shells read all environmental variables set by other shells? carlodelmundo Linux - Newbie 2 07-23-2010 02:03 PM
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 PM.

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
Open Source Consulting | Domain Registration