Question 1 I have a config file written by a program which I want to overwrite the values by running a script in terminal.
The config file is laid out like so (there is probably a name for this format, but I don't know what it is):
name_1 = "value1"
name_2 = "value2"
...
The extra spaces I believe are what is causing the issue when I try to read the values in my script file as I get the error "command not found" for each name.
Is there anyway I can include this file in my bash script so it understand that each line is a variable?
Question 2 How can I overwrite values of single lines in that file with the same formatting? The quotation marks in the config file have me confused, as well as the space before and after the = symbol.
Would the following work?
CONFIG_FILE=test.cfg
TARGET_KEY=$"name_1 "
REPLACEMENT_VALUE=" "true" "
sed -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE
For the first question, pre-process your .cfg
file and then read it, like this:
CONFIG_FILE=test.cfg
sed 's/ = /=/g' < test.cfg > /tmp/processed.cfg
. /tmp/processed.cfg
Now, all your name_1
, name_2
pairs will be available to your shell scripts.
For the second question, do it like this:
CONFIG_FILE=test.cfg
TARGET_KEY='name_1 '
REPLACEMENT_VALUE='"true" '
sed -i -e "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE