gpt4 book ai didi

linux - 在 bash 中访问 Perl 数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:13 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com