Mac shell 小脚本开发(转)

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

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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
echo -"enter the request host: "
read host # request host
 
#cookie
echo -"use cookie ? (y/n)  "
read is_cookie
 
cookie=""
 
#if need cookie set
if [[ $is_cookie = "y" ]]; then
  echo -"input the cookie: "
  read input_cookie
  cookie=$input_cookie
fi
 
echo -"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 -"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 -"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的时候就有结果了..有点方便吧..这个东西能跑通,说明后台接口基本能运行~~然后你就专心写你的请求代码吧..

原文地址:https://www.cnblogs.com/mafeng/p/6855219.html