Modify existing values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify existing values in a file
# 1  
Old 04-09-2014
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
Code:
./modify.sh
enter ref no: 123

add below values in file sysctl.cnf

Code:
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
Code:
sysctl.cnf_123

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

Code:
/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

# 2  
Old 04-09-2014
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
Code:
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.
# 3  
Old 04-15-2014
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..
Code:
#!/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

# 4  
Old 04-15-2014
What isnt working?
# 5  
Old 04-15-2014
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
# 6  
Old 04-15-2014
Where is this script failing?
Code:
#!/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

# 7  
Old 04-15-2014
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

Code:
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
Code:
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:

Code:

#!/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

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

is

Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern search and modify the values

I have one file and the file may contain 500 to 15,000 records. I need to search pattern ^F509= and then increment the corresponding value by one and print the entire line. Please note that Its not a fixed length file. Can anyone please help? ex: ^F509=204656 ^F509=204656 ... (6 Replies)
Discussion started by: vinus
6 Replies

2. Shell Programming and Scripting

Need to implement new CRON job or modify the existing one in MDM PROD in coming days, which will sen

Need to implement new CRON job or modify the existing one in MDM PROD in coming days, which will send email of SQL statement running from past 1 minute.Currently there is one CRON job in PROD, which send below email. there is one cron job which send email, when there is long running... (0 Replies)
Discussion started by: dampu
0 Replies

3. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

4. Shell Programming and Scripting

how to modify existing env var

greetings, i have the following env variable: LSB_HOSTS='t70cra102 t70cra102 t70cra108 t70cra108'the variable could be any number of host names and sometimes the same name several times. i was hoping there's a way to turn it into the following: NEW_LSB_HOSTS=,,,]always appreciate the help... (2 Replies)
Discussion started by: crimso
2 Replies

5. Shell Programming and Scripting

modify a file by inserting a conditional values

Hi, input file CCCC 1204 215764.85 9405410.40 1189 DDDD 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540 DDDD 5199 1556 5278 1567 5529 1603 5674 1614 6076 1915 DDDD 6605 2371 7004 2779 CCCC ... (4 Replies)
Discussion started by: Indra2011
4 Replies

6. Shell Programming and Scripting

modify Existing MS excel workbook in perl

Hi I need to modify an excel file in perl and for which I installed perl in Linux 1. Open a existing excel file 2. delete an unwanted Sheet called "summary" 3. and i want to insert some data into range of cells ( B1:B11) 4. Remove unwanted value called "Sum" repeated in the... (1 Reply)
Discussion started by: luke_devon
1 Replies

7. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

8. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

9. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

10. UNIX for Advanced & Expert Users

How to modify an existing pdf with unix shell commands

Hi, I know that to create a pdf file I can use the txt2pdf command. But if I would change an existing pdf file, by inserting lines in particular positions of this file, what can I use? And How? (3 Replies)
Discussion started by: fandwick
3 Replies
Login or Register to Ask a Question