Bash 脚本执行 wget,其中包含一个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30107250/
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
Bash script execute wget with a variable inside it
提问by Broshi
I'm trying to execute a wget command with a variable inside it but it just ignores it, any idea what am I doing wrong?
我正在尝试执行一个带有变量的 wget 命令,但它只是忽略了它,知道我做错了什么吗?
#!/bin/bash
URL=http:://www.myurl.com
echo $(date) 'Running wget...'
wget -O - -q "$URL/something/something2"
回答by Dave McMordie
Four things:
四件事:
- Add quotes around your URL: http:://www.myurl.com ==> "http:://www.myurl.com"
- Remove the double colon: "http:://www.myurl.com" ==> "http://www.myurl.com"
- Get rid of the extra flags and hyphen on the wget command: "wget -O - -q "$URL/something/something2"" ==> wget "$URL/something/something2"
- Add curly braces around your variable: "wget "$URL/something/something2"" ==> "wget "${URL}/something/something2""
- 在您的 URL 周围添加引号:http://www.myurl.com ==> "http://www.myurl.com"
- 去掉双冒号:“http://www.myurl.com”==>“ http://www.myurl.com”
- 去掉 wget 命令上的额外标志和连字符: "wget -O - -q "$URL/something/something2"" ==> wget "$URL/something/something2"
- 在变量周围添加花括号: "wget "$URL/something/something2"" ==> "wget "${URL}/something/something2""
This works:
这有效:
#!/bin/bash
URL="http://www.google.com"
echo $(date) 'Running wget...'
wget "${URL}"
回答by David C. Rankin
Another handy option in Bash (or other shells) is to create a simple helper function that calls wget
with the common options required by nearly all sites and any specific options you generally use. This reduces the typing involved and can also be useful in your scripts. I place the following in my ~/.bashrc to make it available to all shells/subshells. It validates input, checks that wget
is available, and then passes all command line arguments to wget
with the default options set in the script:
Bash(或其他 shell)中的另一个方便的选项是创建一个简单的辅助函数,wget
该函数调用几乎所有站点所需的通用选项以及您通常使用的任何特定选项。这减少了所涉及的输入,并且在您的脚本中也很有用。我将以下内容放在我的 ~/.bashrc 中,使其可用于所有 shells/subshells。它验证输入,检查wget
可用,然后将所有命令行参数传递给wget
脚本中设置的默认选项:
wgnc () {
if [ -z ]; then
printf " usage: wg <filename>\t\t(runs wget --no-check-certificate --progress=bar)\n"
elif ! type wget &>/dev/null; then
printf " error: 'wget' not found on system\n"
else
printf " wget --no-check-certificate --progress=bar %s\n" "$@"
wget --no-check-certificate --progress=bar "$@"
fi
}
You can cut down typing even more by aliasing the function further. I use:
您可以通过进一步为函数设置别名来进一步减少输入。我用:
alias wg='wgnc'
Which reduces the normal wget --no-check-certificate --progress=bar URL
to simply wg URL
. Obviously, you can set the options to suit your needs, but this is a further way to utilize wget
in your scripts.
这将正常减少wget --no-check-certificate --progress=bar URL
到简单wg URL
。显然,您可以设置选项以满足您的需要,但这是wget
在脚本中使用的另一种方式。