Home > front end >  Applying 'for loop' in wget
Applying 'for loop' in wget

Time:07-27

I am trying to get a for loop to work, fetching (wget) from a website through a list of files saved as list, using the above code

for f in `cat list`; do wget --header='Content-type:application/json' 'https://grch37.rest.ensembl.org/ld/human/${f}/1000GENOMES:phase_3:CEU?content-type=application/json;window_size=500;r2=0.8;attribs=yes'
-O - > ${f}.json; done;

Here is the content of my list file

rs10023056
rs10025704
rs10026092
rs10026790
rs10028892

There is an error where the for loop cannot pass the correct name from list to the for loop, resulting in the error as stated below.

--2022-07-27 12:05:21--  https://grch37.rest.ensembl.org/ld/human/${f}/1000GENOMES:phase_3:CEU?content-type=application/json;window_size=500;r2=0.8;attribs=yes
Resolving grch37.rest.ensembl.org (grch37.rest.ensembl.org)... 193.62.192.83
Connecting to grch37.rest.ensembl.org (grch37.rest.ensembl.org)|193.62.192.83|:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2022-07-27 12:05:22 ERROR 400: Bad Request.

It seems to be the names in my list file are replaced with ${f}.

Could someone help me in this?

Thank you.

CodePudding user response:

Single quotes prevent variables from being expanded. You need double quotes to let the variable in your URL expand. When reading from a file, usually it's preferable to use a while-loop:

while IFS= read -r rsid; do
  wget \
    --header='Content-type:application/json' \
    "https://grch37.rest.ensembl.org/ld/human/${rsid}/1000GENOMES:phase_3:CEU?content-type=application/json;window_size=500;r2=0.8;attribs=yes" \
    -O - > "${rsid}.json"
done < list
  • Related