带有错误级别 bash 输出的 wget
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4118837/
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
wget with errorlevel bash output
提问by CyberK
I want to create a bash file (.sh) which does the following:
我想创建一个执行以下操作的 bash 文件 (.sh):
I call the script like ./download.sh www.blabla.com/bla.jpg
我将脚本称为 ./download.sh www.blabla.com/bla.jpg
the script has to echo then if the file has downloaded or not...
如果文件已下载,脚本必须回显...
How can I do this? I know I can use errorlevel but I'm new to linux so...
我怎样才能做到这一点?我知道我可以使用 errorlevel 但我是 linux 新手所以...
Thanks in advance!
提前致谢!
采纳答案by ojblass
Typically applications in Linux will set the value of the environment variable $? on failure. You can examine this return code and see if it gets you any error for wget.
通常 Linux 中的应用程序会设置环境变量 $? 失败。你可以检查这个返回码,看看它是否会给你带来 wget 的任何错误。
#!/bin/bash
wget 2>/dev/null
export RC=$?
if [ "$RC" = "0" ]; then
echo OK
else
echo FAILED
fi
You could name this script download.sh. Change the permissions to 755 with chmod 755. Call it with the name of the file you wish to download. ./download.sh www.google.com
您可以将此脚本命名为 download.sh。使用 chmod 755 将权限更改为 755。使用您要下载的文件的名称调用它。./download.sh www.google.com
回答by Tim Post
You could try something like:
你可以尝试这样的事情:
#!/bin/sh
[ -n ] || {
echo "Usage: foo-downloader >/dev/null 2>/some/log/file.txt
[url to file to get]" >&2
exit 1
}
wget
[ $? ] && {
echo "Could not download " | mail -s "Uh Oh" [email protected]
echo "Aww snap ..." >&2
exit 1
}
# If we're here, it downloaded successfully, and will exit with a normal status
When making a script that will (likely) be called by other scripts, it is important to do the following:
在制作将(可能)被其他脚本调用的脚本时,执行以下操作很重要:
- Ensure argument sanity
- Send e-mail, write to a log, or do something else so someone knows what went wrong
- 确保论证理智
- 发送电子邮件、写入日志或执行其他操作,以便有人知道出了什么问题
The >&2simply redirects the output of error messages to stderror, which allows a calling script to do something like this:
在>&2简单地重定向错误信息的输出stderror,它允许调用脚本做这样的事情:
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
Since it is a short wrapper, no reason to forsake a bit of sanity :)
由于它是一个简短的包装器,因此没有理由放弃一点理智:)
This also allows you to selectively direct the output of wgetto /dev/null, you might actually want to see it when testing, especially if you get an e-mail saying it failed :)
这也使您可以选择性直接输出wget到/dev/null,你可能真的想看到它在测试时,特别是如果你收到一封邮件说失败了:)
回答by kazzikazzi
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 状态代码示例:
##代码##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”。这不是最好的方法,但可以很好地测试解决方案。

