0

我想分析 wget 命令的返回值。我尝试那些:

  GET=$(wget ftp://user:user@192.168.1.110/conf.txt  
  echo $GET
  GET=`wget ftp://user:user@192.168.1.110/conf.txt`  
  echo $GET

但是显示 GET 变量时我没有得到返回值

如何获取wget的返回值

4

2 回答 2

3

你的问题有点模棱两可。如果您要问“'wget' 进程的退出代码是什么,可以在$?特殊变量中访问。”

[~/tmp]$ wget www.google.foo
--2013-11-01 08:33:52--  http://www.google.foo/
Resolving www.google.foo... failed: nodename nor servname provided, or not known.
wget: unable to resolve host address ‘www.google.foo’
[~/tmp]$ echo $?
4

如果你要求'wget'命令的标准输出,那么你所做的就是给你那个,尽管你在第一行有一个错字(在“conf.txt”之后添加一个右括号) . 问题是 wget 默认情况下不会向标准输出添加任何内容。当您以交互方式运行 wget 时,您看到的进度条和消息实际上是转到 stderr,您可以通过使用 shell 重定向将 stderr 重定向到 stdout 来看到2>&1

[~/tmp]$ GET=`wget www.google.com 2>&1`
[~/tmp]$ echo $GET
--2013-11-01 08:36:23-- http://www.google.com/ Resolving www.google.com... 74.125.28.104, 74.125.28.99, 74.125.28.103, ... Connecting to www.google.com|74.125.28.104|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 18637 (18K) [text/html] Saving to: ‘index.html’ 0K .......... ........ 100% 2.72M=0.007s 2013-11-01 08:36:23 (2.72 MB/s) - ‘index.html’ saved [18637/18637]

如果您要询问 wget 收到的资源的内容,那么您需要指示 wget 将其输出发送到 stdout 而不是文件。根据您的 wget 风格,它可能是类似-Oor的选项--output-document,您可以将命令行构造为:wget -O - <url>. 按照惯例,单破折号 ( -) 代表命令行选项中的标准输入和标准输出,因此您告诉 wget 将其文件发送到标准输出。

[~/tmp]$ GET=`wget -O - www.google.com`
--2013-11-01 08:37:31--  http://www.google.com/
Resolving www.google.com... 74.125.28.104, 74.125.28.99, 74.125.28.103, ...
Connecting to www.google.com|74.125.28.104|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18621 (18K) [text/html]
Saving to: ‘STDOUT’

100%[=======================================>] 18,621      98.5KB/s   in 0.2s

2013-11-01 08:37:32 (98.5 KB/s) - written to stdout [18621/18621]
[~/tmp]$ echo $GET
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><head>
<snip lots of content>
于 2013-11-01T15:40:00.660 回答
-1

你可以得到退出代码

echo $?

执行命令后。但是,如果您想对工作/不工作的下载做出反应,您可以使用 if

if wget -q www.google.com
then
   echo "works"
else
   echo "doesn't work"
fi
于 2013-11-01T20:28:54.930 回答