not entirely sure this does all that you're after, but it should give you a few ideas..

I haven't actually ever written anything that takes commandline options before, so thought I'd have a crack!

I'm assuming here that your file simply contains:
@dom1.com fred
@dom2.com sarah
@dom3.com tony
etc..

usage: pfvedit -u bob -a @test2.com

#!/usr/bin/perl -w use strict; # single-character switches processing with switch clustering use Getopt::Std; # declare globals our ($opt_u,$opt_a); # the actual postfix file #my $datafile = '/etc/postfix/virtual' ; my $datafile = "evpostdata.txt"; # a temp file we'll use for writing #my $tempfile = '/tmp/pfvirtual'; my $tempfile = "evposttemp.txt"; # get the supplied args (go into $opt_u,$opt_a) getopt('ua'); # make sure args are present usage() unless($opt_u && $opt_a); # make sure args are ok usage() unless $opt_u =~ /^\w+$/; usage() unless $opt_a =~ /^\@[\w\.\-]{3,}$/; # read the existing postfix file open FH, $datafile or die "Couldn't read $datafile: $!" ; my @virtual = <FH>; close FH ; # add our new line push(@virtual,"$opt_a\t$opt_u"); # slice n dice into an array of arrays for sorting foreach(@virtual){ chomp $_; my ($addr,$user) = split(/\t/,$_); $_ = [ $addr, $user ]; } # sort by usernames @virtual = sort { $a->[1] cmp $b->[1] } @virtual; # sort by domains @virtual = sort { $a->[0] cmp $b->[0] } @virtual; # write data to temp file open FH, ">$tempfile" or die "Couldn't write $datafile: $!" ; foreach(@virtual){ print FH "$_->[0]\t$_->[1]\n"; } close FH; # move new file to real location rename($tempfile,$datafile); print "Added user '$opt_u' with address '$opt_a'.\n"; exit; sub usage{ print "usage: evpostfix -u USER -a \@domain.com\n"; exit; }

In reply to Re: Postfix Virtual Edit Script by edoc
in thread Postfix Virtual Edit Script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.