Computing.Net > Forums > Linux > BASH scripting question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

BASH scripting question

Reply to Message Icon

Name: nato
Date: November 3, 2002 at 10:38:25 Pacific
OS: RH8.0
CPU/Ram: buku
Comment:

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"



Sponsored Link
Ads by Google

Response Number 1
Name: junky_toof
Date: November 3, 2002 at 15:19:10 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: BASH scripting question

simple bash script www.computing.net/answers/linux/simple-bash-script/18032.html

Separating commands in bash scripts www.computing.net/answers/linux/separating-commands-in-bash-scripts/20341.html

Writing a small bash script. www.computing.net/answers/linux/writing-a-small-bash-script/17165.html