checking wget's return value [if]

2019-01-19 09:33发布

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?

标签: bash wget
6条回答
forever°为你锁心
2楼-- · 2019-01-19 10:06

$r is empty, and therefore your condition becomes if [ -ne 0 ] and it seems as if -ne is used as a unary operator. Try this instead:

wget -q www.someurl.com
if [ $? -ne 0 ]
  ...

EDIT As Andrew explained before me, backticks return standard output, while $? returns the exit code of the last operation.

查看更多
何必那么认真
3楼-- · 2019-01-19 10:15

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

查看更多
虎瘦雄心在
4楼-- · 2019-01-19 10:15

I been trying all the solutions without lucky.

wget executes in non-interactive way. This means that wget work in the background and you can't catch de return code with $?.

One solution it's to handle the "--server-response" property, searching http 200 status code Example:

wget --server-response -q -o wgetOut http://www.someurl.com
sleep 5
_wgetHttpCode=`cat wgetOut | gawk '/HTTP/{ print $2 }'`
if [ "$_wgetHttpCode" != "200" ]; then
    echo "[Error] `cat wgetOut`"
fi

Note: wget need some time to finish his work, for that reason I put "sleep 5". This is not the best way to do but worked ok for test the solution.

查看更多
干净又极端
5楼-- · 2019-01-19 10:19

you could just

wget ruffingthewitness.com && echo "WE GOT IT" || echo "Failure"

-(~)----------------------------------------------------------(07:30 Tue Apr 27)
risk@DockMaster [2024] --> wget ruffingthewitness.com && echo "WE GOT IT" || echo "Failure" 
--2010-04-27 07:30:56--  http://ruffingthewitness.com/
Resolving ruffingthewitness.com... 69.56.251.239
Connecting to ruffingthewitness.com|69.56.251.239|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `index.html.1'

    [ <=>                                                                                    ] 14,252      72.7K/s   in 0.2s    

2010-04-27 07:30:58 (72.7 KB/s) - `index.html.1' saved [14252]

WE GOT IT
-(~)-----------------------------------------------------------------------------------------------------------(07:30 Tue Apr 27)
risk@DockMaster [2025] --> wget ruffingthewitness.biz && echo "WE GOT IT" || echo "Failure"
--2010-04-27 07:31:05--  http://ruffingthewitness.biz/
Resolving ruffingthewitness.biz... failed: Name or service not known.
wget: unable to resolve host address `ruffingthewitness.biz'
zsh: exit 1     wget ruffingthewitness.biz
Failure
-(~)-----------------------------------------------------------------------------------------------------------(07:31 Tue Apr 27)
risk@DockMaster [2026] --> 
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-19 10:26

Best way to capture the result from wget and also check the call status

wget -O filename URL
if [[ $? -ne 0 ]]; then
    echo "wget failed"
    exit 1; 
fi

This way you can check the status of wget as well as store the output data.

  1. If call is successful use the output stored

  2. Otherwise it will exit with the error wget failed

查看更多
Viruses.
7楼-- · 2019-01-19 10:27

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
    ...
查看更多
登录 后发表回答