4

I am reading a file (with URL's) line by line:

#!/bin/bash
while read line
do
    url=$line
    wget $url
    wget $url_{001..005}.jpg
done < $1

For first, I want to download primary url as you see wget $url. After that I want to add to the url sequence of numbers (_001.jpg, _002.jpg, _003.jpg, _004.jpg, _005.jpg):

wget $url_{001..005}.jpg

...but for some reason it's not working.

Sorry, missed out one thing: the url's are like http://xy.com/052914.jpg. Is there any easy way to add _001 before the extension? http://xy.com/052914_001.jpg. Or I have to remove ".jpg" from the file containing URL's then simply add later to the variable?

Adrian
  • 2,576
  • 9
  • 49
  • 97

2 Answers2

5

Another way escaping the underscore char:

wget $url\_{001..005}.jpg
Iris G.
  • 433
  • 1
  • 6
  • 15
3

Try encapsulating your variable name:

wget ${url}_{001..005}.jpg

Bash is trying to expand the variable $url_ in your command.

As for your jpg within the URL followup, see substring expansion in the bash manual.

wget ${url:0: -4}_{001..005}.jpg

The :0: -4 means, expand to the variable from position zero (the first character), minus the last 4 characters.

Or from this answer:

wget ${url%.jpg}_{001..005}.jpg

%.jpg removes .jpg specifically and will work on older versions of bash.

Community
  • 1
  • 1
Politank-Z
  • 3,653
  • 3
  • 24
  • 28