wget to print errors, but nothing otherwise

Solution 1:

There are very good answers in this question, be sure to check them out, but what I've done is this:

wget [wget options] 2>&1 | grep -i "failed\|error"

Solution 2:

Use curl, no point guessing what every error will look like.

[wizard@laptop ~] curl -s -S http://www.google.coccm/ > /dev/null && echo "TRUE"
curl: (6) Couldn't resolve host 'www.google.coccm'
[wizard@laptop ~]$ curl -s -S http://www.google.com/ > /dev/null && echo "TRUE"
TRUE

-s/--silent

Silent mode. Don’t show progress meter or error messages. Makes Curl mute.

-S/--show-error

When used with -s it makes curl show error message if it fails.

And if you need stderr on stdout for some reason.

curl -s -S http://www.google.coccm/  2>&1 1> /dev/null

Solution 3:

I don't see an option for that. Do you need to know what the error is, or just if it happened? If you happen to just need to know if there was error, you can use the exit status.

if ! wget -o /dev/null www.google.com/flasfsdfsdf; then
    echo 'Oops!'
fi

Or, maybe:

if ! wget -o logfile www.google.com/flasfsdfsdf; then
    cat logfile
fi

And you can change the cat to a grep command if you want to get fancy...


Solution 4:

OUT=`wget --no-verbose -O /tmp/a http://example.com/ 2>&1` || echo $OUT

works. But it always truncates the output file, which you may or may not want.

Curl is better:

curl --fail --silent --show-error -o /tmp/a http://example.com

In case of an error it does not modify the output file.


Solution 5:

Redirect standard output to /dev/null, but keep the error output in your choice of shell.

In bash this would be:

wget [wget options] > /dev/null

Edit: So wget misbehaves. If all errors contain the word "error" in them you could pipe to grep

wget [wget options] 2>&1 | grep -i "error"

Tags:

Wget