0

So I'm writing a script that downloads images from a certain website. Things are going great thus far, but I've run into the issue of inputting URLs containing the character "&". Is there a way to take inputs like this without needing to put quotes in the command input itself? In other words, am I able to take the raw input?

Example code:

#!/bin/bash

# Sample input:         https://website.com/posts?page=2&tags=sometag
#                       https://website.com/posts?tags=sometag&page=2
#                       https://website.com/posts?tags=sometag
#                       https://website.com/posts?page=2
#                       https://website.com/posts

folder=""
if [[ "$folder" == "" ]]; then
        case "$1" in
                *website.com/posts*)    if [[ "$1" == *"tags="* ]]; then
                                                folder="/default/tags"
                                                echo "$folder"
                                        else
                                                folder="/default/posts"
                                                echo "$folder"
                                        fi;;
        esac
fi

# Expected output:      /default/tags   (page 2)
#                       /default/tags   (page 2)
#                       /default/tags   (page 1)
#                       /default/posts  (page 2)
#                       /default/posts  (page 1)

# Actual output:        /default/posts  (page 2)
#                       /default/tags   (page 1)
#                       /default/tags   (page 1)
#                       /default/posts  (page 2)
#                       /default/posts  (page 1)

Any help is appreciated!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
wflame
  • 3
  • 3
  • Do you mean input or argument? – choroba Oct 03 '20 at 22:06
  • @choroba I think I meant argument. A little hazy on the difference between those – wflame Oct 03 '20 at 22:09
  • Arguments are specified on the command line, input is read from a file or standard input. – choroba Oct 03 '20 at 22:11
  • @choroba Argument. – wflame Oct 03 '20 at 22:12
  • 1
    If you put a bare (unescaped) `&` in a command line, that isn't passed as an argument -- instead, it _separates_ the command on the left of it from the command on the right. You can't fix that from inside the script: By the time the script is running, it's too late. – Charles Duffy Oct 03 '20 at 22:29
  • See https://ideone.com/OLsWCx (and in the future if you can use that form for your [mre]s, it's a lot easier to copy-and-paste-to-test) – Charles Duffy Oct 03 '20 at 22:33

0 Answers0