Shell 读取文件 截取字符串

创建test.sh内容如下: 

for line in `cat $1`
do
echo "===source str ==="
echo $line
firstStr=`echo $line | cut -d \: -f 1`
length=$[${#firstStr}-2]
key=${firstStr:1:$length}
echo "===key str==="
echo $key
firstStr=`echo $line | cut -d \: -f 2`
length=$[${#firstStr}-2]
value=${firstStr:1:$length}
echo "===value str ==="
echo $value
echo "---success---"
done

创建test.json内容如下:

"groupMode":"clusterAsGroup"
"enableRegister":"true"
"customize":"22"
"enableBeta":"false1"
"clearWk":"false2"
"groupSize":"2"
"shareHost":"false3"

执行脚本: 传入test.json文件名为参数

    sh test.sh test.json

结果如下:

[root@see-k8s-master1 ~]# sh test.sh.0 test.json 
===source str ===
"groupMode":"clusterAsGroup"
===key str===
groupMode
===value str ===
clusterAsGroup
---success---
===source str ===
"enableRegister":"true"
===key str===
enableRegister
===value str ===
true
---success---
===source str ===
"customize":"22"
===key str===
customize
===value str ===
22
---success---
===source str ===
"enableBeta":"false1"
===key str===
enableBeta
===value str ===
false1
---success---
===source str ===
"clearWk":"false2"
===key str===
clearWk
===value str ===
false2
---success---
===source str ===
"groupSize":"2"
===key str===
groupSize
===value str ===
2
---success---
===source str ===
"shareHost":"false3"
===key str===
shareHost
===value str ===
false3
---success---

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区