bash从字符串中解析出带引号的变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
#shebang must be bash.
 
print_args()
{
	echo "1st is [$1]"
	echo "2nd is [$2]"
	echo "3rd is [$3]"
	echo "4th is [$4]"
	echo "5th is [$5]"
	echo "6th is [$6]"
	echo "7th is [$7]"
	echo "8th is [$8]"
}
 
VAR1=(1 2 "3 4" 5)
print_args "${VAR1[@]}"
 
VAR2="11 22 \"33 44\" 55"
#eval "VAR2=($VAR2)"
declare -a "VAR2=($(echo $VAR2 | tr '`$<>' '????'))"
print_args "${VAR2[@]}"

refer to:
https://superuser.com/questions/1066455/how-to-split-a-string-with-quotes-like-command-arguments-in-bash

用curl发送json请求

以下是json.bat的内容:

1
2
3
4
set /P json=<%1
set result=%json:"=\"%
curl -H "Content-Type:application/json" -X POST -d %result% http://you.url
pause

然后把只含json内容的文件拖到json.bat上面运行就可以了。

注意1,我测试时下载的curl命令行工具是不支持json串中包含空格的,这里只作测试用途,空格先都去掉。
注意2,json.bat第2行包含了Windows批处理脚本中,往字符串里插入字符的方法,此处是把json字符串中每一个双引号前插入一个反斜杠。