bash - 在没有 curl 的情况下在 Bash 中使用 API

标签 bash api

我正在编写一个脚本,该脚本具有使用天气 API 的功能。目前我只设法用 curl 解决了这个问题,但我最近注意到我无法访问需要运行脚本的 curl 。还有其他方法可以解决这个问题吗?我的代码是这样的:

weatherLondon()
{
    echo "<h3>Weather in London</h3>"
    (( $# )) || set -- "London,UK"
    echo "Location: ${1//,/, }"
    curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1" | perl -ne 's/&amp;deg;/°/g;
                                                                                                s/ \(\d+°F\)//g;
                                                                                                /<title>([^<]+)/ && printf "%s: ",$1;
                                                                                                /<fcttext>([^<]+)/ && print $1,"\n"';
}

我在 http://www.commandlinefu.com/commands/view/4821/get-the-weather-forecast-for-the-next-24-to-48-for-your-location 找到了这段代码

我对 bash 和 API 都很陌生

最佳答案

您可以使用旧的 wget。它应该是几乎每个 Linux 发行版的一部分(除非没有被管理删除)。将您的 curl 语句替换为:

wget -q -O - "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1"

-O - 将输出设置为标准输出,-q 像 curl 的 -s

一样抑制调试输出

关于bash - 在没有 curl 的情况下在 Bash 中使用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20202124/

相关文章:

linux - 用 bash 脚本解析 `ls -l` 的结果

javascript - Discord-api "PATCH Modify Guild Member"没有做任何更改,当我使用 Google-Scripts : “UrlFetchApp.fetch()” 进行修补时

python - 'invalid_grant : Invalid JWT: Google Translation Api

bash - 在 sh shell 中找不到源命令

bash - 如何用sed替换第一次出现?

bash - 如何在 find -exec 语句中使用 >> ?

javascript - 无法使用 Node.js 服务器连接到 Google Directions API

c# - 为 Solidworks 构建 C# 插件

api - 检查Skype用户名是否存在

linux - 对 expr 用法的困惑