tags:

views:

377

answers:

2

Hi,

Is is possible to prevent Wget from making an output file when there is an error like 404.

When I run

wget -O my.html http://sdfsdfdsf.sdfds

http://sdfsdfdsf.sdfds does not exist but Wget still creates my.html


I am making a bash script and want to make sure it stops if wget can't get a valid file.

+4  A: 

wget returns a non-zero response for non-200 replies (it seems).

This sample code worked for me with GNU wget:

#!/bin/sh

wget -O my.html http://sdfsdfdsf.sdfds

if [ "$?" -ne "0" ]; then
    echo "ERROR"
fi

Here's more info about $? from here.

$? the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.

seth
Thanks for your reply. What does "$?" mean?
Alex L
I've updated my answer with more info about $?. Hope that helps.
seth
+1  A: 

You could perhaps use curl(1) instead:

curl -s -f -o my.html http://sdfsdfdsf.sdfds
goldfish