Shell脚本批量发送POST请求

概述

重点

eval

命令行前面放eval,在执行命令之前,它将被扫描两次

1
2
pipe="|"
eval ls $pipe wc -c

shell第1次扫描命令行时,它替换出pipe的值|,接着eval使它再次扫描命令行,这时shell把|作为管道符号了。

如果变量中包含任何需要shell直接在命令行中看到的字符(不是替换的结果),就可以使用eval。命令行结束符(;| &),I/o重定向符(< >)和引号就属于对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
#! /bin/bash

ip=10.12.102.134
port=7080
cat data | while read i
do
line=$i
source_id=`echo $line | cut -d ' ' -f 1`
item_code=`echo $line | cut -d ' ' -f 2`
description=`echo $line | cut -d ' ' -f 3`
address=`echo $line | cut -d ' ' -f 4`
area_code=`echo $line | cut -d ' ' -f 5`
longitude=`echo $line | cut -d ' ' -f 6`
latitude=`echo $line | cut -d ' ' -f 7`
echo $source_id
echo $item_code
echo $description
echo $address
echo $area_code
echo $longitude
echo $latitude
send_data='{"sourceId":"'$source_id'", "itemCode":"'$item_code'", "description":"'$description'", "address":"'$address'","areaCode":"'$area_code'", "longitude":"'$longitude'", "latitude":"'$latitude'","files":[], "data":{}}'
send_data="'"$send_data"'"
param=' -d '$send_data
command='curl -X POST http://'$ip':'$port'/dcm-web/caseApi/addCase.json -H "Content-Type:application/json"'$param
# curl -X POST http://$ip:$port/dcm-web/caseApi/addCase.json -H "Content-Type: application/json" $param
echo $command
eval $command
done

参考链接

http://www.cnblogs.com/dwdxdy/archive/2012/07/25/2608816.html

http://blog.zengrong.net/post/1591.html

https://droidyue.com/blog/2014/07/02/send-post-request-using-curl/

https://blog.csdn.net/wanruirui/article/details/5975867

http://blog.51cto.com/363918/1341977