Search code examples
bashfor-looptouchvar

Cannot touch files with var name in bash script for loop


I'm editing a script for PiHole to convert adblock list format to dns format, to be usable by PiHole. The idea is to scroll over a lists.list file which contains links to different lists, do a curl to each links of this file and create a file for each links named $link.list which contains all dns names.

*The problem is : * I've the message "touch: cannot touch 'https://easylist-downloads.adblockplus.org/easylistgermany.txt.list': No such file or directory"

I tried to see if it was some rights issue, so I took the thing to my home/user folder. If I do

curl --silent $source >> ads.txt 

or

touch ads.txt

it works

Here is what I wrote :

for sources in `cat lists.list`; do
    echo $source
    touch "$source".list
    echo `curl --silent $source` > $source.list
    echo -e "\t`wc -l $source.list | cut -d " " -f 1` lines downloaded"
done

And I get

https://easylist-downloads.adblockplus.org/easylist.txt
touch: cannot touch 'https://easylist-downloads.adblockplus.org/easylist.txt.list': No such file or directory

So any suggestions ? Thanks for your time !


Solution

  • It's because you're using the full URL as a file name, but you can't have slashes in file names. Can you name the files to just the last bit in the URL? Then this should work:

    #!/bin/bash
    
    for source in `cat lists.list`; do
        echo $source
        filename=$(sed -E 's@.+/@@' <<< $source).list  # <-- remove everything up to and including the last slash
        curl --silent "$source" -o "$filename"
        echo -e "\t`wc -l $filename | cut -d " " -f 1` lines downloaded"
    done