Search code examples
linuxbashfedora

How can I cat a remote file to read the parameters in Bash?


How can I cat a remote file? Currently, it works for local files only.

#!/bin/bash
regex='url=(.*)'
# for i in $(cat /var/tmp/localfileworks.txt);
for i in $(cat http://localhost/1/downloads.txt);
do
        echo $i;
        # if [[ $i =~ $regex ]]; then
        #echo ${BASH_REMATCH[1]}
        #fi
done

cat: http://localhost/1/downloads.txt: No such file or directory


Solution

  • Instead of cat, which reads a file from the file-system, use wget -O- -q, which reads a document over HTTP and writes it to standard output:

    for i in $(wget -O- -q http://localhost/1/downloads.txt)
    

    (The -O... option means "write to the specified file", where - is standard output; the -q option means "quiet", and disables lots of logging that would otherwise go to standard error.)