Can I get the number of downloaded files with wget -r
and use that as a variable? I want to write a script that runs multiple wget commands in it (using -q so i can control output) and then at the end, add up the number of downloaded files and echo that to the user. Is this possible?
Bash – Use number of downloaded file in wget as variable in bash script
bashinternetscriptingwget
Best Answer
To build on Jed's suggestion and take it the rest of the way, you could do something like this:
Or, if you wanted to go a touch more complicated, but more reliable and robust, you could pull out the download count given as the summary by wget:
Update: Regarding the
2>&1
construct, for a *nix application, there are two default output streams,stdout
(file descriptor 1) andstderr
(file descriptor 2). For interactive commands, both are typically sent to the screen/terminal, so it can be hard to differentiate them. The2>&1
tells your bash to take the output sent tostderr
(fd 2) and combine it with the output forstdout
(fd 1). The result is a single output stream, which would look to the next tool in a pipe (as above) the same as the output looks to your screen.This is required because wget defaults to sending it's status information to
stderr
, notstdout
. If you ran the above without thestderr
redirection, you're sendingstdout
to the piped program, but that's all. You'd get all of wget's output sent to your screen (stderr
), and the variable would be empty, because thegrep
orawk
statements are processingstdout
(which got no output).To see what gets sent where for an application, you can do something like this:
Or just:
If you don't specify a number for output redirection, it defaults to
stdout
.The files
foo.err
andfoo.out
will contain the contents of whatever your commands end tostderr
andstdout
, respectively.Also worth noting, you can also use
|&
as your pipe command to combinestderr
withstdout
, as a shorthand for2>&1
.For a little more information on the command redirection and
stdin
,stdout
, andstderr
, see the bash man page, the stdin, stdout, and stderr man pages, and for historical reasons behindstdout
andstderr
, see this posting: http://jstorimer.com/2011/12/29/the-difference-between-stdout-and-stderr.html