分析wget命令的返回值

时间:2013-11-01 15:24:33

标签: linux shell wget

我想分析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的返回值

2 个答案:

答案 0 :(得分: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。之后添加一个右括号)。文本”)。问题是默认情况下wget没有向stdout添加任何内容。以交互方式运行wget时看到的进度条和消息实际上是stderr,您可以通过使用shell重定向2>&1将stderr重定向到stdout来查看:

[~/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风格,它可能是-O--output-document之类的选项,您可以将命令行构建为:wget -O - <url>。按照惯例,单个破折号(-)表示命令行选项中的stdin和stdout,因此您告诉wget将其文件发送到stdout。

[~/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>

答案 1 :(得分:-1)

您可以使用

获取退出代码
echo $?

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

if wget -q www.google.com
then
   echo "works"
else
   echo "doesn't work"
fi
相关问题