我迭代一个文件,我想用URL中的行(word)来压缩api。
list2csv.sh的内容如下:
#!/bin/bash
for word in $( cat $1 )
do
echo "https://api.dictionaryapi.dev/api/v2/entries/en/$word"
curl "http://api.dictionaryapi.dev/api/v2/entries/en/$word"
donelist文件的内容:
timber
clatter当我运行./list2csv.sh list时,输出是:
https://api.dictionaryapi.dev/api/v2/entries/en/timber
curl: (3) URL using bad/illegal format or missing URL
https://api.dictionaryapi.dev/api/v2/entries/en/clatter
curl: (3) URL using bad/illegal format or missing URL如果我试图压缩那些回响的URL,我就会得到:
$ curl https://api.dictionaryapi.dev/api/v2/entries/en/timber
[{"word":"timber","phonetic":"ˈtɪmbə","phonetics":[{"text":"ˈtɪmbə","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/timber--_gb_1.mp3"}],"origin":"Old English in the sense ‘a building’, also ‘building material’, of Germanic origin; related to German Zimmer ‘room’, from an Indo-European root meaning ‘build’.","meanings":[{"partOfSpeech":"noun","definitions":[{"definition":"wood prepared for use in building and carpentry.","example":"the exploitation of forests for timber","synonyms":["wood","logs","firewood","planks","wood products","forest","woodland","woods","lumber"],"antonyms":[]},{"definition":"personal qualities or character.","example":"she is frequently hailed as presidential timber","synonyms":[],"antonyms":[]}]}]}]%
$ curl https://api.dictionaryapi.dev/api/v2/entries/en/clatter
[{"word":"clatter","phonetic":"ˈklatə","phonetics":[{"text":"ˈklatə","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/clatter--_gb_1.mp3"}],"origin":"Old English (as a verb), of imitative origin.","meanings":[{"partOfSpeech":"noun","definitions":[{"definition":"a continuous rattling sound as of hard objects falling or striking each other.","example":"the horse spun round with a clatter of hooves","synonyms":[],"antonyms":[]}]},{"partOfSpeech":"verb","definitions":[{"definition":"make or cause to make a continuous rattling sound.","example":"her coffee cup clattered in the saucer","synonyms":["rattle","clank","clink","clunk","clang","bang","blatter"],"antonyms":[]}]}]}]%我使用macOS,但我也尝试过其他操作系统。
发布于 2022-01-24 18:04:13
您的输入文件是DOS文本文件。它在每一行的末尾都包含一个额外的回车字符,这些字符正在成为word变量值的一部分,这将引发来自curl的特定错误:
$ curl 如果最后没有回车,我就会得到预期的错误(这台机器上没有运行web服务器):$ curl 'http://localhost/somepath'
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused考虑使用例如dos2unix将输入文件转换为Unix文本文件。代码中还存在问题,因为强制shell一次读取整个输入文件,在空格、制表符和换行符上拆分文件的内容,以及对结果的单词执行文件名全局化。此外,您还以同样的方式拆分了命令行上给定的路径名。使用while循环一次读取一个单词会更安全:#!/bin/sh
cat -- "$@" |
while IFS= read -r word; do
curl "https://api.dictionaryapi.dev/api/v2/entries/en/$word"
done或者,用xargs,#!/bin/sh
cat -- "$@" |
xargs -t -I {} \
curl 'https://api.dictionaryapi.dev/api/v2/entries/en/{}'上述两个脚本都将命令行中作为参数提供的所有文件连接在一起,并将输出传递给curl,每次一行。请注意,我还在您的URL中将HTTP更正为HTTPS。使用HTTP,远程服务将重定向到HTTPS站点,这要求您使用-L和curl自动跟踪。http://localhost/somepath\r'
curl: (3) URL using bad/illegal format or missing URL如果最后没有回车,我就会得到预期的错误(这台机器上没有运行web服务器):
A3
考虑使用例如D4将输入文件转换为Unix文本文件。
代码中还存在问题,因为强制shell一次读取整个输入文件,在空格、制表符和换行符上拆分文件的内容,以及对结果的单词执行文件名全局化。此外,您还以同样的方式拆分了命令行上给定的路径名。
使用D5循环一次读取一个单词会更安全:
A6
或者,用D7,
A8
上述两个脚本都将命令行中作为参数提供的所有文件连接在一起,并将输出传递给D9,每次一行。
请注意,我还在您的URL中将HTTP更正为HTTPS。使用HTTP,远程服务将重定向到HTTPS站点,这要求您使用D10和D11自动跟踪。
https://unix.stackexchange.com/questions/687738
复制相似问题