Hi, here's what I came up with:
Code:
SEARCH_TEXT="net.ipv4.ip_forward"
REPLACE_TEXT="net.ipv4.ip_forward = some_other_value"
FILE='sysctl.conf'
grep $SEARCH_TEXT $FILE > /dev/null
if [ $? -eq 0 ] ;
then
# Its found, so now use GNU sed to do inline replacement (-I) with backup.
#VERY IMPORTANT: using current time at part of backup extension tp safeguard backup
current_time=`date +%k%M%S`
sed -i.backup.${current_time} "/${SEARCH_TEXT}/ c\\${REPLACE_TEXT} " $FILE
else
echo "Not found. Adding a line" ;
echo $REPLACE_TEXT >> $FILE
fi