第一个Mac shell 小脚本

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

   废话很少说,直接看个人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

下面是个人一个登陆的接口调试.bash

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

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

相关文章
相关标签/搜索