How do I get value of variable from file using shell script?
10,055
Solution 1
try
grep -Po '(?<=Max_value=).*' post_check.ini
Solution 2
Max_value=$(sed -n '/^Max_value=\([0-9]*\)$/s//\1/p' post_check.ini)
Solution 3
I recommend using crudini which is a dedicated tool to manipulate ini files from shell
value=$(crudini --get /usr/post_check.ini section Max_value)
Details on usage and download at: http://www.pixelbeat.org/programs/crudini/
Author by
JumpOffBox
Updated on June 05, 2022Comments
-
JumpOffBox almost 2 years
There is a file
post_check.ini
I need the value set for:Max_value=2
How would I get the value
2
fromMax_value
?-
tripleee about 8 yearsPossible duplicate of bash get string after character
-
-
JumpOffBox about 11 yearsThis one works with one change of, I added the awk '{print $1}' to get first part else it would return whole line. But what if I want to set back the value I got value=$(grep -Po '(?<=Max_value=).*' /usr/post_check.ini|awk '{print $1}') sed -i -r 's/Max_value=[0-9]+/Max_value=0/g' /usr/master.ini echo "$value" sed -i -r 's/Max_value=[0-9]+/Max_value=$value/g' /usr/master.ini value=$(grep -Po '(?<=Max_value=).*' /usr/master.ini|awk '{print $1}') echo "$value" If I reset it to zero and try to set it back to old value How would I do that ?
-
Kent about 11 years@JumpOffBox grep can only extract data. cannot set data. you may need awk/sed to do that. But that would be another new question. You could open a new question. Please don't write big block code in comment, it is hard to read.
-
tripleee about 8 yearsPoints for the use of an empty pattern after
s
but maybe simply's/^Max_value=//p'
would be easier to type and comprehend.