【问题标题】:call the subroutine of the shell script from perl script and pass certain parameters to it从 perl 脚本调用 shell 脚本的子例程并将某些参数传递给它
【发布时间】:2013-10-29 12:44:34
【问题描述】:

如何调用 shell 脚本的子程序并传递某些参数给它?像下面这样的?

#!/usr/bin/perl
### need something like this
source file.sh
routine;            # <-- this is supposed to be part of file.sh which is called 
                    # from perl script and some parameter are passed to it

【问题讨论】:

  • 这可能是一个 XYProblem ...你确定你需要这样做 this 吗?您需要解决的潜在需求是什么?

标签: perl shell unix


【解决方案1】:

没有。它们是两种不同的语言。您所能做的就是从 Perl 调用一个 shell 脚本作为子进程,例如使用 system()qx()

用一种语言编写程序,Perl 或 shell,不要试图混合使用它们。

好的,可以从 shell 中导出一个函数,然后在 Perl 中解析和执行它,但是工作量很大,不安全,而且通常不值得。 p>

【讨论】:

    【解决方案2】:

    解决这个问题的方法是将例程的内容从 file.sh 隔离到 subfile.sh - 然后您可以在 Perl 中执行此操作:

    @cmdargs=('subfile.sh', $arg1, "arg2");
    system(@cmdargs);
    

    第一个列表元素是命令,第二个是作为参数传递给 subfile.sh 的 Perl 变量的值;第三个是作为参数传递给 subfile.sh 的文字。

    通过在 file.sh 中为 subfile.sh 编写一个包装器并使用适当的参数调用它,就像任何其他 shell 命令一样,无论您在哪里重新调用 file.sh 中的例程。

    我认为这会奏效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多