0

I am trying to use a nested loop so that I can get the urls I desire, while also renaming the downloaded files to be unique. I am not getting something right.

The outcome is that I have unique file names (exactly like I want), but then they all have the same content, not at all what I want.

Here is what I have:

for image_name in `cat $IMAGE_TEST`;
do
  for manifest_url in `cat $MANIFEST_URL_LIST`;
  do
     curl -H "Authorization: Bearer $KC_ACCESS_TOKEN" $manifest_url > \
     manifests/$image_name.json
  done 
done 

I have also tried:

for image_name in `cat $IMAGE_TEST`;
do
  while read -r manifest_url
  do
    curl -H "Authorization: Bearer $KC_ACCESS_TOKEN" $manifest_url > \
    mainifests/$image_name.json
  done < $MANIFEST_URL_LIST
done 

and other combinations of this, but seem to be not seeing what I am missing. Please help!

16
  • Think about what your code does -- it's overwriting every $image_name.json with every possible url, so only the last URL is saved. Commented Feb 13, 2020 at 16:03
  • I'd guess you want the first image URL to be saved in the first filename, the second URL in the second filename, etc; we have Q&A entries on how to do that, but (in any language, not just bash) a nested loop is the wrong tool for that job altogether. Commented Feb 13, 2020 at 16:03
  • BTW, for var in $(anything) and its backtick-based alternative are generally bad practice -- see BashPitfalls entry #1. Use arrays instead: read -t image_names <"$IMAGE_TEST"; read -t manifest_urls <"$MANIFEST_URL_LIST"; for idx in "${!image_names[@]}"; do curl "${manifest_urls[$idx]}" >"manifests/${image_names[$idx]}.json"; done Commented Feb 13, 2020 at 16:05
  • 1
    The sites I linked to already -- the bash-hackers' wiki and the Wooledge wiki -- are the two resources I most recommend other than the official manual itself. In addition to the previously-linked BashPitfalls and BashFAQ pages on the Wooledge wiki, see also the BashGuide. Commented Feb 13, 2020 at 16:39
  • 1
    @CharlesDuffy - I agree 100% - the duplicate you reference (stackoverflow.com/questions/56544497/…, ) is nearly a perfect fit for this question. Commented Feb 13, 2020 at 16:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.