Prallel function with different shell ids


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prallel function with different shell ids
# 1  
Old 08-18-2006
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
# 2  
Old 08-18-2006
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
# 3  
Old 08-18-2006
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
# 4  
Old 08-18-2006
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
# 5  
Old 08-18-2006
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.
# 6  
Old 08-18-2006
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
# 7  
Old 08-18-2006
Hammer & Screwdriver

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

List of all ids,groups, privilege ids

I wish to pull out a list of all user ids on the system, including the privileged ids, the groups to which they belong to. Sometimes after deleting an id also, its home dir does not get deleted or an entry is left behind in /etc/passwd. Can someone help me with a script to achieve both. (2 Replies)
Discussion started by: ggayathri
2 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Shell Script for generating NTIDs(Usernames) from the email ids which exists in MSAD Directory

Hi Everyone, I just need a shell script which automatically gives the list of NT IDs mean the Usernames from the list of email ids. We have around 140 users from AMERICAS,ASIAPACIFIC and EMEA User Directories and we have their email ids.For ex. i have email id called naveen-kumar.dasu@hp.com... (7 Replies)
Discussion started by: naveen.dasu
7 Replies

4. UNIX for Dummies Questions & Answers

Email ids trucated in Mailx function

I wanted to send email to list of people using mailx in unix. I am getting the emailds from a oracle table and getting the ids in a variable. Shell script is shown below: ----------------------------------------------------------------------- filename=testdata921 export filename... (5 Replies)
Discussion started by: sasi02
5 Replies

5. Red Hat

how to call a particular function from one shell another shell script

please help me in this script shell script :1 *********** >cat file1.sh #!/bin/bash echo "this is first file" function var() { a=10 b=11 } function var_1() { c=12 d=13 (2 Replies)
Discussion started by: ponmuthu
2 Replies

6. Shell Programming and Scripting

Function in Shell script

Legends, Can you please debug, what's wrong with the below code. I am gettng unexpected token error RebuldPF() ( #Changing the directory to data directory where the pf exists. cd /home/sandeep/files #Listing the names of the Pricefiles for rebuilding echo "The following pricefiles will... (6 Replies)
Discussion started by: sdosanjh
6 Replies

7. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

8. Shell Programming and Scripting

shell function help

SUN os KSH ERROR_CHK () { ERR_DATA="$ABC" if `echo "$ABC" | egrep 'PLS|ERROR|ORA'` ; then echo "Failed: Can't execute procedure ${PRG_NAME} ERROR found " return 1 else echo "Info : ${PRG_NAME} done" fi } my function fails iwth below message "ERROR: not found" I think i... (0 Replies)
Discussion started by: Amresh Dubey
0 Replies

9. UNIX for Advanced & Expert Users

how to see function in shell

Hi, is there any way we can see user defined functions in a shell? thanks in advance, -Ashish (2 Replies)
Discussion started by: shriashishpatil
2 Replies

10. Shell Programming and Scripting

getting the value of a c function in shell script

Let say there is a module fileselection module written in c language which returns the file name. Is it possible to get the file name from the file selection module directly, I mean can we call a c function directly in shell script without doing executable. If possible then how it can be... (1 Reply)
Discussion started by: surjyap
1 Replies
Login or Register to Ask a Question