使用反引號,系統調用僅將wget數據顯示在屏幕上。如何將wget的輸出存儲到變量中
我想要做的就是將wget中的信息「管道化」爲字符串或數組而不是屏幕。
下面是我的代碼片段。
sub wgetFunct {
my $page = `wget -O - "$wgetVal"`;
while (<INPUT>) {
#line by line operations
}
}
使用反引號,系統調用僅將wget數據顯示在屏幕上。如何將wget的輸出存儲到變量中
我想要做的就是將wget中的信息「管道化」爲字符串或數組而不是屏幕。
下面是我的代碼片段。
sub wgetFunct {
my $page = `wget -O - "$wgetVal"`;
while (<INPUT>) {
#line by line operations
}
}
您可以運行任何操作系統命令(我指的僅限Linux)和捕捉輸出/錯誤如下:
open (CMDOUT,"wget some_thing 2>&1 |");
while (my $line = <CMDOUT>)
{
### do something with each line of hte command output/eror;
}
編輯看完OP的評論後:
任何方式沒有wget信息打印到標準輸出?
下面的代碼會下載文件,而無需繳納任何屏幕:
#!/usr/bin/perl -w
use strict;
open (CMDOUT,"wget ftp://ftp.redhat.com/pub/redhat/jpp/6.0.0/en/source/MD5SUM 2>&1 |");
while (my $line = <CMDOUT>)
{
;
}
參考perlipc以獲取更多信息。
與管道開:
open my $input, "-|", "wget -O - $wgetVal 2>/dev/null";
while (<$input>) {
print "Line $_";
}
close $input;
檢查連接字符串:
open my $input, "-|", "wget -O - $wgetVal 2>&1";
while (<$input>) {
print "Good\n" and last if /Connecting to.*connected/;
}
close $input;
「LWP」有什麼問題? – squiguy 2013-05-14 05:57:27
@squiguy:如果網站已連接,我正在查找信息。 wget在其可以解析的信息塊中返回單詞「connected」。 – com 2013-05-14 07:17:16
相信與否:LWP確實有錯誤處理。 – innaM 2013-05-14 07:34:23