Bash
捲曲不良網址 (3)
我遍歷一個文件,我想用 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,但我也嘗試過其他作業系統。
您的輸入文件是一個 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
來自動跟隨。