on some of my machines i have this block of setting in sysctl.conf(the numbers are different on each machine) :

#Size Of RAM (In GB):   23
kernel.shmmni=  2547
kernel.shmmax=  365482458
kernel.shmall=  25471984
kernel.sem=     325     12496852  74      3654
kernel.msgmni=  1204
kernel.msgmax=  57452
kernel.msgmnb=  74235

the location of this block is different sometimes. i wanted to write a bash that remove this old settings and insert new configuration. since it is sysctl.conf i need to do it safely. how i can remove them with sed or grep and be sure that only this block will remove?


If that is the only place in the file where those lines appear:

sed '/#Size Of RAM (In GB):   23/,/kernel.msgmnb/d' /etc/sysctl.conf

That searches for everything in between and including the first and last lines of that block and deletes it. I included the first line in its entirety instead of a smaller string in case Size or something else in it appears elsewhere in the file. I have also used kernel.msgmnb as you've stated that the number is different on other machines.

That command will send the changes to stdout so that you can make sure that the changes are what you want. If they are, you can do an inplace edit of the file by adding the -i switch:

sed -i '/#Size Of RAM (In GB):   23/,/kernel.msgmnb/d' /etc/sysctl.conf
| improve this answer | |
  • this kernel.msgmnb exist in configuration file. it won't make mistakes, does it? – BlackCrystal Jun 22 '19 at 12:09
  • 1
    @BlackCrystal No. It will look for the first instances of #Size Of RAM (In GB): 23 and kernel.msgmnb= 74235. kernel.msgmnb is not identical to your last line so even if it were in that block of text around the middle, the commands would still work. Give the first command without the -i switch a try to make sure that it does what you want as it will only sends the changes to stdout. – Nasir Riley Jun 22 '19 at 12:18
  • numbers are different on each machine. i can't use kernel.msgmnb= 74235. i should use kernel.msgmnb. that's why i asked. – BlackCrystal Jun 22 '19 at 12:21
  • 1
    @BlackCrystal You can do it that way. It will still work as long as long as the block looks like what you have above and kernel.msgmnb doesn't appear inside of that code block before it does on the last line. See my update. – Nasir Riley Jun 22 '19 at 12:25
  • @NasirRiley OP also asked for the script to insert new settings, would you consider using the "c \ " switch for sed to replace the entire block of text with the new settings? – Jeff Jun 22 '19 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.