Home: Perl Programming Help: Intermediate:
Using functions from shell script



brunogon
New User

Dec 16, 2011, 1:59 AM


Views: 11846
Using functions from shell script

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


Views: 11840
Re: [brunogon] Using functions from shell script

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


Views: 11837
Re: [wickedxter] Using functions from shell script

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


Views: 11832
Re: [brunogon] Using functions from shell script

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


Views: 11359
Re: [brunogon] Using functions from shell script

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


Views: 11026
Re: [brunogon] Using functions from shell script

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


Views: 11015
Re: [saurabhsharma] Using functions from shell script

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


Views: 10949
Re: [FishMonger] Using functions from shell script

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


Views: 10946
Re: [saurabhsharma] Using functions from shell script


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


Views: 10486
Re: [brunogon] Using functions from shell script

 
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)