在Shell脚本中定义

在Shell脚本中定义

本文介绍了如何在Perl脚本中调用函数(在Shell脚本中定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:具有要实现的代码和逻辑,要调用的功能等.

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

shell_script.sh文件的内容如下:


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

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

func_2 ()
{
  echo "function definition"
}

perl_script.pl文件的内容如下:


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)";

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

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

推荐答案

要调用外壳程序功能,外壳程序需要知道其定义.一种实现方法是让外壳程序首先source是定义函数的文件.然后,在不退出外壳的情况下,调用该函数.在Perl脚本中,例如:

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';

这篇关于如何在Perl脚本中调用函数(在Shell脚本中定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:58