The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell function, system, awk jkl_jkl Shell Programming and Scripting 5 06-02-2008 05:46 AM
shell function help Amresh Dubey Shell Programming and Scripting 0 11-15-2007 01:16 AM
how to see function in shell shriashishpatil UNIX for Advanced & Expert Users 2 05-09-2006 08:35 PM
getting the value of a c function in shell script surjyap Shell Programming and Scripting 1 09-27-2005 01:33 AM
calling a c function from shell seshagiri UNIX for Dummies Questions & Answers 4 12-06-2000 06:51 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 3
Prallel function with different shell ids

Hi,
I have a script as follows


func1()
{
echo $$
}

func1
func1


This returns the id of the current shell twice so outputs:
4201
4201


I'd like to change this script so the function is called in a different subshell each time, giving a different shell id for each function call.
i.e. output will be something like:
4201
4204

Does anyone know how I can do this? Is there a way to execute a function in a seperate subshell? Or, can I open a new subshell, call the function and then exit back to the parent shell all from within the script??

I could put the function into a seperate new script and then run this script twice from my original script. This would run the new script in a new subshell. However I would rather keep the code in the original script if possible.

Thanks in advance,
Andy
Reply With Quote
Forum Sponsor
  #2  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
You cannot call a function across shell, but you always have the option to open a sub shell in your script and execute the script again. Be cautious while doing so or you can end up writing a recursive function.

regards
Apoorva Kumar
Reply With Quote
  #3  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 3
I feared that might be the answer.

Having looked about a bit, I thought that:

echo $$
(echo $$)

would produce different spids as one is run in a subshell. However this outputs the same for both. Any idea why that is the case??

Cheers,
Andrew
Reply With Quote
  #4  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
It's because you are calling a comelete command not a funtion. What was asked in the original post was something like calling a function across shells, which doesnot seems to be possible as a sub shell will not be aware of the function.

regards
Apoorva Kumar
Reply With Quote
  #5  
Old 08-18-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Seems that the shell does'nt update the value of $$
The following code x.sh :
Code:
func1()
{
  (
     ps -f
     echo $$
  )
}
func1
func1
gives the ouput :
Code:
     UID   PID  PPID   C    STIME    TTY  TIME CMD
  gssjgu 27864 30168   1 14:30:55  pts/3  0:00 ps -f 
  gssjgu 30168 32486   0 14:30:55  pts/3  0:00 sh x.sh 
  gssjgu 32486 34188   1 14:30:55  pts/3  0:00 sh x.sh 
  gssjgu 34188 27930   0   Aug 09  pts/3  0:01 -ksh 
32486
     UID   PID  PPID   C    STIME    TTY  TIME CMD
  gssjgu 27866 30170   1 14:30:55  pts/3  0:00 ps -f 
  gssjgu 30170 32486   0 14:30:55  pts/3  0:00 sh x.sh 
  gssjgu 32486 34188   1 14:30:55  pts/3  0:00 sh x.sh 
  gssjgu 34188 27930   0   Aug 09  pts/3  0:01 -ksh 
32486
The pid displayed is the same 32486 but the echo command is executed in two different subshells of which the pid is displayed as the parent pid of the ps command.


Jean-Pierre.
Reply With Quote
  #6  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 3
Do you know if it is possible to open a new shell from within the script which you can perform the echo and then exit back to the parent?

I tried

ksh
echo $$
exit
echo $$

But it never exits back to the parent.

Thanks for your replies by the way.

Andy
Reply With Quote
  #7  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Wink

This may be little ugly, but what you want to achive can be done this way also .......

say we have one file 'file1' which has only one line 'echo $$'.
We have another file 'file2' which is.....
echo $$
sh file1

if you execute the file2 you will get the desirable.

regards
Apoorva Kumar
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0