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"
2 个解决方案
#1
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""
在您的网址周围添加引号: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}"
#2
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中,以使其可用于所有shell /子shell。它验证输入,检查wget是否可用,然后使用脚本中设置的默认选项将所有命令行参数传递给wget:
wgnc () {
if [ -z $1 ]; 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的另一种方法。
#1
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""
在您的网址周围添加引号: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}"
#2
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中,以使其可用于所有shell /子shell。它验证输入,检查wget是否可用,然后使用脚本中设置的默认选项将所有命令行参数传递给wget:
wgnc () {
if [ -z $1 ]; 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的另一种方法。