【发布时间】:2018-10-19 00:34:29
【问题描述】:
我正在编写一个脚本,我将在 bash shell 上运行一个命令并打印出结果。 我会从文件 A.txt 中读取
A.txt
AAA:BBB
CCC:DDD
AAA:FFF
我的代码
my $file = /path/to/A.txt;
open(my $fh, '<', $file)
or die "Couldn't open file";
while (my $line = <$fh>){
my ($ins,$pre) = split /:/, $line;
if ((substr $ins,0,1) eq "A"){
$mo = "A/C/$ins";
}
elsif ((substr $ins,0,1) eq "C"){
$mo = "X/Y/$ins";
}
Env::modulecmd:load($mo);
print "Running command cat $pre\%";
$result = `cat '$pre'\% | head`;
Env:modulecmd:unload($mo);
print $result;
}
close $fh;
我遇到了一个奇怪的问题,打印命令只适用于最后一行,其余的它会在下一行打印 '%' 字符,但对于最后一行它完全按预期工作。
因此,$result 变量仅针对 A.txt 的最后一行进行正确评估,其余 2 行不提供任何输出。
谁能建议我在这里做错了什么。
感谢您的宝贵时间
【问题讨论】: