0

I need to read a value of variable and set it to zero and again set back to old value I tried these steps

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" # say $value is 3
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"

The above code is setting the value Max_value to $value[I mean actual to text $value but not value contained it which is 3 like "Max_value=$value" and not to Max_value=3] but not to old value

If I reset it to zero and try to set it back to old value How would I do that ?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
JumpOffBox
  • 703
  • 3
  • 10
  • 19

3 Answers3

0

Like I said in my answer to your last question, you should use a config parser:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('post_check.ini')

print config.get('section 1','Max_value')

config.set('section 1','Max_value','0')
print config.get('section 1','Max_value')

Demo

$ cat post_check.ini
[section 1]
Max_value=123
[section 2]
Max_value=456

$ python config.py
123
0 
Community
  • 1
  • 1
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • What is the relevance? You don't need an X server! – Chris Seymour Apr 03 '13 at 17:53
  • That is what the Error I am getting when I run the script – JumpOffBox Apr 03 '13 at 17:57
  • How did you try and run the script? – Chris Seymour Apr 03 '13 at 17:58
  • I am doing a shell script and that is just a part of shell script, I cannot change entire thing to python now – JumpOffBox Apr 03 '13 at 18:00
  • The code you have posted is a complete mess, you wouldn't have to rewrite the whole script *(although you probably should in this case)* you just add one line `python config.py`. Obviously you cannot copy and paste the python code into a shell script and expected it to work. – Chris Seymour Apr 03 '13 at 18:05
  • That is what I am saying I don't want to call a separate python script for a 4 lines of code, I did not copy and pasted it, I made changes accordingly, I am not sure what the mess is ? I read a value from post_check.ini, I set it to 0 and then after some time I want to reset the value back to old value I had read. – JumpOffBox Apr 03 '13 at 18:11
  • Thanks for your help. I figured it out, double quotes was the issue – JumpOffBox Apr 03 '13 at 18:20
  • You could use crudini which is designed to be call from shell script and has the config parser built in – pixelbeat Jan 12 '15 at 12:11
0

sed -i -r "s/Max_value=[0-9]+/Max_value=$value/g" /usr/master.ini

JumpOffBox
  • 703
  • 3
  • 10
  • 19
0

The particular issue with $value being inserted directly was because you used single quotes instead of double quotes.

However 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)
crudini --set /usr/post_check.ini section Max_value 0
echo "$value"
crudini --set /usr/post_check.ini section Max_value "$value"
value=$(crudini --get /usr/post_check.ini section Max_value)
echo "$value"

Details on usage and download at: http://www.pixelbeat.org/programs/crudini/

pixelbeat
  • 30,615
  • 9
  • 51
  • 60