Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check wget's return value

I'm writing a script to download a bunch of files, and I want it to inform when a particular file doesn't exist.

r=`wget -q www.someurl.com` if [ $r -ne 0 ]   then echo "Not there"   else echo "OK" fi 

But it gives the following error on execution:

./file: line 2: [: -ne: unary operator expected

What's wrong?

like image 441
Igor Avatar asked Apr 26 '10 22:04

Igor


People also ask

How do I know if my wget is successful?

If the download is successful without any errors wget will return 0 . Anything else indicates something went wrong. Take a look at the "Exit status" section of man wget .

How do I check my wget error?

Check `wget` command is installed or not Run the following command to check the installed version of `wget` command. If the command is not installed before then you will get the error, “ –bash:wget:Command not found”. The following output shows that wget command of version 1.19.


1 Answers

Others have correctly posted that you can use $? to get the most recent exit code:

wget_output=$(wget -q "$URL") if [ $? -ne 0 ]; then     ... 

This lets you capture both the stdout and the exit code. If you don't actually care what it prints, you can just test it directly:

if wget -q "$URL"; then     ... 

And if you want to suppress the output:

if wget -q "$URL" > /dev/null; then     ... 
like image 191
Cascabel Avatar answered Sep 20 '22 05:09

Cascabel