我想測試一個反向輸出字符串(它是一個字符串,對嗎?)包含一個子字符串。Perl索引函數不工作?
my $failedCoutner = 0;
my $tarOutput = `tar -tvzf $tgzFile`;
print "$tarOutput\n";
my $subStr = "Cannot open: No such file or directory";
if (index($tarOutput, $subStr) != -1)
{
push(@failedFiles, $tgzFile);
$failedCounter++;
print "Number of Failed Files: $failedCounter\n\n\n";
}
print "Number of Failed Files: $failedCounter\n\n\n";
但是這不起作用。它從不輸入if語句。
倒引號輸出:
tar (child): /backup/Arcsight/EDSSIM004: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
Number of Failed Files: 0
顯然子串是在第一行。爲什麼它不認識到這一點?
你確定輸出實際上是在'$ tarOutput'嗎?我的猜測是它實際上會變得更加stderr。嘗試添加像'print'這樣的tar輸出是[$ tarOutput] \ n「'來查看實際內容。 – friedo
打印像'$ tarOutput'這樣的值總是好事,以確保它們是你自己認爲的。 –
如果您需要從stdout和stderr中讀取,請嘗試[IPC :: Open3](https://metacpan.org/module/IPC::Open3)。 – friedo