如何在 Perl 的 system() 中使用 bash 语法? [英] How can I use bash syntax in Perl's system()?

查看:25
本文介绍了如何在 Perl 的 system() 中使用 bash 语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Perl 的 system() 命令中使用 bash 语法?

How can I use bash syntax in Perl's system() command?

我有一个特定于 bash 的命令,例如以下使用 bash 的进程替换:

I have a command that is bash-specific, e.g. the following, which uses bash's process substitution:

 diff <(ls -l) <(ls -al)

我想从 Perl 调用它,使用

I would like to call it from Perl, using

 system("diff <(ls -l) <(ls -al)")

但它给了我一个错误,因为它使用 sh 而不是 bash 来执行命令:

but it gives me an error because it's using sh instead of bash to execute the command:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `sort <(ls)'

推荐答案

告诉 Perl 直接调用 bash.使用 system()<的 list 变体/a> 以降低引用的复杂性:

Tell Perl to invoke bash directly. Use the list variant of system() to reduce the complexity of your quoting:

my @args = ( "bash", "-c", "diff <(ls -l) <(ls -al)" );
system(@args);

如果您计划经常这样做,您甚至可以定义一个子程序:

You may even define a subroutine if you plan on doing this often enough:

sub system_bash {
  my @args = ( "bash", "-c", shift );
  system(@args);
}

system_bash('echo $SHELL');
system_bash('diff <(ls -l) <(ls -al)');

这篇关于如何在 Perl 的 system() 中使用 bash 语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆