我遍历一个文件,并且想使用 URL 中的行(单词)来卷曲 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"
done
文件内容list
:
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,但我也尝试过其他操作系统。
答案1
您的输入文件是 DOS 文本文件。它在每行末尾包含一个额外的回车符,这些字符正在成为word
变量值的一部分,这会引发以下特定错误curl
:
$ curl $'http://localhost/somepath\r'
curl: (3) URL using bad/illegal format or missing URL
如果末尾没有回车符,我会收到预期的错误(本机上没有运行网络服务器):
$ curl 'http://localhost/somepath'
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
考虑使用例如将输入文件转换为 Unix 文本文件dos2unix
。
您的代码中还存在问题,因为您强制 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
withcurl
来自动跟随。