[英]Perl: suppress output of backtick when file not found
在我的代碼中:
$status = `ls -l error*`;
它顯示輸出: ls *error No such file or directory.
我怎么能壓制這個消息。 我有興趣確定是否生成了錯誤文件。 如果是,我需要忽略文件列表(不打印消息)
通過運行它
$status = `ls -l error* 2> /dev/null`;
並將外部命令的輸出抑制為標准錯誤。
如果你只需要文件名(而不是ls
的-l
開關給你的所有其他信息),這可以在純Perl中用類似的語句完成
@files = glob("error*");
if (@files == 0) {
... there were no files ...
} else {
... do something with files ...
}
如果你確實需要從ls -l ...
獲得的所有其他信息,將builtin stat
函數應用於每個文件名可以為您提供相同的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.