Results 1 to 10 of 10
Hi
I need to download a number of web pages save them to disk and then use gawk to extract info from the pages, the gawk bit is not a ...
- 04-27-2007 #1Just Joined!
- Join Date
- Apr 2007
- Posts
- 4
Passing Variables to wget
Hi
I need to download a number of web pages save them to disk and then use gawk to extract info from the pages, the gawk bit is not a problem. I've got a file that lists the web page URL. Each URL is assigned to an incremented variable. I have a FOR loop which should (in theory) pass each variable to wget. The problem is I do not know how to pass the incremented variable to wget -
#!/bin/bash
a1="http://wasd.gre.uk/sd?HTT:IPP:R231"
a2="http://wasd.gre.uk/sd?HTT:IPP:R1445"
a3="http://wasd.gre.uk/sd?HTT:IPP:R891"
nf = 3
for (( i=1; i<=nf; i++ ))
do
wget ${ai}
done
If anyone can shed any light on where I'm going wrong will be appreciated.
Thanks
- 04-27-2007 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
If the variables are all working and being generated (test this with echo) then you should try
instead of ${ai}Code:wget a$i
- 04-27-2007 #3Just Joined!
- Join Date
- Apr 2007
- Posts
- 4
I've tried your suggestion, and it gives the the desired variable name, but in the process it loses the variable value -
a1="http://wasd.gre.uk/sd?HTT:IPP:R231"
nf = 1
for (( i=1; i<=nf; i++ ))
do
echo $a1
echo a$i
done
> http://wasd.gre.uk/sd?HTT:IPP:R231
> a1
- 04-27-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Use arrays:
RegardsCode:#!/bin/bash a[1]="http://wasd.gre.uk/sd?HTT:IPP:R231" a[2]="http://wasd.gre.uk/sd?HTT:IPP:R1445" a[3]="http://wasd.gre.uk/sd?HTT:IPP:R891" nf = 3 for (( i=1; i<=nf; i++ )) do wget ${a[i]} done
- 04-27-2007 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
A solution that is not very general, but works for small sets is:
Which produces:Code:#!/bin/bash # @(#) s1 Demonstrate for loop. a1="http://wasd.gre.uk/sd?HTT:IPP:R231" a2="http://wasd.gre.uk/sd?HTT:IPP:R1445" a3="http://wasd.gre.uk/sd?HTT:IPP:R891" for i in $a1 $a2 $a3 do echo wget $i done
cheers, drlCode:% ./s1 wget http://wasd.gre.uk/sd?HTT:IPP:R231 wget http://wasd.gre.uk/sd?HTT:IPP:R1445 wget http://wasd.gre.uk/sd?HTT:IPP:R891
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 04-27-2007 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
A more general solution, that keeps your loop style is (with corrected assignment):
which produces:Code:#!/bin/bash # @(#) s2 Demonstrate eval: re-scan a command line. a1="http://wasd.gre.uk/sd?HTT:IPP:R231" a2="http://wasd.gre.uk/sd?HTT:IPP:R1445" a3="http://wasd.gre.uk/sd?HTT:IPP:R891" nf=3 for (( i=1; i<=nf; i++ )) do eval echo wget \$a${i} done
cheers, drlCode:% ./s2 wget http://wasd.gre.uk/sd?HTT:IPP:R231 wget http://wasd.gre.uk/sd?HTT:IPP:R1445 wget http://wasd.gre.uk/sd?HTT:IPP:R891
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 04-28-2007 #7Just Joined!
- Join Date
- Apr 2007
- Posts
- 4
Thanks to all that replied.
drl - your a diamond, it works beautifully - Thanks
- 04-28-2007 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
You can take advantage of special array syntax to assign contents easily:
Producing:Code:#!/bin/bash # @(#) s3 Demonstrate easy array assignment. echo " Version $BASH_VERSION" a=( "http://wasd.gre.uk/sd?HTT:IPP:R231" "http://wasd.gre.uk/sd?HTT:IPP:R1445" "http://wasd.gre.uk/sd?HTT:IPP:R891" ) nf=${#a[@]} echo " Obtaining $nf files." for (( i=0; i<nf; i++ )) do echo wget ${a[i]} done exit 0
Best wishes ... cheers, drlCode:% ./s3 Version 2.05b.0(1)-release Obtaining 3 files. wget http://wasd.gre.uk/sd?HTT:IPP:R231 wget http://wasd.gre.uk/sd?HTT:IPP:R1445 wget http://wasd.gre.uk/sd?HTT:IPP:R891
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 04-28-2007 #9Just Joined!
- Join Date
- Apr 2007
- Posts
- 4
Hi DRL
You've given 2 versions, which do you think is the best with respect to computer processing time? In reality I have about 700 URL's that I have to process.
Also do you think my approach of using a FOR loop is good, or would you use something different?
Many thanks for your help
Az
- 04-28-2007 #10Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi, Azz.
The time for the processing of the loop in any of the versions is very small compared to the wget, so choose whichever one you like. I like the 3rd one because it would be very easy to paste URLs in, or even to place the URLs in with another script.
If you get ambitious, try them all on the same 10 or so URLs, and I think you will see that the times are dominated by the wget ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote