简体   繁体   English

检查 wget 的返回值

[英]Check wget's return value

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?怎么了?

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

$r is the text output of wget (which you've captured with backticks). $r是 wget 的文本输出(您用反引号捕获了它)。 To access the return code, use the $?要访问返回代码,请使用$? variable.多变的。

$r is empty, and therefore your condition becomes if [ -ne 0 ] and it seems as if -ne is used as a unary operator. $r为空,因此您的条件变为if [ -ne 0 ]并且似乎-ne用作一元运算符。 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.返回上次操作的退出代码。

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

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否则它将退出并显示错误 wget failed

I been trying all the solutions without lucky.我一直在尝试所有的解决方案,但没有运气。

wget executes in non-interactive way. wget 以非交互方式执行。 This means that wget work in the background and you can't catch the return code with $?.这意味着 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 $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".注意:wget 需要一些时间来完成他的工作,因此我设置了“sleep 5”。 This is not the best way to do but worked ok for test the solution.这不是最好的方法,但可以很好地测试解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM