Porting Shell to Perl must be done mindfully of simpler ways to achieve the same thing. It is ineffective to use `cat | grep` multiple times, when you could simply read the file in and print out the lines that you are interested in one by one.

For example; and this will still not quite do what you want, which is no doubt to retrieve all active aliases entries for a given site. You will also need to consider /etc/mail/virtusertable if present on your system.

#!/usr/bin/perl -w use strict; use constant MAIL_ALIASES => "/etc/mail/aliases"; my $site = shift; # all users of a site have home directories under # /home/sites/XXX/users/. my @users = (map { m{/([^/]*)$} } # `basename' with a RE (grep { -d $_ } # only directories </home/sites/$site/users/*> # glob )); # build a big regular expression with each user name; # use \b to anchor the pattern match, so that a user called # `jane' won't show up entries for `janet' my $pattern = '\b(' . join("|", @users) . ')\b'; open ALIASES, MAIL_ALIASES or die("Failed to open ".MAIL_ALIASES."; $!"); while (<ALIASES>) { print if m/$pattern/o; } close ALIASES;

In reply to Re: Error in converted shell script (was: Pls Help) by mugwumpjism
in thread Error in converted shell script (was: Pls Help) 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.