LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-21-2002, 06:45 PM   #1
360
Member
 
Registered: Jun 2001
Distribution: FC4
Posts: 136

Rep: Reputation: 15
I need a quick shell script.


Hi,

Need a quick shell script or sed command that will change the ip address from one ip to another in every zone file in the dir /etc/named.d

Also need a script that will change the ip address from one ip to another in my apache conf file.

I was able to find and replace the ip address in the apache conf file with sed but could not figure out how to make it write the whole file with changes to a new file or save it to the existing file.

If you have a suggestion, please be very specific. I am still fairly new at linux.

Thanks for your help.
 
Old 03-22-2002, 08:51 AM   #2
Mik
Guru
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Gentoo + LFS
Posts: 1,316

Rep: Reputation: 46
Here's how you would change the ip for one file:

sed -e "s/192.168.1.1/192.168.5.1/" originalfile > tempfile
mv -f tempfile originalfile

Adding a loop to do every zone file isn't that much harder.
 
Old 03-22-2002, 10:12 AM   #3
360
Member
 
Registered: Jun 2001
Distribution: FC4
Posts: 136

Original Poster
Rep: Reputation: 15
Thanks Mik,

The code works perfect!

Are you up for helping me with the loop code that will look in every file of one dir for ip address changes. I have a lot of zone files and would hate to do it with vi.

Thanks again.
 
Old 03-22-2002, 04:44 PM   #4
Sixpax
Member
 
Registered: Mar 2002
Distribution: Mandrake 8.1
Posts: 386

Rep: Reputation: 30
#!/bin/ksh
#Make sure you run this script from the dir where the files to change are.
#Pass the origional IP as the first argument and the new IP as the second.
if [ "$1" = "" -o "$2" = "" ]; then
echo "Usage: $(basename $0) <Origional IP> <New IP>"
exit
fi
echo "Origional IP = $1"
echo "New IP = $2"
for FILE in $(find . -type f); do
if $(grep -q "$1" $FILE); then
echo "Changing file: $FILE"
sed -e "s/$1/$2/g" $FILE > /tmp/newfile.tmp
if [ $? -eq 0 ]; then
#Make a copy of the origional file in case something goes wrong.
cp -f $FILE $FILE.bak
mv -f /tmp/newfile.tmp $FILE
fi
fi
done
echo "Done... check results then delete files with .bak extension"
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


All times are GMT -5. The time now is 03:37 PM.

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
Open Source Consulting | Domain Registration