bash - output of shell script in json form -


i want output of following small shell script in json form.

#!/bin/bash   top -b -d1 -n1 | grep cpu  

output:

cpu(s):  6.2%us,  1.6%sy,  0.2%ni, 90.9%id,  1.1%wa,  0.0%hi,  0.0%si,  0.0%st 

required output:

{"cpu": "6.3" } 

how can convert output of such every shell scripts in json form ?

you try this

echo "{\"cpu\":\"`top -b -d1 -n1 | grep cpu | cut -f3 -d " " | cut -f1 -d %`\"}" 

a brief description: first, take @ man cut, -f , -d arguments. \"s double quotations, should preceded backslash avoid misunderstanding shell interpreter. , @ last, enclosed in quotation marks `` executed, described here.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -