checking wget's return value [if]

前端 未结 6 1750
离开以前
离开以前 2020-12-09 15:18

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          


        
6条回答
  •  执念已碎
    2020-12-09 15:38

    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.

提交回复
热议问题