第一個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腳本到此結束...新手寫得比較菜..有不妥地方大家多拍磚..


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM