Modify existing values in a file

Hi,
I am trying to write a shell script which will modify existing values and take backup of old values.

file trying to modify is : /etc/sysctl.conf

script name: modify.sh

execute

./modify.sh
enter ref no: 123

add below values in file sysctl.cnf

kernel.shmall = 4194304

kernel.shmmax = 2147483648

kernel.sem = 1280 512000 250 10240

kernel.msgmni = 1024

kernel.shmmni = 4096

fs.file-max = 524288

kernel.pid_max = 120000

net.ipv4.tcp_max_syn_backlog = 8192

net.core.somaxconn=4096

net.core.netdev_max_backlog=16384

net.ipv4.tcp_max_syn_backlog=8192

net.ipv4.tcp_syncookies=1

net.ipv4.tcp_tw_reuse=1

net.ipv4.tcp_keepalive_time = 120

and take backup of sysctl.cnf with ref no

sysctl.cnf_123

and take copy of old values in files by adding "#" infront of old values

/etc/sysctl.conf



#kernel.shmall = 130

#kernel.shmmax = 140

#kernel.sem = 2000 

#kernel.msgmni = 24

#kernel.shmmni = 96

#fs.file-max = 242

#kernel.pid_max = 110

#net.ipv4.tcp_max_syn_backlog = 19

#net.core.somaxconn=09

#net.core.#netdev_max_backlog=638

#net.ipv4.tcp_max_syn_backlog=19

#net.ipv4.tcp_syncookies=0

#net.ipv4.tcp_tw_reuse=0

#net.ipv4.tcp_keepalive_time = 2

What you seem to be trying to do is version control and maintain a history. In general system administration staff don't want to be prompted and want the script to take care of everything. In this case, it could figure out a reference number like

date +%j%m%d%s

. Your method of putting comments in from of changed stanzas will make the file grow. I would simply have a script that would make a backup of the file with a predetermined reference, number, then call of the vi editor to edit the file. The script could be smart enough to recognize if the file actually changed.

Yes, I understand but as i need to change it on multiple servers i would like to have a script. I have tried below but seems not working..

#!/bin/sh

#val = $0
cp /etc/sysctl.conf /etc/test_bck.txt
sed '/kernel.shmmax/d' /etc/sysctl.conf > /etc/test3.txt
sed '/kernel.sem/d' /etc/test3.txt > /etc/test4.txt
echo "kernel.shmmax = 2147483648" >> /etc/test4.txt
echo "kernel.sem = 1280 512000 250 10240" >> /etc/test4.txt

What isnt working?

I tried to alter the values which i want. I have mentioned those values above. and i wrote above script and its not working to modify

Where is this script failing?

#!/bin/sh

#val = $0
cp /etc/sysctl.conf /etc/test_bck.txt   # Here already?
sed '/kernel.shmmax/d' /etc/sysctl.conf > /etc/test3.txt # here?
sed '/kernel.sem/d' /etc/test3.txt > /etc/test4.txt # in which case here also
echo "kernel.shmmax = 2147483648" >> /etc/test4.txt # or here?
echo "kernel.sem = 1280 512000 250 10240" >> /etc/test4.txt

ok.. Thanks for your swift responce.. Let me tell you what i am trying to do here..

i am trying to edit "sysctl.conf" file.. if this file has got below variables with below values or morethan that then it should leave that value...

If variable value is less than that then it should alter the value to below given value

For example:

sysctl.conf file has below values

kernel.shmall = 5194304 (more than 4194304 so it should leave it as it is)

kernel.shmmax = 2147483648 (it is equal to 2147483648 so it should leave it as it is)

kernel.msgmni = 124 (it is less than 1024 so it should alter the value to 1024)

expecting output is

kernel.shmall = 5194304 

kernel.shmmax = 2147483648

kernel.msgmni = 1024


will you able to help me on this to write a script or weather i am going in right direction to achive above output by previously shared script

---------- Post updated at 09:39 AM ---------- Previous update was at 03:49 AM ----------

This is my script i am preparing to replace "kernel.shmall" if value is less than equal 4194303

script:


#!/bin/sh
echo `cat /emblocal/sysctl.conf|fgrep kernel.shmall`|while read kernel.shmall
if
[ "kernel.shmall" -le 4194303 ];
then
kernel.shmall = 4194304
fi

output of

cat /emblocal/sysctl.conf|fgrep kernel.shmall

is

kernel.shmall = 419430 #kernel.shmall = 2097152  

it should take only kernel.shmall, not which are commented "#" like #kernel.shmall

can you let me know how to do it?

I have tried below code and it is working but it is failing where there is no "kernel.shmall" parameter in file /etc/sysctl.conf

if grep -o "kernel.shmall" /etc/sysctl.conf > /dev/null
      then
            oldvalue=$(grep -v '^#' /etc/sysctl.conf|grep kernel.shmall|sed 's/=/ /g'| awk '{ print $2}')
      
            if [ $oldvalue -lt 4194304 ]
            then
               sed -i "s|\("kernel.shmall" *= *\).*|\14194304|" /etc/sysctl.conf
            fi
       else
           echo "kernel.shmall=" >> /etc/sysctl.conf
           sed -i "s|\("kernel.shmall" *= *\).*|\14194304|" /etc/sysctl.conf
      fi

is there any modifications do i need to do in script to add "kernel.shmall=4194304" value to file sysctl.conf where there is no parameter kernel.shmall