perl - 我如何在 Perl 的 system() 中使用 bash 语法?

标签 perl bash command

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

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

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

我想从 Perl 调用它,使用

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

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

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

最佳答案

告诉 Perl 直接调用 bash。使用 system()list 变体降低报价的复杂性:

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

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

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

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

关于perl - 我如何在 Perl 的 system() 中使用 bash 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/571368/

相关文章:

linux - 使用 awk linux 操作文件

android - Bash sed 命令删除 Android 参数中匹配的所有行

bash - 只显示grep的第n个匹配项

terminal - 如何在终端中停止/取消“git log”命令?

java - 定位给定目录的命令

php - 如何在本地数据库中保存距离矩阵?

linux - 如何同时使用 Parallel::ForkManager 和 Capture::Tiny?

bash - MV 命令行不适用于某些文件 (OSX)

perl - move 文件时未定义的子例程

perl - 如何从 Perl 系统命令中查看错误?