shell脚本调用api接口,对返回数据处理并返回结果

 1 #!/bin/bash
 2 # 经纬度反解析地址区域,调用的是腾讯位置的api
 3 
 4 parse_district(){
 5     url="https://apis.map.qq.com/ws/geocoder/v1/?location=$1,$2&key=4XCBZ-BPJ6G-23JQI-I2FZ3-ZSDQ7-E3BVp"
 6     #echo ${url}
 7     curl ${url} > index.json
 8     # 使用正则提取需要的区域信息,实际有需要可以提取省市等接口返回的其他信息
 9     grep -E '"district":(.*?),' index.json > district.info
10     res=`cat district.info`    # eg: "district": "海淀区",
11     res=${res##*:}    # "海淀区",
12     res=${res:0:-1}    # "海淀区"
13     echo ${res}    
14 }
15 
16 # shell函数默认返回值是函数 打印的结果, 如果要使用return,只能是return int型数字
17 a1=`parse_district "$1" "$2"`
18 echo "这是处理返回后的结果: ${a1}"

上述是shell脚本, 比如文件名是:  parse_district.sh

执行的话传入函数需要的两个参数, 分别是latitude(纬度),和经度(longitude) eg:  shell  parse_district.sh 39.984154  116.307490

执行结果:  "这是处理返回后的结果: 海淀区"

ps: 上述key需要自己申请获取;

posted on 2019-11-07 11:24  一剑风徽  阅读(15479)  评论(0编辑  收藏  举报

导航