【问题标题】:How to call a function (defined in shell script) in a Perl script如何在 Perl 脚本中调用函数(在 shell 脚本中定义)
【发布时间】:2018-12-02 21:48:22
【问题描述】:

我有两个脚本,分别是shell_script.shperl_script.pl

shell_script.sh : 它具有函数定义,当从 Perl 脚本调用时,将在 Linux 上以批处理模式执行某些命令。

perl_script.pl : 它有要实现的代码和逻辑,要调用的函数等。

shell_script.sh文件内容如下:

bash-4.2$ 猫 shell_script.sh
#!/bin/bash

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

func_2 ()
{
  echo "function definition"
}

perl_script.pl文件内容如下:

bash-4.2$ 猫 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)";

如何在 perl 脚本perl_script.pl 中调用函数func_1()(在shell_script.sh 中定义)?

【问题讨论】:

  • 您希望这些函数(实际上)从/在 Perl 脚本中做什么?将一些值返回给 Perl?还是只是做他们的事?
  • 您可能会调用定义和调用函数的脚本,但根据它的作用,最好将其重写为 Perl 函数。
  • 您可以从 perl 脚本中调用这样的函数:system 'bash', '-c', 'source shell_script.sh; func_1'

标签: linux shell perl unix external


【解决方案1】:

要调用一个shell函数,shell 需要知道它的定义。实现这一点的一种方法是让shell首先source定义函数的文件。然后,在不退出 shell 的情况下调用该函数。在 Perl 脚本中,例如:

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

【讨论】:

    【解决方案2】:

    要使用bash 函数,您需要在bash 中。因此,当您在 Perl 脚本中时,这会将您置于反引号或 system 中,而您在 bash 进程中。然后,在该进程中,您可以source 带有函数的脚本,将它们带入并执行它们

    funcs.sh

    #!/bin/bash
    
    function f1 {
        t1=$1
        u1=$2
        echo "f1: t1=$t1 u1=$u1"
    }
    
    function f2 {
        t2=$1
        u2=$2
        echo "f2: t2=$t2 u2=$u2"
    }
    

    在 Perl 中(单行)

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

    其中qx 是反引号的运算符,但可能更清晰。如果您需要从这些函数返回,我会使用反引号。如果您的 /bin/sh 未链接到 bash,请明确调用 bash

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

    对数组的赋值将qx 放在列表上下文中,在其中它返回它作为行列表运行的STDOUT。这可用于将不同函数的返回分开,如果它们每个返回一行。 a1,b1a2,b2 是传递给 f1f2 的参数。

    打印

    得到: f1: t1=a1 u1=b1 得到: f2: t2=a2 u2=b2

    这做出了一些(合理的)假设。

    如果不需要返回,而函数只需要做自己的事情,你可以使用

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

    Håkon's answer


      确实是/bin/sh,但通常归为bash。检查您的系统(/bin/sh 可能是指向另一个 shell 的链接)。或者确保bash 运行命令

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

    请参阅文本以了解此示例的说明。

    【讨论】:

      【解决方案3】:

      这个问题有点过头了,但是Env::Modify 提供了一种在 Perl 中使 shell 函数可用的方法。使用Env::Modify,您可以说,导入一次shell 函数,然后在子序列system 调用中一遍又一遍地使用它们。

      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`;
      ...
      

      【讨论】:

        猜你喜欢
        • 2020-04-30
        • 2012-03-08
        • 2015-10-15
        • 1970-01-01
        • 2022-01-04
        • 2014-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多