Wget creates output file even when page does not exist
Question
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.
Solution
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.
OTHER TIPS
You could perhaps use curl(1) instead:
curl -s -f -o my.html http://sdfsdfdsf.sdfds