使用Bash Shell处理JSON文件

前言

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,,易于人阅读和编写,同时也易于机器解析和生成。本文提供一个真实的测试用例需求,设计逻辑类似Makefile,我以Bash处理JSON为例,Coding水平有限,请各位多多包涵哈,欢迎大家一起学习和挑战各种不同的语言来实现。

巧用jq处理JSON数据

更新历史

2015年06月19日 – 初稿

阅读原文 –

扩展阅读

JSON – jq – Test Case

In data pipeline system and configuration management systems, it’s very common that you need execute a bunch of jobs which has dependencies with each other.

Write a program pipeline_runner to execute a list of shell scripts. The definition of those scripts and their dependencies are described in a JSON file. The program only takes in one argument which is the file path of JSON file that defines the jobs.

For example, // jobs.json

{“log0_compressed” : {“commands”: “curl > access0.log.gz”,”input”: [],”output”: “access0.log.gz”},”log0″ : {“commands”: “gunzip access0.log.gz”,”input”: [“access0.log.gz”],”output”: “access0.log”},”log1_compressed”: {“commands”: “curl > access1.log.gz”,”input”: [],”output”: “access1.log.gz”},”log1″ : {“commands”: “gunzip access1.log.gz”,”input”: [“access1.log.gz”],”output”: “access1.log”},”log_combined”: {“commands”: “cat access0.log access1.log > access.log”,”input”: [“access0.log”, “access1.log”],”output”: “access.log”}}

To run the program

pipeline_runner jobs.json

As you can see, each job has its input files and output files. – A job will only be executed if all its input files exist. – A job can have multiple input files (or none) but only produce one output file. – Users could run the program multiple times, but if a job’s output file already exists, the program would skip the job.

If you’re still not very clear, think of Makefile in Linux systems. The logic is quite similar.

You could complete the test with the programming language you preferred.

Bash Shell#!/bin/bashrunuser=root# commandslog_combined_commands=`cat jobs.json | ./jq -r ‘.log_combined.commands’`log1_commands=`cat jobs.json | ./jq -r ‘.log1.commands’`log1_compressed_commands=`cat jobs.json | ./jq -r ‘.log1_compressed.commands’`log0_commands=`cat jobs.json | ./jq -r ‘.log0.commands’`log0_compressed_commands=`cat jobs.json | ./jq -r ‘.log0_compressed.commands’`# input file namelog0_input=`cat jobs.json | ./jq -r ‘.log0.input[0]’`log1_input=`cat jobs.json | ./jq -r ‘.log1.input[0]’`log_combined_input1=`cat jobs.json | ./jq -r ‘.log_combined.input[0]’`log_combined_input2=`cat jobs.json | ./jq -r ‘.log_combined.input[1]’`# output file namelog_combined_output=`cat jobs.json | ./jq -r ‘.log_combined.output’`log1_output=`cat jobs.json | ./jq -r ‘.log1.output’`log1_compressed_output=`cat jobs.json | ./jq -r ‘.log1_compressed.output’`log0_output=`cat jobs.json | ./jq -r ‘.log0.output’`log0_compressed_output=`cat jobs.json | ./jq -r ‘.log0_compressed.output’`[ as (){read -n1 -p “Press any key to continue…”}log_combined_check_first(){has been generated, the programe will exit” }log0_compressed_check(){}log0_check(){}log1_compressed_check(){}log1_check(){}log_combined_check(){has been generated, the programe will exit”fi}”Please read first:[0]Check jobs.json and jq by yourself first[1]A job will only be executed if all its input files exist.[2]A job can have multiple input files (or none) but only produce one output file.[3]Users could run the program multiple times, but if a job’s output file already exists, the program would skip the job.”pause#check if file exist and do the joblog_combined_check_firstlog0_compressed_checklog0_checklog1_compressed_checklog1_checklog_combined_check#—————————- main ———————end小结 人只要不失去方向,就不会失去自己

使用Bash Shell处理JSON文件

相关文章:

你感兴趣的文章:

标签云: