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
Calling a perl script from a perl script new2ss Shell Programming and Scripting 3 02-06-2007 07:17 PM
to kill a process in perl gurukottur Shell Programming and Scripting 2 11-20-2006 12:01 AM
My "Bread and Butter" Process Keep Alive Perl Script.... Neo Tips and Tutorials 0 01-08-2005 01:17 AM
PERL: wait for process to complete dangral Shell Programming and Scripting 2 04-21-2004 12:37 PM
Killing a process from perl script. sharuvman Shell Programming and Scripting 3 04-01-2004 09:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Perl: Run perl script in the current process

I have a question regarding running perl in the current process.

I shall demonstrate with an example.

Look at this.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.sh
#! /bin/sh
cd /etc
sh-2.05b$ ./test.sh 
sh-2.05b$ pwd
/tmp
sh-2.05b$ . ./test.sh 
sh-2.05b$ pwd
/etc
sh-2.05b$
So invoking ./test.sh spawns a sub-shell and runs the script. Whereas . ./test.sh will run the script in the current shell.

Now, how can I simulate the latter behavior with a perl script. I tried the following but it did not help.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.pl
#! /usr/bin/perl
chdir("/etc");
sh-2.05b$ ./test.pl 
sh-2.05b$ pwd
/tmp
sh-2.05b$ . ./test.pl 
sh: ./test.pl: line 2: syntax error near unexpected token `"/tmp"'
sh: ./test.pl: line 2: `chdir("/tmp");'
sh-2.05b$
Is there any other way of running the script to make sure that the changes made by the script will affect the current process ?

Thanks,
Vino
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-07-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,854
AFAIK perl always creates a child.

Is there a reason you can't read the other script and do what it does in the context of the current process? ie., find the chdir and then do what it does in your perl script?
Reply With Quote
  #3 (permalink)  
Old 12-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Quote:
Originally Posted by jim mcnamara
Is there a reason you can't read the other script and do what it does in the context of the current process?
Yes, it can be done that way. That would work.

I am curious to know if it can be done this way.

Thanks,
vino
Reply With Quote
  #4 (permalink)  
Old 12-07-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
Just a guess, but I think the . command is instructing the current shell to interpret the script - which it can't because it has perl commands/functions in it.
Reply With Quote
  #5 (permalink)  
Old 12-07-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,434
The "exec" builtin in bash (sh on Linux seems to be too) will run the perl process without creating a new process by replacing the original shell process, just like the C exec* family of functions.

So you ought to be able to run a script by "./script.sh" to create a new process and then "exec perl script.pl" to continue execution without introducing a new process, but the original shell script will terminate (control will not return to the shell script).

Last edited by cbkihong; 12-07-2005 at 04:52 PM.
Reply With Quote
  #6 (permalink)  
Old 12-07-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by cbkihong
The "exec" builtin in bash (sh on Linux seems to be too).....
On most (recent) Linux distros sh is soft/hard linked to bash...
Code:
# ls -l /bin/sh /bin/bash
-rwxr-xr-x  1 root root 616312 Dec  7  2004 /bin/bash
lrwxrwxrwx  1 root root      4 May 18  2005 /bin/sh -> bash
Reply With Quote
  #7 (permalink)  
Old 12-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Quote:
Originally Posted by cbkihong
The "exec" builtin in bash (sh on Linux seems to be too) will run the perl process without creating a new process by replacing the original shell process, just like the C exec* family of functions.

So you ought to be able to run a script by "./script.sh" to create a new process and then "exec perl script.pl" to continue execution without introducing a new process, but the original shell script will terminate (control will not return to the shell script).
Hmm...okie. So I used exec. This is what I got.

Code:
sh-2.05b$ pwd
/tmp
sh-2.05b$ cat test.sh
#! /bin/sh
exec perl /tmp/test.pl
sh-2.05b$ cat test.pl
chdir("/etc");
sh-2.05b$ ./test.sh
sh-2.05b$ pwd
/tmp
sh-2.05b$
I did a . ./test.sh and my xterm window just disappeared.

I changed test.pl to contain

Code:
#! usr/bin/perl
chdir("/etc");
and test.sh to
Code:
#! /bin/sh
exec /tmp/test.pl
The pwd still shows /tmp.

vino
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:44 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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0