Reputation: 29
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`
Upvotes: 0
Views: 1239
Reputation: 25865
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
Upvotes: 2
Reputation: 901
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
Upvotes: 1