04-23 周二 shell环境下读取使用jq 读取json文件

embedded/2024/9/23 1:08:52/
04-23 周二 shell环境下读取使用jq 读取json文件
时间版本修改人描述
04-23V0.1宋全恒新建文档

简介

工具列表

Shell脚本处理JSON数据工具jq

jshon是另外一个读取json数据的工具 而且其支持XML和YAML格式文件

linux shell环境下处理yml文件

#!/bin/bash# 加载shyaml库
. /usr/local/bin/shyaml.sh# 读取YAML文件
mapfile -t yaml_content < config.yaml# 解析YAML内容
eval $(shyaml parse <<<"${yaml_content[*]}")# 使用解析出来的变量
echo $name
echo ${servers[0]}

[如何在shell脚本中从yaml文件读取特定数据](javascript:void(0)😉

看来要用shyaml 和yq来进行参数的读取。

  • json
    • jq
    • jshon
  • yaml文件
    • shyaml
    • yq

使用cat和jq演示配置文件的读取

配置文件config.json

ci_repositories:- user: "user1"passwd: "password1"url: "http://ci.example.com/repository1"tags:- "tag1"- "tag2"- "tag3"- user: "user2"passwd: "password2"url: "http://ci.example.com/repository2"tags:- "tag4"- "tag5"- user: "user3"passwd: "password3"url: "http://ci.example.com/repository3"tags: []

注: 上述展示了一个yaml文件,在下面程序运行时,应该将其转化为json文件。

yangfei@ubuntu:/home/songquanheng/json$ cat config.json 
{"ci_repositories": [{"user": "user1","passwd": "password1","url": "http://ci.example.com/repository1","tags": ["tag1","tag2","tag3"]},{"user": "user2","passwd": "password2","url": "http://ci.example.com/repository2","tags": ["tag4","tag5"]},{"user": "user3","passwd": "password3","url": "http://ci.example.com/repository3","tags": []}]
}

程序脚本

#!/bin/bash# 读取 JSON 文件并遍历 ci_repositories
jq -c '.ci_repositories[]' config.json | while IFS= read -r repository; do# 从每个 repository 中提取 user、passwd、url 和 tagsUser=$(echo "$repository" | jq -r '.user')Passwd=$(echo "$repository" | jq -r '.passwd')Url=$(echo "$repository" | jq -r '.url')# 将 Tags 数组存储为 Shell 数组变量Tags=$(echo "$repository" | jq -r '.tags | join(" ")')read -r -a TagsArray <<<"$Tags"# 获取 TagsArray 数组的长度TagsLength=${#TagsArray[@]}# 获取 TagsArray 数组的第一个元素Tag1=${TagsArray[0]}# 打印变量值(可替换为你想要的处理逻辑)echo "User: $User"echo "Passwd: $Passwd"echo "Url: $Url"echo "Tags: ${TagsArray[@]}"echo "Tags Length: $TagsLength"echo "Tag at index 1: $Tag1"echo "------------------------"
done

代码解析

 代码解析

image-20240423143052301

shell执行结果

 上述的代码,在执行时输出了如下的结果

songquanheng@ubuntu:~/json$ bash b.sh 
User: user1
Passwd: password1
Url: http://ci.example.com/repository1
Tags: tag1 tag2 tag3
Tags Length: 3
Tag at index 1: tag1
------------------------
User: user2
Passwd: password2
Url: http://ci.example.com/repository2
Tags: tag4 tag5
Tags Length: 2
Tag at index 1: tag4
------------------------
User: user3
Passwd: password3
Url: http://ci.example.com/repository3
Tags: 
Tags Length: 0
Tag at index 1: 

总结

 上文演示了使用jq读取yaml的配置文件,并且处理了解读过程中的配置文件。通过使用在shell中使用jq命令,我们可以非常方便的进行配置文件的读取。非常的方便。


http://www.ppmy.cn/embedded/37656.html

相关文章

中国棋手再夺世界桂冠,李轩豪问鼎第五届梦百合杯世界围棋公开赛

5月4日,第五届“MLILY梦百合0压床垫杯”世界围棋公开赛决赛在江苏省如皋市圆满结束,中国棋手党毅飞和李轩豪展开巅峰对决,最终,李轩豪执白168手战胜党毅飞,以3比1的比分夺得桂冠并获得由MLILY梦百合赞助的180万冠军奖金,这是李轩豪个人职业生涯中首次问鼎世界冠军,成为世界棋坛…

计算机视觉与深度学习实战之以Python为工具:基于融合特征的以图搜图技术

注意:本文的下载教程,与以下文章的思路有相同点,也有不同点,最终目标只是让读者从多维度去熟练掌握本知识点。 下载教程:计算机视觉与深度学习实战-以MATLAB和Python为工具_基于融合特征的以图搜图技术_项目开发案例教程.pdf 一、引言 随着信息技术的飞速发展,图像数据呈…

【MsSQL】数据库基础 库的基本操作

目录 一&#xff0c;数据库基础 1&#xff0c;什么是数据库 2&#xff0c;主流的数据库 3&#xff0c;连接服务器 4&#xff0c;服务器&#xff0c;数据库&#xff0c;表关系 5&#xff0c;使用案例 二&#xff0c;库的操作 1&#xff0c;创建数据库 2&#xff0c;创建…

Promise魔鬼面试题

文章目录 题目解析难点分析分析输出step1step2step3step4step5step6 参考/致谢&#xff1a;渡一袁老师 题目 Promise.resolve().then(() > {console.log(0);return Promise.resolve(4);}).then((res) > {console.log(res);});Promise.resolve().then(() > {console.l…

【负载均衡在线OJ项目日记】项目简介

目录 前言 什么是负载均衡 所用的技术和开发环境 所用技术 开发环境 项目的宏观结构 leetcode 结构 结构 编写思路 前言 从C语言的文章到现在Linux网络部分&#xff0c;我已经涉猎了很多知识&#xff1b;终于在今天我要开始搞项目了&#xff0c;通过项目我也可以开始…

【C++历练之路】红黑树——map与set的封装实现

W...Y的个人主页&#x1f495; gitee代码仓库分享&#x1f60a; 前言&#xff1a;上篇博客中&#xff0c;我们为了使二叉搜索树不会出现”一边倒“的情况&#xff0c;使用了AVL树对搜索树进行了处理&#xff0c;从而解决了数据在有序或者接近有序时出现的情况。但是AVL树还会…

探索(淘宝京东1688)电商API接口:新一代电商问题解决方案

前言 随着电子商务的蓬勃发展&#xff0c;电商平台的API接口成为了连接商家、开发者与电商平台的桥梁。淘宝、京东、1688等主流电商平台提供的API接口&#xff0c;不仅极大地丰富了电商服务的多样性&#xff0c;也为解决传统电商问题提供了新的解决方案。本文将深入探讨这些电商…

基于SSM的农产品销售管理系统

文章目录 项目介绍一、项目功能介绍二、部分页面展示三、部分源码四、底部获取全部源码&#xff08;9.9&#xffe5;带走&#xff09; 项目介绍 农产品销售管理系统 一、项目功能介绍 一、介绍 系统分为两个角色 用户功能&#xff1a;登陆&#xff0c;注册&#xff0c;商品分…