words.sh:
while read word || [[ -n ${word} ]]
do
encoded=`echo $word | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g'`
html=`curl -s "https://cn.bing.com/dict/search?q=$encoded"`
#meaning=`echo "$html" | grep -e "必应词典为您提供.*的释义" | perl -pe 's/.*<meta name="description" content="必应词典为您提供.*的释义,(美\[[^]]+\],)?(英\[[^]]+\],)?(.*)(" \/><meta content="text\/html;).*/\3/g' | perl -pe 's/(.*)网络释义:.*/\1/g'`
meaning=`echo "$html" | grep -e "必应词典为您提供.*的释义" | perl -pe 's/.*<meta name="description" content="必应词典为您提供.*的释义,(美\[[^]]+\],)?(英\[[^]]+\],)?(.*?)(网络释义:.*)?" \/><meta content="text\/html;.*/\3/g'`
printf "${word}:${meaning}\n"
#if [ -z "$meaning" ]; then
# printf "\n\n====\n$html\n====\n\n"
#fi
done < $1
样例输入文件1.txt:
hypertensive
hyperactive
hypersensitive
执行:
sh words.sh 1.txt > 2.txt
样例输出文件2.txt:
hypertensive:adj. 高血压的; n. 高血压患者;
hyperactive:adj. 过分活跃的;多动的;
hypersensitive:adj. 非常敏感的;很容易生气的;(对某些物质、药物、光等)过敏的;
这段代码展示了一个Shell脚本,用于读取一个包含单词的文件,通过Bing词典的API获取每个单词的英文释义,并将结果输出到另一个文件中。脚本首先对单词进行URL编码,然后使用curl发送HTTP请求,解析返回的HTML以提取释义信息。

2235

被折叠的 条评论
为什么被折叠?



