0

I am trying to write a shell script that uses wget to download files in bulk from urls that follow a certain numeric pattern.

Understandably, the url from the user input must contain the variable $i.

dl.sh http://some/url/$i/some/url 1 9

This yields repeated result from the final loop because $i will be expanded before passing down into the loop.

http://some/url/9/some/url
http://some/url/9/some/url
...
http://some/url/9/some/url

Is there a workaround to get this shell script working?

Source Code:

#!/bin/bash
# dl.sh url | index_from | index_to

for i in $(seq $2 $3)
do
  echo ${1} # replace with wget for actual download. 
done

Expected Result:

http://some/url/1/some/url
http://some/url/2/some/url
http://some/url/3/some/url
...
http://some/url/9/some/url
Sati
  • 716
  • 6
  • 27
  • You are using the script wrong. Define your script as below `#!/bin/bash for i in $(seq $2 $3) do echo http://some/url/$i done` and run the script as `bash dl.sh 1 9` – Inian Jun 07 '18 at 08:22
  • @Inian: Yes, I know your script will work. My question is: What if I need to use `http://some/url/$i` as part of the user input to the script? Is there a possible workaround for that? – Sati Jun 07 '18 at 08:55
  • 1
    That is really a bad practice, when you pass `http://some/url/$i`, the `$i` will be expanded by the shell before passed as the argument, which will be empty. – Inian Jun 07 '18 at 08:57
  • Leave the `$i` from arg and do `dl.sh http://some/url/ 1 9` and inside the script do `echo $1$i` – Inian Jun 07 '18 at 08:59
  • What if `$i` is duplicated? Or embedded somewhere within the url? Eg. `http://some/url/$i/$i` or `http://some/url/$i/some/url` – Sati Jun 07 '18 at 09:02
  • 1
    In that case I would recommend passing base URL as the first arg, and build up the the other parameters inside – Inian Jun 07 '18 at 09:07
  • Could you show me an example? – Sati Jun 07 '18 at 09:08
  • Ask it as a separate question which is not a duplicate of the contents above, – Inian Jun 07 '18 at 09:09
  • Amended my question. See if it goes. – Sati Jun 07 '18 at 09:14
  • Why did you modify the same question? You have totally de-scoped your original question. – Inian Jun 07 '18 at 09:21
  • 1
    If the value between the URL is dynamic, there is no other way than to split your URL as separate parts. In case of `http://some/url/1/some/url`, pass argument `http://some/url/` as separate and `/some/url` as separate – Inian Jun 07 '18 at 09:23
  • 1
    Just use a Placeholder in your URL and replace with `sed` like this: `url=$(echo $1 | sed "s/\_IDX\_/$i/g")` and use your script like this: `./dl.sh http://some/url/_IDX_/some/url 1 9` – Nidhoegger Jun 07 '18 at 10:13

0 Answers0