Shell脚本使用curl循环遍历URL

Men*_*ega 21 bash curl loops

我一直在试图创建一个简单的脚本,将采取查询列表从一个.txt文件,追加的主URL变量,然后刮的内容并把它输出到一个文本文件中.

这是我到目前为止所拥有的:

#!/bin/bash

url="example.com/?q="
for i in $(cat query.txt); do
    content=$(curl -o $url $i)
    echo $url $i
    echo $content >> output.txt
done
Run Code Online (Sandbox Code Playgroud)

列表:

images
news
stuff
other
Run Code Online (Sandbox Code Playgroud)

错误日志:

curl: (6) Could not resolve host: other; nodename nor servname provided, or not known
example.com/?q= other
Run Code Online (Sandbox Code Playgroud)

如果我直接从命令行使用此命令,我会在文件中输出一些内容:

curl -L http://example.com/?q=other >> output.txt
Run Code Online (Sandbox Code Playgroud)

最终我希望输出为:

fetched:    http://example.com/?q=other
content:    the output of the page

followed by the next query in the list.
Run Code Online (Sandbox Code Playgroud)

Gil*_*not 26

使用更多报价!

试试这个:

url="example.com/?q="
for i in $(cat query.txt); do
    content="$(curl -s "$url/$i")"
    echo "$content" >> output.txt
done
Run Code Online (Sandbox Code Playgroud)


Dav*_*rge 5

你有嵌套的引号,尝试这样的事情:

#!/bin/bash

url=https://www.google.fr/?q=
while read query
do
    content=$(curl "{$url}${query}")
    echo $query
    echo $content >> output.txt
done < query.txt
Run Code Online (Sandbox Code Playgroud)