我正在编写一个shell脚本,我需要从网上下载6个文件,在脚本中我已经完成了
curl a curl b curl c and so on
它工作,但有时curl: (7) Couldn't connect to host的一些文件在脚本中,例如,它会成功下载一个错过文件B与上述错误,并将下载文件C。 我想赶上这个错误,以便我的代码将执行与所有文件的成功下载。
在x秒后停止命令而不使用超时
Bash:多redirect
bashstring有/无引号和单/双引号
如何在/ etc / environment中正确设置JAVA_HOME
Whiptail Gauge:循环中的variables未被设置
你可以链接&& …
curl --fail a && curl --fail b && curl --fail c...
更新 :正如@nwk指出的那样,我们需要添加--fail来使curl在坏的http代码上失败。
把set -e放在shell脚本的开头,并使用curl --fail 。 例如,
#!/bin/sh set -e curl --fail http://example.com/a curl --fail http://example.com/b curl --fail http://example.com/c
set -e会使脚本在第一个不成功的命令(一个退出状态≠零)上停止,并出现错误。
你可以使用一个循环:
while true; do curl --fail b && break done
直到b被下载,循环才会中断。 如果第一次尝试下载失败,您可以将其设置为可以调用的重试功能:
retry(){ while true; do curl --fail "$1" && break || echo "Download Failed for url: $1 retrying..." done }
然后这样做:
curl --fail a || retry a curl --fail b || retry b curl --fail c || retry c
curl a 2>/dev/null curl b 2>/dev/null ...
或者,如果你只想检测错误,那么:
if ! curl --fail a; then echo "Failed" fi
或者,一个班轮:
curl --fail a || echo Failed
curl --fail a 2>/dev/null || { echo Failed; exit 1; }
您可以通过查看$?来检查最后一个命令的执行状态$? shell变量。 执行下面的命令检查最后一个命令的状态,其他0表示错误。
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 ...
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
对于abcd中的file2get; 渡渡鸟 :; 直到curl –fail $ file2get; DONE
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。