LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
  Search this Thread
Old 07-10-2008, 11:42 AM   #1
OTIM
Member
 
Registered: Nov 2007
Posts: 37

Rep: Reputation: 15
shell script (bind related)


Hello,

I made a shell script which assignes a reverse to an ip , using sed
The problem is that i don't know how to increment the serial after i make the changes.
i mean each time i run the script to also increment the serial
I have the reverse file like this :
$ORIGIN .
$TTL 86400 ; 1 day
<something>.IN-ADDR.ARPA IN SOA ns.domain.com. root.domain.com (
2006101900 ; serial
10800 ; refresh (3 hours)
15 ; retry (15 seconds)
172800 ; expire (2 days)
86400 ; minimum (1 day)
)


I need the script to increment the serial 2006101900 lets say to 2006101901

Thank you
 
Old 07-10-2008, 12:29 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You can use "n=2006101900; n=$[$n+1]; echo $n" or "n=2006101900; let n=$n+1; echo $n" or "n=2006101900; ((n++)); echo $n" or "n=2006101900; n=$(echo "$n + 1"|bc -l); echo $n".
 
Old 07-10-2008, 12:41 PM   #3
OTIM
Member
 
Registered: Nov 2007
Posts: 37

Original Poster
Rep: Reputation: 15
hello,

Thank you for your reply. The problem is not that simple, because i don't know the value of the serial number; this value can change over time when people modify the file.
I need the script to somehow extract that value increment it and replace the old value with the new one
 
Old 07-10-2008, 01:33 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,794

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
Code:
 
n=$(sed -n 's/[ \t]*\(.*\) ; serial/\1/p' reverse-file)
let n=$n+1 # or one of the others...
sed -i "s/.* ; serial/      $n ; serial/" reverse-file
 
Old 07-10-2008, 01:33 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Not knowing the value is not a problem. As long as one record template version is used you can grep for any markers in use. For instance considering the record
Code:
example.com.  IN      SOA   ns.example.com. root.example.com. (
                              2006101900 ; se = serial number
                              3h         ; ref = refresh
                              15m        ; ret = update retry
                              3w         ; ex = expiry
                              3h         ; min = minimum
                              )
                  IN      NS      ns1.example.com.
                  IN      NS      ns2.example.com.
                  IN      MX  10  mail.example.com.
; public hosts
ns1             IN      A       192.168.254.1
ns2             IN      A       192.168.254.2
mail            IN      A       192.168.254.3
www           IN      A       192.168.254.4
ww1           IN      A       192.168.254.5
; private hosts 
abaddon     IN      A       192.168.254.6
abdiel         IN      A       192.168.254.7
abraxas      IN      A       192.168.254.8
accounting  IN      A       192.168.254.9
something like " awk '/(serial|se|200[0-9].* / {print}' " should get you the value to work with, else use a better regex or post a properly anonimised example record.
 
Old 07-10-2008, 03:08 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I assume you want to bump the serial number, using the common date/revision formatting practice. So a bump should change the date to today's date, not just increment by one.

The following sets the serial number date to today's date + revision number. Note: it will fail after 99, as the general serial number update scheme with a date fails there anyway.

Code:
$ cat bump_serial.sh
#!/bin/bash

perl -i.bak -pe 'BEGIN {chomp ($now=qx/date +%Y%m%d/)};
    /(\d{8})(\d{2})/ and do {
        $serial = ($1 eq $now ? $2+1 : 0);
        s/\d{8}(\d{2})/sprintf "%8d%02d",$now,$serial/e;
}' zonefile

$ bump_serial.sh

$ diff zonefile zonefile.bak
5c5
< 2008071000 ; serial
---
> 2006101902 ; serial
13c13
<                               2008071000 ; se = serial number
---
>                               2006101900 ; se = serial number
35c35
<                               2008071035 ; se = serial number
---
>                               2008071034 ; se = serial number
Use the perl statement inside your shell script. Replace "zonefile" with the variable or zone file you use in your script. It automatically creates a backup file too.
 
Old 07-10-2008, 04:26 PM   #7
OTIM
Member
 
Registered: Nov 2007
Posts: 37

Original Poster
Rep: Reputation: 15
Thank you very much friends.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell related query !!!!!!!!!!! iacharyya Linux - General 10 06-09-2008 06:39 PM
K-Shell startup problem - $ENV related woollybugger Linux - General 1 11-10-2007 04:00 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
BIND 9 question - PID Related ChardRF Red Hat 1 03-13-2005 02:15 PM
Some queries related to DNS(bind) coolamit78 Linux - Networking 1 12-19-2003 03:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 PM.

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