shell中json使用
# 直接写入【推荐】
# 提交在web目录下处理打印版本信息
printVersion() {
cd ${curDir}
echo "==================================printVersion===================================="
curBranch=$(git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)|\1|' | sort -u | grep -v HEAD)
commitID=$(git rev-parse --short HEAD)
commitTime=$(git show --pretty=format:"%ci" | head -1)
buildTime=$(date "+%Y-%m-%d %H:%M:%S")
echo "{
\"version\":{
\"curBranch\": \"$curBranch\",
\"commitID\": \"$commitID\",
\"commitTime\": \"$commitTime\"
},
\"buildCfg\":{
\"buildTime\": \"$buildTime\",
\"envName\":\"$envName\",
\"isPro\":$isPro,
\"defaultLang\":$defaultLang,
\"isHideLang\":$isHideLang,
\"isTokenAuth\":$isTokenAuth
}
}" > $versionDist
echo "=====版本记录生产version.json成功========"
cat $versionDist
}
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
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
生成后的数据:
{
"version": {
"curBranch": "1.1.1",
"commitID": "9431f49",
"commitTime": "2022-01-13 17:48:26 +0800"
},
"buildCfg": {
"buildTime": "2022-01-13 18:45:22",
"envName": ".env.defaultCookie",
"isPro": true,
"defaultLang": "zh",
"isHideLang": false,
"isTokenAuth": false
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 动态替换
#!/bin/bash
>r.json
OLD_IFS=$IFS
export IFS=","
sentence='"衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"'
sentence2='5, 20, 36, 10, 10, 20'
s_arr=($sentence)
s2_arr=($sentence2)
IFS="$OLD_IFS"
IFS=";"
template=" {\"name\":var1,\"value\":var2},"
# template=" \"name\":var1,\"value\":var2,"
# echo '{' >>r.json
for i in ${!s_arr[@]}; do
var1=${s_arr[$i]}
var2=${s2_arr[$i]}
# sed 's/var1/$var1/g' $template | sed 's/var2/$var2/g'>>r.txt
echo $template | sed "s/var1/$var1/g" | sed "s/var2/$var2/g" >>r.json
done
# echo '}' >>r.json
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
生成后的数据:
{"name":"衬衫","value":5},
{"name":"羊毛衫","value": 20},
{"name":"雪纺衫","value": 36},
{"name":"裤子","value": 10},
{"name":"高跟鞋","value": 10},
{"name":"袜子","value": 20},
1
2
3
4
5
6
2
3
4
5
6
上次更新: 2022/04/15, 05:41:30