BASH脚本,用于将Google地理编码api查找存储为变量

时间:2016-04-08 00:09:01

标签: bash google-maps curl geolocation

我正在尝试将Google地理编码查找存储为BASH中的变量。

以下是我想要的结果 (或类似的东西)

 _GEOINFO=$(curl "http://maps.googleapis.com/maps/api/geocode/json?address="$_WHERE"" 

以下几乎可行:

_CITY="Grand rapids"
_STATE="Michigan"
_COUNTRY="United States of America"
_WHERE="\"$_CITY\",\"$_STATE\",\"$_COUNTRY\""
curl "http://maps.googleapis.com/maps/api/geocode/json?address="$_WHERE""

以下是没有的:

curl "http://maps.googleapis.com/maps/api/geocode/json?address=$_WHERE"

我尝试过很多不同的变种:

\"$_WHERE\", \'\"$_WHERE\"\', \"${_WHERE}\",

对于我尝试过的大部分内容,我收到错误消息:

parse error: Invalid numeric literal at line 1, column 10

1 个答案:

答案 0 :(得分:0)

固定IT!

# store location information into variables 
_CITY="Grand rapids"
_STATE="Michigan"
_COUNTRY="United States of America"

# store all information as one string
_WHERE="$_CITY,$_STATE,$_COUNTRY"

# replace spaces with a +
_WHERE="${_WHERE// /+}"

# store the geo-location information into a variable
_GEOINFO=$(curl "http://maps.googleapis.com/maps/api/geocode/json?address=\"$_WHERE\""