wube has asked for the wisdom of the Perl Monks concerning the following question:

Hello enlightened brothers and sisters. I am a newbie to UNIX and to Perl. I have written a script to fix a problem we had a couple of days ago and I was wondering if the same could be done in Perl. The Bourne script follows:
#!/bin/sh # # This script fixes the ownership of all files that is in /var/sadm/in +stall # It bypasses 3 directories /usr # /usr/lib # /usr/openwin # WKDIR=/var/sadm/install cd $WKDIR # egrep -v "/usr d |/usr/lib d |/usr/openwin d " contents | grep ' d ' | + awk '{ print "chown -R " $5":"$6,$1 }' > /tmp/fixchown.sh
Any assistance towards reaching Nirvana will be greatly appreciated.
wube

Edit kudra, 2001-11-14 Added code tags

Replies are listed 'Best First'.
Re: How to do this in Perl ??
by VSarkiss (Monsignor) on Nov 13, 2001 at 21:19 UTC

    "Can it be done in Perl"? Do we need to discuss Turing-completeness? I think what you mean to ask is "How easily can this be done in Perl?" to which the answer is, "Very easily."

    #! /usr/bin/perl -w use strict; my $wkdir = '/var/sadm/install'; chdir $wkdir or die "Couldn't cd to $wkdir: $!\n"; open I, 'contents' or die "Couldn't open 'contents': $!\n"; my @line; while (<I>) { next if m{/usr d|/usr/lib d|/usr/openwin d}; if (/ d /) { @line = split; system("chown -R $line[4]:$line[5] $line[0]"); } } close I; # be polite
    (Note, I haven't tested this.)

    As always, TIMTOWTDI. (This may even touch off a round of golf. ;-) Notice I didn't write the chown commands to a script, I just executed them directly. There are other optimizations possible, but I just wanted to make the correspondence to your script very clear.

    HTH

    Update
    Changed cd to chdir. Thanks, blakem!

      You can take a few strokes off by inserting use File::Find; after line 2 and replacing system(...) with
      find(sub { chown($line[4], $line[5], $File::Find::name); }, $line[0]);
      pop; pop(@FIZZ); @FIZZ;
      One minor point:

      cd is spelled chdir in perl...

      -Blake

      I will play with this.
      Thanks to all that replied
      wube
Re: How to do this in Perl ??
by brianarn (Chaplain) on Nov 13, 2001 at 21:08 UTC
    I'm sure it can be, except your code isn't exactly easy to break down right now because it's all running together - try putting <CODE></CODE> tags around your script - then someone'll be able to read it and break it down much easier :)

    ~Brian