Alex L
Alex L

Reputation: 8449

Wget creates output file even when page does not exist

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.

Upvotes: 2

Views: 3177

Answers (2)

user138442
user138442

Reputation:

You could perhaps use curl(1) instead:

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

Upvotes: 3

seth
seth

Reputation: 37287

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.

Upvotes: 5

Related Questions