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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-09-2008, 09:12 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2008
Posts: 1
Rep:
|
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
|
|
|
|
09-09-2008, 08:46 PM
|
#2
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
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)
|
|
|
|
09-09-2008, 11:50 PM
|
#3
|
|
LQ Newbie
Registered: Apr 2008
Posts: 2
Rep:
|
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
|
|
|
|
09-09-2008, 11:56 PM
|
#4
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
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.
|
|
|
|
09-10-2008, 10:02 PM
|
#5
|
|
LQ Newbie
Registered: Apr 2008
Posts: 2
Rep:
|
That's an interesting idea. Yes, that would simplify things a lot.
|
|
|
|
09-10-2008, 11:13 PM
|
#6
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,938
|
Actually used to work at a Merchant Bank where we used that system 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:57 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|