5

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"
7
  • What makes you think it is being ignored? What is happening exactly? Commented May 7, 2015 at 16:53
  • it just keeps running, the command im running is supposed to create a log file and it doesnt...maybe I should remove the -O -q flags? Commented May 7, 2015 at 16:54
  • -O - will spit the retrieved document to standard output so you should see output if it is getting the page. You could remove -q to see if wget says anything about what is going on. But this sounds like it might just be taking wget a while to connect. How long have you waited? Commented May 7, 2015 at 16:57
  • aha, now I see the error: Resolving http (http)... failed: Name or service not known. wget: unable to resolve host address http and it added ftp:// prefix to the real http:// address... Commented May 7, 2015 at 16:58
  • 4
    You have two colons in your URL. Commented May 7, 2015 at 17:01

4 Answers 4

8

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}"
Sign up to request clarification or add additional context in comments.

Comments

1

I use that code in IPython (colab):


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

I wrote this answer because was searching it!)

Comments

0

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.

Comments

0

for Kaggle notebook:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.