LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-09-2008, 09:12 AM   #1
knightto
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
Question bash script to create text in a file or replace value of text if already exists


I have 20+ servers built with the same build of RedHat.
There are multiple files who's contents I need to change on each box.
I'm guessing some sort of script that does the following is what I need but don't know syntax.
--
Script that does:
1. Searches each file for specific text.
a. if text not found (ADD FIELD=VALUE)
b. if text IS found (REMOVE LINE and ADD FIELD=VALUE).

I'm looking to:
add/modify grub password in /etc/grub.conf
change PUBLIC to 'other value' in /etc/snmp/snmp.conf
add/modify lines in /etc/sysctl.conf

IF someone could give me an idea on some sort of IF loop that looks for TEXT, replaces entire line if exists
IF doesn't exist
ADD LINE

That should help.

Thanks in advance
 
Old 09-09-2008, 08:46 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
I'd recommend Perl for that. Im hoping you don't allow root ssh logins. Instead designate a normal user to have sufficient privs via sudo and use that.Perl has modules for you to be able to script your stuff even under those requirements.
See
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
search.cpan.org


Actually, this is exactly the sort of issue Perl was originally designed for. There's even an O'Reilly book:
http://oreilly.com/catalog/9781565926097/index.html (Perl for System Admin)
 
Old 09-09-2008, 11:50 PM   #3
chankle
LQ Newbie
 
Registered: Apr 2008
Posts: 2
Blog Entries: 1

Rep: Reputation: 0
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
 
Old 09-09-2008, 11:56 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
rsync might be an idea for files whose contents you want to be the same everywhere.
Even if not, you can build a 'correct' set on a master box, then just loop/rsync them across. Might be simplest option.
 
Old 09-10-2008, 10:02 PM   #5
chankle
LQ Newbie
 
Registered: Apr 2008
Posts: 2
Blog Entries: 1

Rep: Reputation: 0
That's an interesting idea. Yes, that would simplify things a lot.
 
Old 09-10-2008, 11:13 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Actually used to work at a Merchant Bank where we used that system
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: replace a line in text file freeindy Programming 10 09-08-2011 12:08 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 05:59 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM


All times are GMT -5. The time now is 03:15 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration