0

I am writing a script to alter the few values in file sysctl.conf in /etc directory

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

For example:

sysctl.conf file contains the following lines (the terms in parentheses are my own comments and are not in the actual file):

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

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

script:

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

2 Answers 2

2

You can do something like this

 sed -i "s|\("kernel.shmall" *= *\).*|\14194304|" /etc/sysctl.conf

And same you can use for other properties.

Also if property not exist then you can do like this

if grep -o "kernel.shmall" /etc/sysctl.conf > /dev/null
then
     oldvalue=$(grep kernel.shmall /etc/sysctl.conf | awk '{ print $3 }')

     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
Sign up to request clarification or add additional context in comments.

8 Comments

@user3541225 i give you only example here forkernel.shmall but you can do use same command for other properties with respective values.
out put of grep kernel.shmall /emblocal/sysctl.conf2| awk '{ print $1}' kernel.shmall=4194304 I am trying to delimit =
@user3541225 It seems correct...have you getting expected result?
achived it by following grep -v '^#' /emblocal/sysctl.conf|grep kernel.shmall|sed 's/=/ /g'| awk '{ print $2}'
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 throwing error [: -lt: unary operator expected because as there is no variable "kernel.shmall" in file. How to add it as above script is not adding when there is no variable
|
1

Perhaps a clearer way of getting the values from your file is:

value=$(grep kernel.shmall /etc/sysctl.conf | awk '{ print $3 }')

Then, to replace the value in the file:

sed -i "/kernel.shmall =/ s/$value/4194304/" /etc/sysctl.conf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.