bash 如何使用 curl 检查文件是否下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37502839/
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-18 14:41:43  来源:igfitidea点击:

How to check if the file is downloaded with curl

linuxbashshellcurl

提问by ashishk

I am writing a shell script, I need to download 6 files from internet, In the script I have done it as

我正在编写一个 shell 脚本,我需要从互联网上下载 6 个文件,在脚本中我已经完成了

curl a
curl b
curl c
and so on

It works, but sometimes curl: (7) couldn't connect to hostfor some files in the middle of script for example, It will successfully download a but miss the file b with the above error and will download the file c. I would like to catch this error so that my code will execute with the successful download of all the files.

它可以工作,但有时curl: (7) couldn't connect to host对于脚本中间的某些文件,例如,它会成功下载a,但会因上述错误而错过文件b,并将下载文件c。我想捕获此错误,以便在成功下载所有文件后执行我的代码。

回答by Jahid

You can use a loop:

您可以使用循环:

while true; do
  curl --fail b && break
done

The loop won't break until b is downloaded. You can make it a retry function which you can call if a download fails on the first try:

在下载 b 之前循环不会中断。您可以将其设置为重试函数,如果第一次尝试下载失败,您可以调用该函数:

retry(){
    while true; do
      curl --fail "" && break ||
      echo "Download failed for url: 
            retrying..."
    done
}

Then do this:

然后这样做:

curl --fail a || retry a
curl --fail b || retry b
curl --fail c || retry c

If you just want to silent the error messages then:

如果您只想使错误消息静音,则:

curl a 2>/dev/null
curl b 2>/dev/null
...

Or if you want to just detect the error then:

或者,如果您只想检测错误,则:

if ! curl --fail a; then
  echo "Failed"
fi

or, a one liner:

或者,单班轮:

curl --fail a || echo Failed

If you want to exit after a failure and also show your own message:

如果您想在失败后退出并显示您自己的消息:

curl --fail a 2>/dev/null || { echo failed; exit 1; }

回答by Andrew White

You could chain them with &&...

你可以用&&...链接它们

curl --fail a && curl --fail b && curl --fail c...

Update: as @nwk pointed out below, we need to add --failto make curl fail on bad http codes.

更新:正如@nwk 在下面指出的,我们需要添加--fail以使 curl 在错误的 http 代码上失败。

回答by nwk

Put set -eat the beginning of the shell script and use curl --fail. E.g.,

放在set -eshell 脚本的开头并使用curl --fail. 例如,

#!/bin/sh
set -e
curl --fail http://example.com/a
curl --fail http://example.com/b
curl --fail http://example.com/c

The set -ewill make the script stop with an error on the first unsuccessful command (one with an exit status ≠ zero).

set -e将使脚本在第一个不成功的命令(退出状态≠零)上出现错误而停止。

回答by randominstanceOfLivingThing

You can check the status of execution of the last command by looking at the $?shell variable. Execute command below to check the status of the last command anything other 0 shall indicate an error.

您可以通过查看$?shell 变量来检查最后一条命令的执行状态。执行下面的命令以检查最后一个命令的状态,任何其他 0 表示错误。

echo $?

echo $?

curl a
if [ "$?" -gt 0 ]
then
echo "Error downloading file a. Exiting"
exit
fi 
curl b
if [ "$?" -gt 0 ]
then
echo "Error downloading file b. Exiting"
exit
fi 
...

A simple modified form after @andlrc suggestion:

@andlrc 建议后的简单修改形式:

if  ! curl a  ; then echo "Got error downloading a";  fi
if  ! curl b  ; then echo "Got error downloading b";  fi
if  ! curl c  ; then echo "Got error downloading c";  fi

回答by Andrew Wolfe

for file2get in a b c d; do do :; until curl --fail $file2get; done

对于 abcd 中的 file2get;渡渡鸟 :; 直到 curl --fail $file2get; 完毕

Or add an iterator counter to prevent endless looping

或者添加一个迭代器计数器来防止无限循环