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

查看:691
本文介绍了如何在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)

我想使用

 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。使用列表变体=nofollow noreferrer> system() 以减少您的引用的复杂性:

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天全站免登陆