3

Im working on writing a bash script to download imgur albums for the purpose or learning bash. I have this much written so far but when I run it i get this output:

Enter the URL of the imgur album you would like to download: imgur.com
./imgur_dl.sh: line 25: Validating: command not found
./imgur_dl.sh: line 26: Getting: command not found
./imgur_dl.sh: line 30: imgur.com: command not found 

This is my code so far.

#!/bin/bash

url=""
id=""

get_id(){
  echo "Getting Album ID..."
  local url=$1
  echo $url
  echo "Successflly got Album ID..."
  return
}

validate_input(){
  echo "Validating Input..."
  local id=$1
  echo $id
  echo "Input Validated Successfully..."
  return
}

get_input(){
  read -p "Enter the URL of the imgur album you would like to download: " url
  echo $url
  $(validate_input url)
  $(get_id url)
  return
}

$(get_input)

What am I doing wrong or what am I not getting? I am working on macOS it that helps at all.

1

2 Answers 2

2

Just call the functions directly, like:

valide_input $url

and so on.

    #!/bin/bash

    url=""
    id=""

    get_id ()
    {
      echo "Getting Album ID..."
      local url=$1
      echo $url
      echo "Successflly got Album ID..."
      return
    }

    validate_input ()
    {
      echo "Validating Input..."
      local id=$1
      echo $id
      echo "Input Validated Successfully..."
      return
    }

    get_input ()
    {
      read -p "Enter the URL of the imgur album you would like to d        ownload: " url
      echo $url
      validate_input $url
      get_id $url
      return
    }

    get_input

Also, as someone else suggested, you can make this better by putting the $Url inside double quotes, like

validate_input "$url"

So it handles otherwise invalid urls.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Cyrus. Any quick way to format a block at once?
Highlight the code then click the Code Sample button.
@imerso: or highlight the code and press Ctrl+k.
2

This syntax means, execute the output of get_id url as shell command:

$(get_id url)

In current implementation the output of get_id url is this:

Getting Album ID...
url
Successflly got Album ID...

And this gets executed as shell commands, producing the error message:

./imgur_dl.sh: line 26: Getting: command not found

Because indeed there is no such shell command "Getting".


I think you want to do something like this:

local id=$(get_id "$url")

Where get_id is a function that takes a URL and uses that URL to get some id, and then echo that id. The function should look something like this:

get_id() {
    local url=$1
    local id=...
    echo id
}

That is:

  • You need to remove other echo statements like the echo "Getting ..." stuff
  • You need to actually implement getting an id, because the function currently doesn't do that.

The same goes for the other functions too.


Here's something to get you started:

#!/bin/bash

is_valid_url() {
    local url=$1
    # TODO
}

input_url() {
    local url
    while true; do
        read -p "Enter the URL of the imgur album you would like to download: " url
        if is_valid_url "$url"; then
            echo "$url"
            return
        fi
        echo "Not a valid url!" >&2
    done
}

download() {
    local url=$1
    echo "will download $url ..."
    # TODO
}

main() {
    url=$(input_url)
    download "$url"
}

main

1 Comment

The correct solution, but not a technically accurate description of command substitution.

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.