加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何在Perl脚本中调用函数(在shell脚本中定义)

发布时间:2020-12-14 01:35:35 所属栏目:Linux 来源:网络整理
导读:我有两个脚本,即 shell_script.sh和perl_script.pl. shell_script.sh:它有函数定义,当从Perl脚本调用时,它将以批处理模式在Linux上执行某些命令. perl_script.pl:它具有要实现的代码和逻辑,用于调用等. shell_script.sh文件的内容如下: bash-4.2$cat shel
我有两个脚本,即 shell_script.sh和perl_script.pl.

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

perl_script.pl:它具有要实现的代码和逻辑,用于调用等.

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中定义)?

解决方法

要调用shell函数,shell需要知道它的定义.实现这一目标的一种方法是让shell首先获取定义函数的文件.然后,在不退出shell的情况下,调用该函数.从Perl脚本,例如:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读