第一个Mac shell 小脚本

    大多数程序员都喜欢偷懒的,我也不例外.相信好多Android开发的coder 在网络http请求方面,会浪费很多时间在接口调试这里..有时候,自己写了一个小测试,行还好,不行的话,还要跟写后台的哥们一起扯扯蛋...于是自己就写了一个curl的小脚本,专门调试这方面的东西.(主要适用于用JSON的传输方式).

   废话不多说,直接看我的SHELL吧:

  

#!/bin/sh
echo -n "enter the request host: "
read host # request host

#cookie
echo -n "use cookie ? (y/n)  "
read is_cookie

cookie=""

#if need cookie set
if [[ $is_cookie = "y" ]]; then
  echo -n "input the cookie: "
  read input_cookie
  cookie=$input_cookie
fi

echo -n "need post? (y/n)  "
read tag

#http get if tag = n
if [[ $tag = "n" ]]; then
  if [[ $cookie != "" ]]; then
    curl $host -b$cookie
  else
    curl $host
  fi
  exit 0
fi

#the json data need to post
data=""

#the input value pair
kv_pair=""

while true; do
  if [[ $tag = "y" ]]; then
  #input key
  echo -n "key :  "
  read key

   # set break condition key = gameover 这里是一个循环结束的标志.输入这个标志表示我需要传递的json数据已经全部写进去了
    if [[ $key = "gameover" ]]; then
    #complete the json format, %,*  start at right side and romove the first ','
    kv_pair=${kv_pair%,*}

    #this is the last data to post
    data="{"$kv_pair"}"
    echo "post data is: $data and exce the curl cmd"
    #curl $host -d$data
    break;
    fi

  #encode with ""
  key='"'$key'"'

  #input value
  echo -n "value : "
  read value

  #encode value with ""
  value='"'$value'"'

  #set value pair and extends itself
  kv_pair="$kv_pair"$key":"$value","
  echo "$kv_pair"
  fi

done

#do http post
if [[ $cookie != "" ]]; then
  curl $host -d$data -b$cookie
else
  curl $host -d$data
fi

  我的是在mac os 下运行的,输入命令 bash xx.sh (xx是你的文件名) .就可以运行了,运行效果如下:

下面是我的一个登录的接口调试.

输入的JSON只需要对着key value输入就好了..gameover的时候就有结果了..有点方便吧..这个东西能跑通,说明后台接口基本能运行~~然后你就专心写你的请求代码吧..

第一个Shell脚本到此结束...新手写得比较菜..有不妥地方大家多拍砖..

原文地址:https://www.cnblogs.com/jarrah/p/3722825.html