linux - 在 bash 中访问 Perl 数组

标签 linux bash perl

我有一个 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/

相关文章:

c - 我正在尝试制作从文件字符中读取的递归函数,但它在 C 中不起作用

python - pyinotify 疑似线程问题

multithreading - 如何在 Perl 中实现信号量线程通信?

perl - 简单: How do doubles work in Perl?

javascript - 使用 CSS 选择器定位存储在 javascript 元素中的一些数据

linux - Bash:使用包含带转义空格的目录的变量的意外行为

linux - Docker - 如何检查 Dockerfile 中的 curl 命令是否有响应代码 200

linux遍历目录中的文件

linux - shell脚本中for循环的迭代

mysql - shell脚本将数据解析为变量然后更新数据库