2

I have the following issue.

I'm putting a string into a variable , $var1. This string will be a numeric type e.g. 118.

I did the following command line to fill my variable:

var1=$(cat test.html | grep "<quote")

But when I'm putting the variable into the loop below ...

for page in {1..$var1} ; do
 wget "http://www.hellomovie.co.uk/movies/decade-2000/year-2001/?page=$page";
 done

The wget is getting me this ...

Saved in : «index.html@page={1..118}»

[ <=> ] 135 284 --.-K/s   in 0,1s 2014-04-08 15:36:58 (1,07 MB/ - «index.html@page=**{1..118}**.2» saved[135284]`

As you can see, the variable was not taken into account.

I tried the following to make sure that $var1 was seen as a numeric variable , $(($var1)) but to no avail.

I tried expr but to no avail as well.

Did I do something wrong? If yes, can you pintpoint me where?

Any insights will be welcomed.

Cheers.

2
  • If the file contains just the number, you can get it more efficiently with read var1 < test.html. Commented Apr 8, 2014 at 14:03
  • Thanks for the tip, Chepner. Alas, the file is a html file with many things. I needed sed to remove many things. But I will keep it , for later usage. Cheers. Commented Apr 8, 2014 at 14:07

3 Answers 3

3

You cannot use variable as in {1..$N}.

Use BASH arithmetic construct i.e. ((...)):

for ((page=1; page<=var1; page++)); do
Sign up to request clarification or add additional context in comments.

3 Comments

Cheers Anubhava. You are my savior. :)
Everything inside ((...)) is evaluated in an arithmetic context, so the dollar sign is optional. Anything that doesn't look like a number or an operator is assumed to be a variable and expanded (to 0, if the variable wasn't previously set).
Indeed Chepner. I tried without the dollar as well and it works!
1

You can use this,

for page in `seq $var1` ; do
...
...
done

Comments

1

Try this:

for(( page=1 ; page <= $var1 ; page++)); do
 wget "http://www.hellomovie.co.uk/movies/decade-2000/year-2001/?page=$page";
done

You're problem wasn't in assigning the variable, but trying to use $var1 inside of the { .. } construct.

Comments

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.