首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卷曲坏URL (3)

卷曲坏URL (3)
EN

Unix & Linux用户
提问于 2022-01-24 16:48:31
回答 1查看 10.3K关注 0票数 2

我迭代一个文件,我想用URL中的行(word)来压缩api。

list2csv.sh的内容如下:

代码语言:javascript
复制
#!/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文件的内容:

代码语言:javascript
复制
timber
clatter

当我运行./list2csv.sh list时,输出是:

代码语言:javascript
复制
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,我就会得到:

代码语言:javascript
复制
$ 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,但我也尝试过其他操作系统。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2022-01-24 18:04:13

您的输入文件是DOS文本文件。它在每一行的末尾都包含一个额外的回车字符,这些字符正在成为word变量值的一部分,这将引发来自curl的特定错误:

代码语言:javascript
复制
$ 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站点,这要求您使用D10D11自动跟踪。

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/687738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档