【问题标题】:Check wget's return value检查 wget 的返回值
【发布时间】:2010-04-26 22:16:16
【问题描述】:

我正在编写一个脚本来下载一堆文件,我希望它在某个特定文件不存在时通知它。

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

但它在执行时会出现以下错误:

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

怎么了?

【问题讨论】:

    标签: bash if-statement wget


    【解决方案1】:

    其他人已正确发布您可以使用$? 获取最新退出代码:

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

    这使您可以同时捕获标准输出和退出代码。如果您实际上并不关心它打印的内容,则可以直接对其进行测试:

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

    如果你想抑制输出:

    if wget -q "$URL" > /dev/null; then
        ...
    

    【讨论】:

    • 使用这种形式时if会得到程序的退出码是标准的吗? (if <program>; then...)
    【解决方案2】:

    $r 是 wget 的文本输出(您已使用反引号捕获)。要访问返回码,请使用$? 变量。

    【讨论】:

    • 虽然这是正确的,但更好的解释是为什么$r 为空白以及为什么会出现错误消息。
    【解决方案3】:

    $r 为空,因此您的条件变为if [ -ne 0 ],似乎-ne 被用作一元运算符。试试这个:

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

    编辑正如 Andrew 在我之前解释的那样,反引号返回标准输出,而 $? 返回最后一个操作的退出代码。

    【讨论】:

    • 这是解释提问者为何收到该错误消息的唯一答案。 +1。
    【解决方案4】:

    你可以

    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] --> 
    

    【讨论】:

    • 不鼓励仅使用代码回答。请写一些关于它是如何工作的解释?
    • 它是如何工作的?
    【解决方案5】:

    从 wget 捕获结果并检查调用状态的最佳方法

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

    这样您可以检查 wget 的状态以及存储输出数据。

    1. 如果调用成功,使用存储的输出

    2. 否则会退出并报错 wget failed

    【讨论】:

      【解决方案6】:

      我没有幸运地尝试了所有的解决方案。

      wget 以非交互方式执行。这意味着 wget 在后台工作,您无法使用 $? 捕获返回码。

      一个解决方案是处理“--server-response”属性,搜索http 200状态码 示例:

      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
      

      注意:wget 需要一些时间来完成他的工作,因此我设置了“sleep 5”。这不是最好的方法,但可以很好地测试解决方案。

      【讨论】:

        猜你喜欢
        • 2016-12-29
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 2016-08-27
        相关资源
        最近更新 更多