Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have this script below, and it works great except in some of the files it searches the serial number line wont be right or the ip# line wont be right, and when sed runs it errors and overwrites the file with a blank 1. is there a way to run sed and if sed doesnt find what you want have it echo "please replace the ip and serial# in $file manually" and then go to the next file and not write to the 1 it has open right now? here is the script hopefully you understand what im saying...
#!/bin/bash
clear
echo "This script will change IP's from an old IP to a new IP in order for cPanel to work correctly."sleep 10
echo "what is the old ip: "
read line
oip="$line"
echo "what is the new ip: "
read line
nip="$line"cp -f /etc/named.conf /var/named/backup/named.conf.backup
echo "changing IP's in: /etc/named.conf"
cat /var/named/backup/named.conf.backup | sed "s/$oip/$nip/g" > /etc/named.conf
cp -f /etc/httpd/conf/httpd.conf /var/named/backup/httpd.conf.backup
echo "changing IP's in: /etc/httpd/conf/httpd.conf"
cat /var/named/backup/httpd.conf.backup | sed "s/$oip/$nip/g" > /etc/httpd/conf/httpd.conf
cp -f /etc/hosts /var/named/backup/hosts.backup
echo "changing IP's in: /etc/hosts"
cat /var/named/backup/hosts.backup | sed "s/$oip/$nip/g" > /etc/hosts
cd /var/namedfor i in *.db
do
todaydate=`date +%m%d%y%H%M`
cp -f $i /var/named/backup/$i.backup
sline=`grep -e "; serial" $i`
snum=`echo $sline | cut -f1 -d' '`
cat /var/named/backup/$i.backup | sed -e "s/$snum/$todaydate/" -e "s/$oip/$nip/" > $i
echo "changing IP's in: $i"
done
echo "Done"
echo "Please restart the machine followed by running /scripts/easyapache"

A lot of uuoc in this script.
There are better ways to grab your
current ip address BTW, given a dhcp
client.One quick hack:
function caddr() {
awk ' BEGIN {
while (("/sbin/ifconfig" | getline array[a++]) > 0) {
}for (x in array) {
if (array[x] ~ /eth0/) {
z = split(array[x + 1], arr, ":")
gsub(/[^0-9.]+/,"",arr[2]); print arr[2]
}
}
}'
return
}Another hack for the saved address:
function oaddr() {
sv="/tmp/oaddr.sv"
parm=$1
if [ -z "$parm" ] ; then
caddr > $sv
elif [ "$parm" = "old" ] ; then
oaddr=`cat $sv`
export oaddr
fireturn
}
Save the op of the shell function:
Get old address:
oaddr old
Get new address:
myadd=`caddr`
Now you can change all of your cat pipes
to sed to simple sed:
sed 's/'"$oaddr"'/'"$myadd"'/g' file > tmpfile && cp tmpfile fileYou should not have a problem with sed overwriting files with blank lines...
A way to possibly avoid this is to use:
sed '/'"$oaddr"'/s/'"$oaddr"'/'"$myadd"'/'
Where the match is dictated by lines containing the old address only.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |