Jacob
Jacob

Reputation: 422

How to use variable value as file name in curl

I am looping through urls in a file to download their data and each url represents data for each hour of the day. I want to name the file the date and hour that it comes from. The following doesn't work, but I'm not quite sure why

myDate=$(date -v -1d '+%Y/%m/%d')
for hour in {0..23}
do
   ...
   ...
   #set file name
   name=$myDate.$hour.txt
   curl -L -o $name "https://..."
done

I think it's just a problem with the syntax for $name in the curl statement, but I don't know how to correct it.

I get the following error

Warning: Failed to create the file 2019/09/18-0: No such file or directory
curl: (23) Failed writing body (0 != 16360)

Upvotes: 0

Views: 356

Answers (1)

ForceBru
ForceBru

Reputation: 44926

date -v -1d '+%Y/%m/%d' returns a string containing slashes, which are used as path separators, so, for example, in:

iMac-ForceBru:~ forcebru$ date -v -1d '+%Y/%m/%d'
2019/09/18

The 2019/09/18 would be treated as a file called 18 in directory 09, which is in turn inside directory 2019. It looks like the path 2019/09 doesn't exist on your system, so the file 2019/09/something.txt can't be created.

Upvotes: 5

Related Questions