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:
Using functions from shell script

 



brunogon
New User

Dec 16, 2011, 1:59 AM

Post #1 of 10 (8289 views)
Using functions from shell script Can't Post

Hello,

I have a shell script file, that won't be ported to perl as it is used by several other scripts. I'm just wondering if it is possible to "import"(execute) the functions of this file to my perl script.

Thanks


wickedxter
User

Dec 16, 2011, 4:00 AM

Post #2 of 10 (8283 views)
Re: [brunogon] Using functions from shell script [In reply to] Can't Post

perl has system function that can call external pergrams form inside the program..

http://perldoc.perl.org/functions/system.html


brunogon
New User

Dec 16, 2011, 4:21 AM

Post #3 of 10 (8280 views)
Re: [wickedxter] Using functions from shell script [In reply to] Can't Post

thanks for the answer, but my problem is not to run a command.
The question is if I have a function "foo" implemented on shell script. And I want to call foo on my perl program, is it possible to do it?


FishMonger
Veteran / Moderator

Dec 16, 2011, 7:30 AM

Post #4 of 10 (8275 views)
Re: [brunogon] Using functions from shell script [In reply to] Can't Post

Perl can execute shell commands and shell scripts, but can't execute functions inside shell scripts.

Either design your shell script to execute the function and have Perl execute that shell script or IMO a better solution would be to write a Perl subroutine that accomplishes the same thing as the shell function.


rovf
Veteran

Dec 20, 2011, 8:31 AM

Post #5 of 10 (7802 views)
Re: [brunogon] Using functions from shell script [In reply to] Can't Post

The only "foreign" programming language, where function can be linked to Perl, is C.

As far I understand, the parrot project allows possibly a (Parrot-)Perl-Program to be linked to a program in another language, but I doubt that we will ever see a Parrot-Implementation of bash, zsh, ksh (or whatever shell language you are using).


saurabhsharma
Novice

Jan 12, 2012, 10:58 PM

Post #6 of 10 (7469 views)
Re: [brunogon] Using functions from shell script [In reply to] Can't Post

Yes.

Assuming that shell script just defines the function(s) and does not do stuff you do not want done. You source shell script inside perl script then call the function that lives in shell script.


FishMonger
Veteran / Moderator

Jan 13, 2012, 6:54 AM

Post #7 of 10 (7458 views)
Re: [saurabhsharma] Using functions from shell script [In reply to] Can't Post

I don't believe that is true.

Please provide a working example that accomplishes what you claim.


saurabhsharma
Novice

Jan 16, 2012, 3:15 AM

Post #8 of 10 (7392 views)
Re: [FishMonger] Using functions from shell script [In reply to] Can't Post

Hello FishMonger,

This can be achieved using perl module Shell::Source Smile

I have also done a work around for this problem.
Lets say if you want to call a function written inside a shell script (first.sh) from a perl script, you can write a wrapper shell script to achieve the same.

e.g.
there is a function fun1() in (first.sh) which you want to call from a perl script (myperltest.pl)

first.sh

#!/bin/bash
fun1() {
arg1="${1}"
echo "Argument passed to the function fun1 in first.sh::fun1 arg1->[${arg1}]"
}

Write a wrapper script (wrapper.sh) to source first.sh and call the function fun1().

wrapper.sh
#! /bin/bash
. /path_to_your_first_script/first.sh
fun1 "saurabh sharma"

Now you can write your perl code using wrapper script to call the function

myperltest.pl
#!/usr/bin/perl
use strict;
my $path_to_wrapper_script ="/path_to_wrapper_script/wrapper.sh";
system("sh","$path_to_wrapper_script");


Cheers,

Saurabh


FishMonger
Veteran / Moderator

Jan 16, 2012, 7:37 AM

Post #9 of 10 (7389 views)
Re: [saurabhsharma] Using functions from shell script [In reply to] Can't Post


Quote
This can be achieved using perl module Shell::Source

No it can't. That module allows you to execute a shell script and then import the environment changes that the shell script setup. It does not give the ability for perl to directly execute a shell function, as if it were a perl subroutine, which is what the OP was wanting to achive.

Your example code is completely different from what the OP was asking, and in fact, it doesn't even do what you claimed.

Quote
You source shell script inside perl script then call the function that lives in shell script.


In your example it's the shell wrapper script which is sourcing the script and calling/executing the function, not the perl script. The perl script is simply using its system function to execute a shell script as a child process, which is what we already said it can do.


budman
User

Feb 12, 2012, 12:25 PM

Post #10 of 10 (6929 views)
Re: [brunogon] Using functions from shell script [In reply to] Can't Post

 
The only way you can access the functions is if they are sourced to environment of the system command:

script: functest.pl

Code
#!/usr/bin/perl 
my $cmd1 = "source functest.ksh; tester1 opt1 opt2";
my $cmd2 = "source functest.ksh; tester2 opt4 opt5";
system($cmd1);
system($cmd2);


script: functest.ksh

Code
#!/bin/ksh 
function tester1 {
echo "Running tester1 function $*"
}
function tester2 {
echo "Running tester2 function $*"
}


Output:
./functest.pl
Running tester1 function opt1 opt2
Running tester2 function opt4 opt5


(This post was edited by budman on Feb 12, 2012, 12:25 PM)

 
 


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

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