bash 检查 wget 的返回值 [if]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2717303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:14:26  来源:igfitidea点击:

checking wget's return value [if]

bashwget

提问by Igor

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

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

What's wrong?

怎么了?

回答by Cascabel

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

回答by nobody

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

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

回答by Bolo

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

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

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

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

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

回答by kSiR

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

回答by kazzikazzi

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 $?.

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

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

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

wget --server-response -q -o wgetOut http://www.someurl.com
sleep 5
_wgetHttpCode=`cat wgetOut | gawk '/HTTP/{ print  }'`
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.

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

回答by Anshul Sharma

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

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

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.

通过这种方式,您可以检查 wget 的状态以及存储输出数据。

  1. If call is successful use the output stored

  2. Otherwise it will exit with the error wget failed

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

  2. 否则它将退出并显示错误 wget failed