CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
calling shell script function from perl script

 



cesarNZ
Novice

Jan 24, 2011, 7:15 PM

Post #1 of 9 (1650 views)
calling shell script function from perl script Can't Post

hi all,

how do i call a function written in a shell script from a perl script ??



shell script function :




Code
  

doStuff()

{

username=$1
password=$2
envi=$3



does other stuff with the input arguments

}







perl script :




Code
  

use common; #this is where i have put the above shell script.

doStuff ('myname', 'passwrod', 'env');







i get this error : Undefined subroutine





thanks for your help.


BillKSmith
Veteran

Jan 25, 2011, 4:42 AM

Post #2 of 9 (1632 views)
Re: [cesarNZ] calling shell script function from perl script [In reply to] Can't Post

Use the system function (refer: perldoc -f system). This is very much the same as running the shell script from the command line.
Good Luck,
Bill


rovf
Veteran

Jan 26, 2011, 6:52 AM

Post #3 of 9 (1592 views)
Re: [cesarNZ] calling shell script function from perl script [In reply to] Can't Post

From the syntax you are using, I guess the shell script is supposed to be either Bourne Shell, or bash, or zsh. Right?

You need to create a wrapper shell program which sources the script where you define the shell function, and invokes this shell functions via the parameters passed from Perl to the wrapper script.

For invoking the wrapper script, you have several posibilities. The most common ones ares: system(...) [if you are not interested in the stdout/stderr of your execution], and qx(...) [if you want to process the script's stdout in your Perl program].


FishMonger
Veteran / Moderator

Jan 26, 2011, 7:11 AM

Post #4 of 9 (1590 views)
Re: [cesarNZ] calling shell script function from perl script [In reply to] Can't Post

Why not take the functionality of the shell script and write that functionality in the Perl script?


cesarNZ
Novice

Jan 26, 2011, 4:44 PM

Post #5 of 9 (1579 views)
Re: [FishMonger] calling shell script function from perl script [In reply to] Can't Post

thanks for the reply so far. i think its a matter of including the shell script which holds the common functionality into the path for the perl script. i tried the below but it still doesnt work :




Code
  

use lib "path to shell script";





any other suggestions ??



ta.


FishMonger
Veteran / Moderator

Jan 26, 2011, 4:53 PM

Post #6 of 9 (1577 views)
Re: [cesarNZ] calling shell script function from perl script [In reply to] Can't Post


In Reply To
thanks for the reply so far. i think its a matter of including the shell script which holds the common functionality into the path for the perl script. i tried the below but it still doesnt work :




Code
  

use lib "path to shell script";




any other suggestions ??



ta.


No, you can't load and use a shell script like you would do with a perl script or module.

The best approach would be to write a perl subroutine that will do what your shell subroutine does.

If you don't want to do that, then use one of Perl's methods of executing external programs (such as system(), qx() or backticks) and pass the needed parameters.


cesarNZ
Novice

Jan 26, 2011, 5:16 PM

Post #7 of 9 (1575 views)
Re: [FishMonger] calling shell script function from perl script [In reply to] Can't Post

but i have seen people call shell script functions from perl scripts by adding the shell script path to 'PERL5LIB ' variable. i just dont know why mine isnt working


FishMonger
Veteran / Moderator

Jan 26, 2011, 5:53 PM

Post #8 of 9 (1573 views)
Re: [cesarNZ] calling shell script function from perl script [In reply to] Can't Post


In Reply To
but i have seen people call shell script functions from perl scripts by adding the shell script path to 'PERL5LIB ' variable. i just dont know why mine isnt working


I believe you misunderstood what you've seen. Can you provide an example source?


cesarNZ
Novice

Jan 26, 2011, 6:23 PM

Post #9 of 9 (1571 views)
Re: [FishMonger] calling shell script function from perl script [In reply to] Can't Post

sorry my apologies. must have been the funny stuff i have been smoking which is making my vision blurry.



i saw a perl script being called by another perl script and mistook that for a shell !!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives