As promised in my last mail wanting help, I've been reading up on perl nd having a go and playing with a script sent to me by Raphael (Many thx) ....
Now my problem is, for some reason, in my virtual user table, there are space and tab's, how do I get the split to recognise the tab as well as the space char?
my @fields = split(" ", $_); # Break it down, this is the original and sees the space ok...
my @fields = split(" ", $_) || split("<ascii for tab?", $_); # Break it down, is this anywhere close ?
What I'm aiming for is a script that I can add/modify or delete entries in the virtual usertable.....
I can get the entries added without any problems, but I will probably be posting again at some time to get help on modifying and deleteing them...
#! /usr/bin/perl -w
# make sure we get command line args
if (not(defined($ARGV[0]))) { print "\n\n\nUsage: chk_alias <username>\n\n\n"; exit(1); }
my $username = $ARGV[0]; # Get the username from commandline chomp($username);
open(VUT, "</etc/mail/virtusertable")|| die "Cannot read virtusertable\n"; while (<VUT>) { # Read a line from /etc/mail/virtusertable my @fields = split(" ", $_); # Break it down my $alias = $fields[0]; my $user = $fields[1]; # User name chomp($user); next unless $user eq $username; print "Alias for $username is $alias\n"; } close VUT;
Hi Simon,
On Thu, 7 Feb 2002, Simon wrote:
Now my problem is, for some reason, in my virtual user table, there are space and tab's, how do I get the split to recognise the tab as well as the space char?
If you're planning to only use the perl script from now on, I'd make life easy on yourself and strip out all the tabs (or all the spaces) so that the file is comparitively consistent....
Andrew.
On Thu, Feb 07, 2002 at 09:56:37AM +0000, Simon wrote:
Now my problem is, for some reason, in my virtual user table, there are space and tab's, how do I get the split to recognise the tab as well as the space char?
my @fields = split(" ", $_); # Break it down, this is the original and sees the space ok...
my @fields = split(" ", $_) || split("<ascii for tab?", $_); # Break it down, is this anywhere close ?
try just
my @fields = split;
which is equivalent to:
my @fields = split /\s+/, $_;
which will split $_ on one or more white space characters.
you could also write:
while(<VUT>) { my($alias, $user) = split; next unless $user eq $username; print "Alias for $username is $alias\n"; }
which saves you having to define @fields. The chomp is unnecessary here, as the split will remove the line feed from the end.