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

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

问题描述

如何使用庆典语法在Perl的系统()命令?

我有一个命令,是庆典专用的,例如下面,它使用的bash的进程替换:

 差异≤(ls -l命令)≤(LS -al)

我想从Perl中调用它,使用

 系统(差异≤(ls -l命令)≤(LS -al))

但是,因为它是用给我一个错误 SH 而不是庆典来执行命令:

  SH:-c:0行:语法错误附近意外的标记'('
SH:-c:0行:`排序≤(LS)'


解决方案

告诉Perl来直接调用的bash。使用列表 的变系统() 以减少你的报价的复杂性:

 我@args =(庆典,-c,差异≤(ls -l命令)≤(LS -al));
系统(@args);

您甚至可以定义一个子程序,如果你打算这样做往往不够:

 子system_bash {
  我@args =(庆典,-c,移);
  系统(@args);
}system_bash('回声$ SHELL');
system_bash('差异≤(ls -l命令)≤(LS -al)');

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

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

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

I would like to call it from Perl, using

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

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)'

解决方案

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)');

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

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