bash - 使用 curl 循环访问 url 的 Shell 脚本

标签 bash loops curl

我一直在尝试创建一个简单的脚本,该脚本将从 .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

列表:

images
news
stuff
other

错误日志:

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

如果我直接从命令行使用这个命令,我会在文件中得到一些输出:

curl -L http://example.com/?q=other >> output.txt

最终我希望输出为:

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

followed by the next query in the list.

最佳答案

使用更多引号!

试试这个:

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

关于bash - 使用 curl 循环访问 url 的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131321/

相关文章:

linux - 在 Linux 中写一个脚本在每个最大深度为 2 的子目录中运行 make

c++ - 在 FOR 循环中声明变量

javascript - While循环: Generated table rows and column inconsistency

php - MailGun 401 响应禁止我哪里出错了

PHP : Curl https xml result returns blank

linux - Shell 脚本在 Ubuntu 中成功但在 Amazon Linux 2 中失败?

bash - 带参数的shell脚本?

linux - 递归重命名所有子目录中的 .jpg 文件

optimization - 如何为向量中的每个元素计算另一个向量中较小的元素的分数?

php - 我可以通过编程方式自动确定 IP 位置来覆盖 Google SERP 吗?