【问题标题】:why can't perl's CaptureOutput::capture_exec_combined run "source"为什么 perl 的 CaptureOutput::capture_exec_combined 不能运行“source”
【发布时间】:2018-01-28 22:41:06
【问题描述】:

使用 CaptureOutput::capture_exec_combined 在 linux 上运行 perl 脚本。它似乎不想执行“源”

#!/usr/bin/env perl
use IO::CaptureOutput qw/capture_exec_combined/;
$cmd = "source test_capout.csh";
my ($stdouterr, $success, $exit_code) = capture_exec_combined($cmd);
print  "${stdouterr}\n";

(test_capout.csh 只是回显一条消息)

我明白了……

无法执行“源”:/tool/pandora64/.package/perl-5.18.2-gcc481/lib/site_perl/5.18.2/IO/CaptureOutput.pm 第 84 行没有这样的文件或目录。

【问题讨论】:

  • source 是内置的 shell 命令,不是可执行程序

标签: perl capture-output


【解决方案1】:

source 导致指定的脚本由给定source 命令的shell 执行。在 shell 之外使用 source 是没有意义的,这就是为什么它不是程序而是内置 shell 命令的原因。您需要生成一个 shell 并让 shell 执行命令。

capture_exec_combined('csh', '-c', 'source test_capout.csh');  # Hardcoded
  -or-
capture_exec_combined('csh', '-c', 'source "$1"', $script);    # Variable

当然,由于shell是之后退出的,所以可以简化为

capture_exec_combined('csh', 'test_capout.csh');        # Hardcoded
  -or-
capture_exec_combined('csh', $script =~ s{^-}{./-}r);   # Variable

【讨论】:

  • @ysth,哎呀,当我改用牙套时它被吃掉了。固定
  • 'test_capout.csh' 脚本被“获取”的原因是让它设置的环境变量之类的东西在当前的 shell 中持续存在。如果我没记错的话,使用 csh 运行 test_capout.csh 不会有同样的效果。
  • “当前shell”是perl。对于 Perl,source PATH 的等价物是 do PATH。当然,您需要将 csh 命令转换为 Perl 语句。
猜你喜欢
  • 2013-01-27
  • 2011-03-23
  • 2022-01-14
  • 1970-01-01
  • 2010-10-14
  • 2023-03-05
  • 2018-03-10
  • 1970-01-01
  • 2019-03-21
相关资源
最近更新 更多