From 1fcad6d6808fc220a4c6f667ca653eb1da11d4c4 Mon Sep 17 00:00:00 2001 From: Kiolow Date: Fri, 7 Jun 2024 13:10:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ebash=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/utils/jq_util.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 bin/utils/jq_util.sh diff --git a/bin/utils/jq_util.sh b/bin/utils/jq_util.sh new file mode 100644 index 0000000..64dfc01 --- /dev/null +++ b/bin/utils/jq_util.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# 修改json文件的属性值 +# 调用方式:modify_json_file "/foo/bar.json" "person.name" "张三" +function modify_json_file() { + local json_file=$1 # json文件路径 + local key=$2 # 要修改的key + local value=$3 # 要修改的value + + #如果key的值是.开头, 则去掉.号 + if [[ ${key} == "."* ]]; then + key=${key:1} + fi + + jq ".${key} = \"${value}\"" ${json_file} > ${json_file}.tmp + mv ${json_file}.tmp ${json_file} +}