tags:

views:

851

answers:

4

My code

#!/bin/bash
for (( c=0; c<=1127; c++ ))
do
id = 9694 + c
if (id < 10000); then
 wget http://myurl.de/source/image/08_05_27_0${id}.jpg
else 
 wget http://myurl.de/source/image/08_05_27_${id}.jpg
fi
done

I only get

./get.sh: line 5: 10000: No such file or directory
--2009-05-06 11:20:36--  http://myurl.de/source/image/08_05_27_.jpg

without the number.

The corrected code:

#!/bin/bash
for (( c=0; c<=1127; c++ ))
do
id=$((9694+c))
if (id -lt 10000); then
    wget http://myurl.de/source/image/08_05_27_0${id}.jpg
else 
    wget http://myurl.de/source/image/08_05_27_${id}.jpg
fi
done

And even better:

for i in $(seq 9694 10821) ; do
    _U=`printf "http://myurl.de/source/image/08_05_27_%05d.jpg" $i`
    wget $_U 
done
+1  A: 

You need:

id=$((9694+c))
...
if [[ id < 10000 ]]; then
joeytwiddle
+2  A: 

This is what you need.

#!/bin/bash
for (( c=0; c<=1127; c++ ))
do
    ((id = 9694 + c))
    if [[ id -lt 10000 ]] ; then
        wget http://myurl.de/source/image/08_05_27_0${id}.jpg
    else 
        wget http://myurl.de/source/image/08_05_27_${id}.jpg
    fi
done
paxdiablo
This won't work. Did you even try it?
Ville Laurikari
Hmm, you've edited your answer, the (( )) were missing from around the assignment line in the original version.
Ville Laurikari
Just to make it clear, the code is now fine and works. And I did cut'n'paste your original code, ran it, and it didn't work.
Ville Laurikari
+2  A: 

You are making a couple of mistakes with bash syntax, especially when dealing with arithmetic expressions.

  • You cannot put a space around the = sign when assigning to a variable.
  • In the assignment to "id", to invoke arithmetic evaluation, you need to use the $(( expression )) syntax.
  • For the "if" condition, you need double parentheses just like you're using with "for".

This should work:

#!/bin/bash
for (( c=0; c<=1127; c++ )); do
  id=$((9694 + c))
  if ((id < 10000)); then
    wget http://myurl.de/source/image/08_05_27_0${id}.jpg
  else
    wget http://myurl.de/source/image/08_05_27_${id}.jpg
  fi
done
Ville Laurikari
Why the downvote?
Ville Laurikari
good explanation.
bendin
+5  A: 

I'll opt for simpler solution

for i in $(seq 9694 10821) ; do
    _U=`printf "http://myurl.de/source/image/08_05_27_%05d.jpg" $i`
    wget $_U 
done
greg
does this add the leading 0 too?
ByteNirvana
Yes. By using %05d as formating argument to printf, that causses printf to left-pad output with zeros
greg
This is a better answer than the others (yes, shock horror, even mine :-). Upvoting.
paxdiablo
Wow, this is very smart. In the end I did it differently but I accept this answer as the best. – ByteNirvana
ByteNirvana
Good answer. Just note that $(...) is preferred to old \`...\`. It could look like this:wget "http://myurl.de/source/image/08_05_27_$(printf "%05d" $i).jpg"
tokland