zoukankan      html  css  js  c++  java
  • 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需要自己申请获取;

    <人追求理想之时,便是坠入孤独之际.> By 史泰龙
  • 相关阅读:
    ubuntu下手动安装autoconf
    解决VMware下的ubuntu桌面鼠标键盘失效的问题
    DP搬运工1
    把数字转换成货币格式
    指定字符隐藏
    JS 时间获取 (常用)
    electron 安装
    el-form表单校验包含循环
    算法-07| 动态规划
    纯手撸——归并排序
  • 原文地址:https://www.cnblogs.com/jason-Gan/p/11810976.html
Copyright © 2011-2022 走看看