如何在 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/