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?
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 .
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With