BASH Scripting Question

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/named

for 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
fi

return
}
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 file

You 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.

------------------------------

Cutting a file with a bash script

I want to wget an html file then cut the file in a way that I can get some of the data from inside the file. I need a command that I can give it a start character and an end character and it will grab everything in between. For example it might start at the 50th character in file bob.html and stop at character 371. Or maybe stop at the end of the file. I am very new at this stuff. I tried the awk and cut commands, but they seem to work on colums.

---

You could change the line endings and operate on the file as a string.
cat file.html | tr -d '\012' | cut -c 50-371 > file.out

Your line endings may be different.

man tr
man cut

Have a Linux Problem
Do you have a Linux Question?

Frequently Used Options
See also common Linux Commands

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Hints and Tips on: Linux System Administration

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.