我有一个 perl 代码,我在其中使用反引号执行一些 bash 命令。我想在该 bash 命令中读取 perl 数组。我的数组有一些字符串,我想在 bash 的 for 循环中读取它们。
my @aArray = (1,2,3,4);
my $command = 'for i in $@aArray; do xxxxx $i; done;';
`$command`
如果循环的任何部分失败,我还想捕获错误。谢谢
最佳答案
正如 @chepner 所建议的,您想要的代码看起来有点像这样:
my @array = (1, 2, 3, 4);
for my $val (@array) {
# Pick your favourite/the most appropriate mechanism for making system calls
system("command", $val);
}
如果您需要在远程系统上进行一次调用,您可以这样做:
my @array = (1, 2, 3, 4);
my $command = "for i in ("
for my $val(@array) {
$val =~ s/(?<!\\) /\\ /g; # Escape spaces that haven't already been (if the array elements might contain them)
$command = "$command $val";
}
$command = $command."); do <command> $i; done;";
system($command);
关于linux - 在 bash 中访问 Perl 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38547307/