The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-07-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #2 (permalink)  
Old 12-07-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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?
  #3 (permalink)  
Old 12-07-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #4 (permalink)  
Old 12-07-2005
grasper grasper is offline
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.
  #5 (permalink)  
Old 12-07-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 08:52 PM..
  #6 (permalink)  
Old 12-07-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #7 (permalink)  
Old 12-08-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
Sponsored Links
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0