Broshi
Broshi

Reputation: 3592

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?

#!/bin/bash

URL=http:://www.myurl.com

echo $(date) 'Running wget...'
wget -O - -q "$URL/something/something2"

Upvotes: 5

Views: 27886

Answers (4)

ATES
ATES

Reputation: 337

for Kaggle notebook:

url = "https://google.com"
name = "file.f"
!wget "$url" -O "$name"

Upvotes: 0

Kobap Bopy
Kobap Bopy

Reputation: 91

I use that code in IPython (colab):


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

I wrote this answer because was searching it!)

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84531

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:

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.

Upvotes: 0

Dave McMordie
Dave McMordie

Reputation: 573

Four things:

  1. Add quotes around your URL: http:://www.myurl.com ==> "http:://www.myurl.com"
  2. Remove the double colon: "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"
  4. Add curly braces around your variable: "wget "$URL/something/something2"" ==> "wget "${URL}/something/something2""

This works:

#!/bin/bash

URL="http://www.google.com"

echo $(date) 'Running wget...'
wget "${URL}"

Upvotes: 8

Related Questions