How to call a function (defined in shell script) i

2019-05-05 07:18发布

问题:

I have two scripts, namely shell_script.sh and perl_script.pl.

shell_script.sh : It has function definitions which, when invoked from Perl script, will execute certain commands on Linux in batch mode.

perl_script.pl : It has the code and logic to be implemented, which functions to invoke etc.

The content of shell_script.sh file is as under:

bash-4.2$ cat shell_script.sh

#!/bin/bash

# Function Definitions
func_1 ()
{
  echo "function definition"
}

func_2 ()
{
  echo "function definition"
}

The content of perl_script.pl file is as under:

bash-4.2$ cat perl_script.pl

#!/usr/bin/perl

use Getopt::Long;

my $var1;
my $var1;

GetOptions("var1=s"     => \$var1,
       "var2=s" => \$var2,
       "help|?" => \$help );

if ($help)
{
    HelpMenu();
}

print " Can I call the func_1 (defined in shell script) with arguments here..?? (in this perl script)";

How can i invoke the function func_1() (which is defined in shell_script.sh) in the perl script perl_script.pl ?

回答1:

To invoke a shell function, the shell needs to know its definition. One way to achieve that is to have the shell first source the file which defines the function. And then, without exiting the shell, call the function. From a Perl script, that would be for example:

system 'bash', '-c', 'source shell_script.sh; func_1';


回答2:

To use bash functions you need to be in bash. So in a Perl script that puts you inside backticks or system, where you are inside a bash process. Then, inside that process, you can source the script with functions, what will bring them in, and execute them

funcs.sh

#!/bin/bash

function f1 {
    t=$1
    u=$2
    echo "f1: t=$t u=$u"
}

function f2 {
    t=$1
    u=$2
    echo "f2: t=$t u=$u"
}

and in Perl (one-liner)

perl -wE'
    @r = qx(source funcs.sh; f1 a1 a2; f2 b1 b2); 
    print "got: $_" for @r
'

where qx is the same as backticks but perhaps clearer. I use backticks in case you need return from those functions. If your /bin/sh isn't linked to bash then call bash explicitly

perl -wE'
    @r = qx(/bin/bash -c "source funcs.sh; f1 a1 a2; f2 b1 b2"); 
    print "got: $_" for @r
'

Assignment to an array puts qx in list context, in which it returns the STDOUT of what it ran as a list of lines. This can be used to separate return from different functions, if they return a single line each. The a1,a2 and b1,b2 are arguments passed to f1 and f2.

Prints

got: f1: t=a1 u=a2
got: f2: t=b1 u=b2

This makes some (reasonable) assumptions.

If there is no need for the return, but the functions only need to do their thing, you can use

system('/bin/bash', '-c', 'source ... ') 

as in Håkon's answer


†   It is /bin/sh really, but that is often relegated to bash. Check your system (/bin/sh is likely a link to another shell). Or ensure that bash runs the command

my @ret = qx( /bin/bash -c "source base.sh; f1 a1 a2; f2 b1 b2" );

See text for the explanation of this example.



回答3:

It's overkill for this problem, but Env::Modify provides a way to make shell functions available in Perl. With Env::Modify, you could, say, import the shell functions once and use them over and over again in subsequence system calls.

use Env::Modify qw(:bash system source qx);

# import the shell functions
system("source shell_script.sh ; export -f func_1 func_2");
# alternate: put  "export -f func_1 func_2"  at the end of shell_script.sh and call
source("shell_script.sh");

# use the shell functions
system("func_1");
$output = `func_2`;
...