简体   繁体   English

Bash 脚本执行 wget,其中包含一个变量

[英]Bash script execute wget with a variable inside it

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"

Four things:四件事:

  1. Add quotes around your URL: http:://www.myurl.com ==> "http:://www.myurl.com"在您的 URL 周围添加引号:http://www.myurl.com ==> "http://www.myurl.com"
  2. Remove the double colon: "http:://www.myurl.com" ==> " http://www.myurl.com "去掉双冒号:“http://www.myurl.com”==>“ http://www.myurl.com
  3. Get rid of the extra flags and hyphen on the wget command: "wget -O - -q "$URL/something/something2"" ==> wget "$URL/something/something2"去掉 wget 命令上的额外标志和连字符: "wget -O - -q "$URL/something/something2"" ==> wget "$URL/something/something2"
  4. Add curly braces around your variable: "wget "$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}"

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. Bash(或其他 shell)中的另一个方便的选项是创建一个简单的辅助函数,该函数使用几乎所有站点所需的通用选项和您通常使用的任何特定选项调用wget 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.我将以下内容放在我的 ~/.bashrc 中,使其可用于所有 shells/subshel​​ls。 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:它验证输入,检查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 .这将正常的wget --no-check-certificate --progress=bar URL到简单的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的另一种方式。

I use that code in IPython (colab):我在 IPython (colab) 中使用该代码:


URL = 'http:://www.myurl.com'
!wget {URL}

I wrote this answer because was searching it!)我写这个答案是因为正在搜索它!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM