shell脚本在不通过使用jq命令的情况下解析{"code":200,"message":"响应成功","data":{"cluster":"hd","node":"hd-1","leader":"hd-1"}}字符串获得leader
时间: 2024-01-13 07:03:58 浏览: 188
你可以使用shell脚本中的字符串处理功能来解析JSON字符串并获取leader的值。以下是一个示例代码:
```shell
# 定义JSON字符串
json_str='{"code":200,"message":"响应成功","data":{"cluster":"hd","node":"hd-1","leader":"hd-1"}}'
# 使用字符串处理功能解析JSON字符串
leader=$(echo $json_str | sed 's/.*"leader":"\([^"]*\)".*/\1/')
# 输出leader的值
echo "Leader: $leader"
```
这段代码使用sed命令来提取JSON字符串中的leader值。sed命令中的正则表达式将匹配到的leader值提取出来,并存储在变量leader中。最后,使用echo命令输出leader的值。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![sh](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)