2013-04-21 507 views
17

我一直在試圖創建一個簡單的腳本,它將從.txt文件中獲取查詢列表,附加主url變量,然後刮取內容並將其輸出到文本文件。使用curl通過網址循環的Shell腳本

這是我到目前爲止有:

#!/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. 

回答

3

你有嵌套的報價,嘗試這樣的事情:

#!/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