Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 37313  / 3 Years ago, tue, june 8, 2021, 4:13:45

I want to create bash script that it converts output of other scripts (Normally simple text) in a valid json form. How can I do this ?



Example:



awk '/^Mem/ {printf("%u%%", 100*$3/$2);}' <(free -m)


command give me used memory. I want to get output in valid jason form.



Desired output:



 {“Memory”:”80”}

More From » bash

 Answers
6

Here's an example for memory:



echo {"Memory":"$(awk '/^Mem/ {printf("%u", 100*$3/$2);}' <(free -m))"} > mem.json


Putting that new file name into json:



echo {"file":"$(ls mem.json)"} > filename.json


Or:



echo {"<paramName-here>":"$(<value-of-param-from-command-here>)"} > mem.json 


When it gets more complicated than this, you can continue to write line by line or more likely build strings in a variable.


[#32029] Wednesday, June 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bleger

Total Points: 468
Total Questions: 108
Total Answers: 100

Location: Belarus
Member since Wed, Dec 7, 2022
1 Year ago
bleger questions
Wed, Mar 1, 23, 03:00, 1 Year ago
Mon, Feb 6, 23, 14:44, 1 Year ago
Wed, Dec 29, 21, 13:59, 2 Years ago
;