Centos – Delete a zone from named.conf with shell script

bindcentosshell-scripting

I am trying to delete an existing zone record from bind's named.conf file (on a centos 7 release).

The zone record looks like that:

zone "example.com" IN {
        type master;
        file "example.com.zone";
};

The command:

sed -nie '/\"example.com\"/,/^\};$/d;p;' /etc/named.conf

deletes that record (as supposed to).

Next, I have created the following shell script (rm-zone.sh) with executable permissions to do the same job:

#!/bin/sh

[ $# -lt 1 ] && {
        echo "usage: $0 <domain>"
        exit 1
}

domain=$1

sed -nie '/\"$domain\"/,/^\};$/d;p;' /etc/named.conf

rndc reload

echo Zone: $domain deleted successfully

When I log in as root and run the command:

./rm-zone.sh example.com

I see the message "Zone example.com deleted successfully" but the zone is still in my named.conf file…

Again, when I give the above mentioned command from cli:

sed -nie '/\"example.com\"/,/^\};$/d;p;' /etc/named.conf

the zone is properly deleted!!!

Apparently there is something wrong in the shell script, but haven't managed to find out what it is…

Any ideas as to what I'm missing here will be greatly appreciated!

Best Answer

Try

sed -nie "/\"$domain\"/,/^\};"'$/d;p;' /etc/named.conf

instead of

sed -nie '/\"$domain\"/,/^\};$/d;p;' /etc/named.conf

The single quotes disable the shell's interpretation of either \| or $domain.

Related Topic