stack_A stack_A - 16 days ago 8
Bash Question

analyse return value of wget command

I would like to analyse returned value of wget command.
I try those :

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


but I don't get the returned value when display GET variable

how to get returned value of wget

Answer

Your question is a little ambiguous. If you're asking "What is the exit code of the 'wget' process, that is accessible in the $? special variable."

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

If you're asking for the standard output of the 'wget' command, then what you're doing is going to give you that, although you have a typo in your first line (Add a closing parentheses after "conf.txt"). The problem is that wget doesn't put anything to stdout, by default. The progress bars and messages you see when you run wget interactively are actually going to stderr, which you can see by redirecting stderr to stdout using shell redirection 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]

If you're asking for the contents of the resource that wget received, then you need to instruct wget to send its output to stdout instead of a file. Depending on your flavor of wget, it's likely an option like -O or --output-document, and you can construct your command line as: wget -O - <url>. By convention the single dash (-) represents stdin and stdout in command line options, so you're telling wget to send its file to 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>