Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从perl脚本调用shell子例程_Perl_Shell - Fatal编程技术网

如何从perl脚本调用shell子例程

如何从perl脚本调用shell子例程,perl,shell,Perl,Shell,我有一个shell脚本,其中编写了两个子例程,它们接受参数,按摩该参数,然后提供新值作为输出 此脚本由许多其他shell脚本使用。现在我有了一个Perl脚本,我想用参数调用这些shell子例程。下面是我的代码: #! /bin/sh extract_data() { arg1 = $1 // massage data code output=massaged data return 0 } Perl脚本: my $output = `source /path/my_shell

我有一个shell脚本,其中编写了两个子例程,它们接受参数,按摩该参数,然后提供新值作为输出

此脚本由许多其他shell脚本使用。现在我有了一个Perl脚本,我想用参数调用这些shell子例程。下面是我的代码:

#! /bin/sh
extract_data()
{
  arg1 = $1
  // massage data code
  output=massaged data 
  return 0
}
Perl脚本:

my $output = `source /path/my_shell-script.sh; extract_data arg1`;
my $output = `source /path/my_shell-script.sh; extract_data arg1; print_output`;
my $output = `source /path/my_shell-script.sh; extract_data_verbose arg1`;
所以这里我想把shell子例程的输出变量的值赋给Perl脚本的输出变量。但我发现,只有当我在shell子例程(如下所示)中回显该输出变量时,它才可用。

#! /bin/sh
extract_data()
{
arg1 = $1
// massage data code
output=massaged data
echo $output 
}
我不想回显输出变量,因为它是敏感数据&将在其他shell脚本的日志中公开

请告知

my $output = `source /path/my_shell-script.sh; extract_data arg1; echo \$output`;
您的shell函数正在shell中设置一个变量。但当shell退出时,该变量将丢失。您需要将它打印到stdout,Perl的backticks正在捕获它


您的shell函数正在shell中设置一个变量。但当shell退出时,该变量将丢失。您需要将它打印到stdout,Perl的backticks正在捕获它

使用时,如果没有任何文件描述符(默认为stdout、stderr和stdin),就不可能在两个进程之间进行交互 系统调用,或者甚至调用open2/
open3
调用

但是,如果问题只是您不想破坏shell脚本的代码,那么您可以用shell中的另一个函数包装它,并从perl调用它。 您可以这样做:

将代码添加到my_shell-script.sh:

print_output()
{
  echo $output
}
以及您的perl脚本:

my $output = `source /path/my_shell-script.sh; extract_data arg1`;
my $output = `source /path/my_shell-script.sh; extract_data arg1; print_output`;
my $output = `source /path/my_shell-script.sh; extract_data_verbose arg1`;

但这太可怕了。不要使用全局变量。这可能是一个很好的扣球。但是最好使用
return

当您使用 系统调用,或者甚至调用open2/
open3
调用

但是,如果问题只是您不想破坏shell脚本的代码,那么您可以用shell中的另一个函数包装它,并从perl调用它。 您可以这样做:

将代码添加到my_shell-script.sh:

print_output()
{
  echo $output
}
以及您的perl脚本:

my $output = `source /path/my_shell-script.sh; extract_data arg1`;
my $output = `source /path/my_shell-script.sh; extract_data arg1; print_output`;
my $output = `source /path/my_shell-script.sh; extract_data_verbose arg1`;

但这太可怕了。不要使用全局变量。这可能是一个很好的扣球。但是最好使用
return

当Perl调用my_shell-script.sh时,而不是当另一个脚本调用它时,您希望有一个回音。脚本必须决定是否由Perl调用。
两种方法是:

  • 使用参数/选项告知脚本必须输出结果
  • 让my_shell-script.sh检查父进程,并在父进程为perl时回显
  • 由于您直接调用函数,因此可以采用另一种解决方案。确保原始函数名在没有回显的情况下工作,这样就不需要编辑其他脚本

    #!/bin/sh
    extract_data_process()
       arg1=$1
       // massage data code
       output=massaged data
       echo $output 
    }
    
    extract_data()
    {
       extract_data_process $1 >/dev/null 2>&1
       return 0
    }
    
    extract_data_verbose()
    {
       output=$(extract_data_process $1 2>&1)
       echo "${output}"
       return 0
    }
    
    Perl脚本:

    my $output = `source /path/my_shell-script.sh; extract_data arg1`;
    
    my $output = `source /path/my_shell-script.sh; extract_data arg1; print_output`;
    
    my $output = `source /path/my_shell-script.sh; extract_data_verbose arg1`;
    

    您希望在Perl调用my_shell-script.sh而不是其他脚本调用my_shell-script.sh时有回音。脚本必须决定是否由Perl调用。
    两种方法是:

  • 使用参数/选项告知脚本必须输出结果
  • 让my_shell-script.sh检查父进程,并在父进程为perl时回显
  • 由于您直接调用函数,因此可以采用另一种解决方案。确保原始函数名在没有回显的情况下工作,这样就不需要编辑其他脚本

    #!/bin/sh
    extract_data_process()
       arg1=$1
       // massage data code
       output=massaged data
       echo $output 
    }
    
    extract_data()
    {
       extract_data_process $1 >/dev/null 2>&1
       return 0
    }
    
    extract_data_verbose()
    {
       output=$(extract_data_process $1 2>&1)
       echo "${output}"
       return 0
    }
    
    Perl脚本:

    my $output = `source /path/my_shell-script.sh; extract_data arg1`;
    
    my $output = `source /path/my_shell-script.sh; extract_data arg1; print_output`;
    
    my $output = `source /path/my_shell-script.sh; extract_data_verbose arg1`;
    

    它给出了以下错误,因为perl脚本无法从shell脚本中识别输出变量。全局符号“$output”在may.pl第35行需要显式的包名。很抱歉,忘记了一个反斜杠。由于perl脚本无法从shell脚本中识别输出变量,因此出现以下错误。全局符号“$output”要求在may.pl第35行显示包名。很抱歉,忘记使用反斜杠。