简体   繁体   English

wget with errorlevel bash output

[英]wget with errorlevel bash output

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! 提前致谢!

Typically applications in Linux will set the value of the environment variable $? 通常Linux中的应用程序会设置环境变量$的值? on failure. 失败了。 You can examine this return code and see if it gets you any error for wget. 您可以检查此返回代码,看看它是否为您带来了wget的任何错误。

#!/bin/bash
wget $1 2>/dev/null
export RC=$?
if [ "$RC" = "0" ]; then
   echo $1 OK
else
    echo $1 FAILED
fi

You could name this script download.sh. 您可以将此脚本命名为download.sh。 Change the permissions to 755 with chmod 755. Call it with the name of the file you wish to download. 使用chmod 755将权限更改为755.使用您要下载的文件的名称调用它。 ./download.sh www.google.com ./download.sh www.google.com

You could try something like: 你可以尝试类似的东西:

#!/bin/sh

[ -n $1 ] || {
    echo "Usage: $0 [url to file to get]" >&2
    exit 1
}

wget $1

[ $? ] && {
  echo "Could not download $1" | mail -s "Uh Oh" you@yourdomain.com
  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 >&2 simply redirects the output of error messages to stderror , which allows a calling script to do something like this: >&2只是将错误消息的输出重定向到stderror ,这允许调用脚本执行以下操作:

foo-downloader >/dev/null 2>/some/log/file.txt

Since it is a short wrapper, no reason to forsake a bit of sanity :) 因为它是一个短的包装,没有理由放弃一点理智:)

This also allows you to selectively direct the output of wget to /dev/null , you might actually want to see it when testing, especially if you get an e-mail saying it failed :) 这也允许你有选择地将wget的输出定向到/dev/null ,你可能真的想在测试时看到它,特别是如果你收到一封电子邮件说它失败了:)

wget executes in non-interactive way. wget以非交互方式执行。 This means that wget work in the background and you can't catch de return code with $?. 这意味着wget在后台工作,你不能用$?来捕获de返回代码。

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需要一些时间来完成他的工作,因此我把“睡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