0

Hi guys

I'm trying to loop over the values of a file (or multiple files) passed as a parameter to the script as in, ./myscript file1 file2 ... but awk only recognizes the first line of each file.

#!/bin/bash

while [[ ${#} > 0 ]]
do
 for archivo in ${@}
 do
  read url filename <<<$(awk -F',' '{print $1, $2}' $archivo)
  curl -o $filename $url
 done
done

Files passed as params to the script have the following structure:

url1,filename1
url2,filename2
...

The goal is, whenever the script is called with one or more parameters, awk should parse (and pass) the urls and filenames contained inside each file to curl for it to download the files listed. Any suggestions on how to make it work?

Thanks in advance and happy day!

2
  • You don't seem to be using awk here for anything more than changing a , separator to a whitespace one - why not simply set the shell's IFS to , and read the 2 fields directly? Mar 31, 2022 at 15:42
  • How can I loop over the contents of the file? Should I use cat?
    – arlugones
    Mar 31, 2022 at 15:45

1 Answer 1

2

Aside from the fact that your loop structure appears to be screwed up (you only read one url-filename pair for each file), the unquoted command substitution $(awk -F',' '{print $1, $2}' $archivo) is going to be subject to word-splitting on the default IFS, with the result that the awk output is going to get concatenated onto a single line. Compare:

$ echo $(printf 'foo bar\nbaz bam\n')
foo bar baz bam

versus

$ echo "$(printf 'foo bar\nbaz bam\n')"
foo bar
baz bam

However this is really a useless-use-of-awk - you can set the shell's IFS to , and read your files directly. Probably what you want is:

for archivo in "$@"
do
  while IFS=, read -r url filename rest
  do 
    curl -o "$filename" "$url"
  done < "$archivo"
done
3
  • Excellent! I don't fully comprehend the use of strong quoting vs soft quoting. Guess I should go back to the bash manual. Thanks!
    – arlugones
    Mar 31, 2022 at 16:34
  • @arlugones To learn about quotes in shell please read mywiki.wooledge.org/Quotes.
    – Ed Morton
    Mar 31, 2022 at 21:57
  • 1
    Much obliged @EdMorton!
    – arlugones
    Apr 8, 2022 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .