Shell script to read specified value from file and echo to the same location to other file.

Hello.
I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back.

The problem is that the source and destination file has a lot of default: strings in it but with different values...

So..
Here is an example:

A part of my source file named "config.json".

    { SPane:{ 
        title:"Blinking effect",
        description:"Control the way the LED blinks."
    }},
    { SCheckBox:{
        description:"Completely disable led notifications.", 
        label:"Disable LED",
        default:0,
                action:"generic /sys/class/sec/led/led_completely_disable"
    }},
    { SCheckBox:{
        description:"If disabled, the controller bypasses slope generation.", 
        label:"Enable LED fading",
        default:1,
                action:"generic /sys/class/sec/led/led_fade"
    }},
    { SSeekBar:{
        title:"Fade-in time period",
        description:"Sets the time period of the rising slope.",
        min:0, max:12, unit:"ms", step:1,
        default:2,
                action:"generic /sys/class/sec/led/led_fade_in_time"
    }},
    { SSeekBar:{
        title:"Fade-out time period",
        description:"Sets the time period of the falling slope.",
        min:0, max:20, unit:"ms", step:1,
        default:8,
                action:"generic /sys/class/sec/led/led_fade_out_time"
    }},

With the code:

echo "$(cat config.json | grep default: | awk -F default '{print $2 }' | awk -F , '{print $1 }')" > def;

This it will output all default values to "def" file.
Like:

:0
:1
:2
:8

Note that some values also have strings in it...

So after some other commands the source file change those default values and simply :cool: i want to restore those values from "def" file to "config.json" file.

Wish to explain it ok.

Thanks

Following might work for you:

"Save" default values

$ grep default: config.json >def

"Restore" script:

#!/bin/bash

#following command requires bash 4
readarray -t arr <def
index=0

function replace {
 echo "${arr[$index]}"
 index=$((index+1))
}

while IFS= read line
do
  if [[ "$line" =~ "default:" ]]; then
   replace
  else
   echo "$line"
  fi
done <config.json >config.json.new

# mv config.json.new config.json

Script explanation: config.json is read line by line. If the line contains a default: string, the first occurrence of default: gets replaced with the first occurrence of default: from the def file, else the (unmodified) line is printed. Whole output is redirected to config.json.new, which basically creates a new config.json file. Uncomment the last line if you're happy with the output.

1 Like

The code is working exactly as i want but i forgot to say that it will used on an android device - terminal...

So as i use it in android terminal i change:

#!/system/bin/sh

but it gives me:

line[4]:readarray: not found
line[14]: syntax error: '=~' unexpected operator/operand

Many thanks

That is really too bad, because almost everything is bash-specific in my script. Unfortunately I have no prerequisites to help you further. I could rewrite it in perl, if it's an option.

1 Like

You could try this instead, which is a translation of junior-helper's "restore" bash 4 code into more universal shell code and should run in any POSIX compliant shell, including bash and ksh:

while IFS= read -r line
do
  case $line in
    (*default:*) IFS= read -r line <&3
  esac
  printf "%s\n" "$line"
done <config.json 3<def >config.json.new

# mv config.json.new config.json
2 Likes

Change your awk backup so it saves the rest of the default: line

awk '/default:/ {sub (/.*default/,""); sub (/,.*/,","); print}' config.json >def

Or with sed

sed '/default:/!d; s/.*default//; s/,.*/,/' config.json >def

And restore with the following shell script

while read line
do
  case $line in
  *default:*)
    read replace <&3
    echo "$line" | sed 's/\(.*default\)[^,]*\(.*\)/\1'"$replace"'/'
  ;;
  *)
    echo "$line"
  esac
done < config.json 3< def > config.json.new
# mv config.json.new config.json

---------- Post updated at 03:08 PM ---------- Previous update was at 02:48 PM ----------

I didn't see Junior's proposal. Storing the whole line is most efficient!
And missed Scrutinizer's post. Go for that one!

1 Like

After compiled bash for arm your script is working... :wink:

Exactly what i want...
Working also on arm without any further requirements. :b:

Many thanks to all of you.