User avatar
ViperTKD
Lisker
Posts: 66
Joined: Mon Apr 04, 2016 11:06 pm
Location: Quebec, Canada
Contact: Facebook Twitter

How to copy by script specific value(s) from a config.json file to another

Tue Apr 12, 2016 2:54 am

Hi guys,

This is a quick guide on how to copy specific value from a config.json file (or any JSON file) to another. If you're scripting and automating your setup and update like I do, you might find this useful.

This is a first draft, so let me know if you find any bugs or if you have suggestions to improve it.

First, you will need to install 'jq' if you don't have it yet:
> sudo apt-get install jq

Then you can copy&paste this script below:

> nano jsoncopy.sh

------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

#!/bin/bash
# JSON value copy
# Copy value(s) from one JSON file to another
# v0.1
# by ViperTKD

# Usage: jsoncopy [srcfile] [destfile] 'jsonkey1' 'jsonkey2' 'jsonkey...'

SRC=$1
DEST=$2

for i in "${@:3}"; do
    QUERY=$i
    CMDLET="jq $QUERY $SRC"
    VALUE=$($CMDLET)
    echo jq \'$i=$VALUE\' $DEST \> tmp.\$\$.json \&\& mv tmp.\$\$.json $DEST >>tmp.$$.sh
done
bash tmp.$$.sh
rm tmp.$$.sh

------------------------------------------------------------------------------------------------------------------------------------------------

Here are some examples how to use it:

# Copy ssl keys from lisk 0.1.2 to lisk 0.1.3
> bash jsoncopy.sh /opt/lisk-0.1.2-Linux-x86_64/config.json /opt/lisk-0.1.3-Linux-x86_64/config.json '.ssl.options.cert' '.ssl.enabled' '.ssl.options.key'

# Copy forging secret from lisk 0.1.2 to lisk 0.1.3
> bash jsoncopy.sh /opt/lisk-0.1.2-Linux-x86_64/config.json /opt/lisk-0.1.3-Linux-x86_64/config.json '.forging.secret'


Hope you will find it useful and if you do, think about me in your vote! ;-)
Check my Delegate Candidacy : https://forum.lisk.io/viewtopic.php?f=6&t=246

User avatar
Gr33nDrag0n
Lisker
Posts: 123
Joined: Sat Mar 26, 2016 8:22 pm
Location: Quebec, Canada

Re: How to copy by script specific value(s) from a config.json file to another

Tue Apr 12, 2016 2:58 am

Nice ! Will try it and integrate it to my toolset. Good Job :p
Gr33nDrag0n | 194109334904015388L | Delegate | lisknode.io

User avatar
ViperTKD
Lisker
Posts: 66
Joined: Mon Apr 04, 2016 11:06 pm
Location: Quebec, Canada
Contact: Facebook Twitter

Re: How to copy by script specific value(s) from a config.json file to another

Tue Apr 12, 2016 3:01 am

Gr33nDrag0n wrote:Nice ! Will try it and integrate it to my toolset. Good Job :p


Thanks! Let me know if you find any bugs!
Check my Delegate Candidacy : https://forum.lisk.io/viewtopic.php?f=6&t=246


User avatar
Gr33nDrag0n
Lisker
Posts: 123
Joined: Sat Mar 26, 2016 8:22 pm
Location: Quebec, Canada

Re: How to copy by script specific value(s) from a config.json file to another

Tue Apr 12, 2016 3:50 pm

Integration done in my lisk ssh toolset. Thanxs for your permission.
Gr33nDrag0n | 194109334904015388L | Delegate | lisknode.io

Return to “Guides”

Who is online

Users browsing this forum: No registered users and 1 guest