bashif-statementwget

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?


Solution

  • $r is the text output of wget (which you've captured with backticks). To access the return code, use the $? variable.