Andy K
Andy K

Reputation: 5044

string variable to turn to an integer variable

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.

Upvotes: 2

Views: 117

Answers (3)

anubhava
anubhava

Reputation: 784928

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

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

for ((page=1; page<=var1; page++)); do

Upvotes: 3

dave
dave

Reputation: 12806

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.

Upvotes: 1

sat
sat

Reputation: 14949

You can use this,

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

Upvotes: 1

Related Questions