Link to home
Start Free TrialLog in
Avatar of shivani
shivani

asked on

perl script quesion pls

hi all,

i don't know anything about perl but since peer who wrote perl has left, I have to learn this as well accomplish this.. may be it is simple change, which you experts can help me in a min.

here is the issue..

there is perl script (on SOLARIS 5.8) which is called fixPrototype.pl which actually takes an argument as name of file (which is always "prototype) and fixes some stuff in this file..

so we will call

fixprototype.pl prototype

and it will change prototype file by fixing few things..

prototype file looks as below (only few lines)..

#ident  "%W%"
i       pkginfo
i       copyright
i       request

x none $OPTDIR 0755 $OWNER $GROUP

!search $INSDIR
f none $OPTDIR/docs.jar 0644 $OWNER $GROUP
x none $OPTDIR/appvar 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/CIO 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/FRANKRUSSELL 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/GENERIC 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/JPMORGAN 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/LJR 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/MELLON 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/MERCER 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/FEED/NORTHERNTRUST 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/P2P 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload/PF 0755 $OWNER $GROUP
x none $OPTDIR/data 0755 $OWNER $GROUP
x none $OPTDIR/data/AUS 0755 $OWNER $GROUP
x none $OPTDIR/data/CMTA 0755 $OWNER $GROUP
x none $OPTDIR/data/GMRS 0755 $OWNER $GROUP
x none $OPTDIR/data/LEBL 0755 $OWNER $GROUP
x none $OPTDIR/data/RETAIL 0755 $OWNER $GROUP
x none $OPTDIR/data/logs 0755 $OWNER $GROUP
x none $OPTDIR/data/logs/app 0755 $OWNER $GROUP
x none $OPTDIR/data/logs/appsrv 0755 $OWNER $GROUP
x none $OPTDIR/data/logs/autosys 0755 $OWNER $GROUP
x none $OPTDIR/docs 0755 $OWNER $GROUP

and fixPrototype.pl looks as below (complete code)


#!/opt/perl/5.8.0/bin/perl
#ident      "%W%"
#==================================================
# GLOBAL VARIABLES
#==================================================

$filename      = $ARGV[0];
$myOwner      = $ARGV[1];
$myGroup      = $ARGV[2];
if ($myOwner eq '-' || $myOwner eq "" ) { $myOwner = '$OWNER'; }
if ($myGroup eq '-' || $myGroup eq "" ) { $myGroup = '$GROUP'; }

$wfilename      = $filename.$$;

#print "OWNER = $myOwner\n";

#==================================================
# USUAGE
#==================================================
sub helpme {

      print "\nUSAGE : \n";
      print "$0 <file name> <owner> <group>";
      print "\nex: $0 prototype isoft wls61";
      print "\n if <owner> and/or <group> is a dash (-) then it will default to \$OWNER \and/or $GROUP resp. from request script";
      print "\n\n";
      exit;
}

#==================================================
# PROGRAM
#==================================================

sub changeLine {
      my ($line, $perm, $owner, $group) = @_;
      $line =~ s/\$OWNER/$owner/;
      $line =~ s/\$GROUP/$group/;
      if ( $line =~ m#\s(\d\d\d\d)\s# ) {
            $line =~ s/$1/$perm/;
      }

      return $line;
}

if ( $ARGV[0] =~ /help/i || (length(@ARGV)+1) != 2 ) {
      &helpme;
} elsif (! -f $filename) {
      print "ERROR: $filename NOT found!\n";
      exit;
}

open(READPERL, "<$filename") || die "CANNOT OPEN $filename for reading";
open(WRITEPERL, ">$wfilename") || die "CANNOT OPEN $wfilename for writing";

while (my $line = <READPERL>) {

            if ($line =~ m|\$OPTDIR/data |) {
                  # if we leave this line in prototype,
                  # sym link will be removed and a physical directory will
                  # be created instead - bad.
                  # by removing it the other dirs spcified in protoype under data will still get created
                  # but they will get created on ftp nfs server as we want
                  
                  # touch this line and die!!!! - ask Scot B first

            } elsif ($line =~ m|\$OPTDIR/weblogic/etc|) {
                  # for shell scripts, etc.
                  print WRITEPERL changeLine($line, '0775', '$OWNER', '$GROUP');
            } elsif ($line =~ m|\$OPTDIR/appvar/upload |) {
                  # general location for writing files by weblogic
                  print WRITEPERL changeLine($line, '0775', 'wls61', '$GROUP');
            } elsif ($line =~ m|\$OPTDIR/appvar/upload/|) {
                  # general location for writing files by weblogic
                  print WRITEPERL changeLine($line, '0775', 'wls61', 'isoft');
              } elsif ($line =~ m|\$OPTDIR/logs/autosys|) {
                  # general location for writing autosys logs
                  print WRITEPERL changeLine($line, '0775', 'isoft', '$GROUP');
              } elsif ($line =~ m|\$OPTDIR/logs|) {
                  # general location for writing logs
                  print WRITEPERL changeLine($line, '0775', 'wls61', '$GROUP');
            } elsif ($line =~ m|\$OPTDIR/data/|) {
                  # general location for data files
                  # make it writable by isoft and wls61
                  # isoft - as this is the id autosys runs as
                  # wls61 as this is the id weblogic runs as
                  print WRITEPERL changeLine($line, '0775', 'wls61', 'isoft');
            } elsif ($line =~ m|config.xml|) {
                  print WRITEPERL changeLine($line, '0775', 'isoft', '$GROUP');
            } elsif ($line =~ m|setEnv.sh|) {
                  print WRITEPERL changeLine($line, '0775', 'isoft', '$GROUP');
            } elsif ($line =~ m|50006_AutosysProfile.sh|) {
                  print WRITEPERL changeLine($line, '0775', 'isoft', '$GROUP');
            } elsif ($line =~ m|rits_ldap.properties|) {
                  print WRITEPERL changeLine($line, '0775', 'isoft', '$GROUP');
            } else {
                  printf WRITEPERL $line;
            }
}

rename($wfilename, $filename);
close(READPERL);
close(WRITEPERL);



Bascially Requirement is as below:

I have to enhance pl script so that before fixing anything, it should read the prototype file and when it finds the line
i       request


after that it should insert a new line saying

i<tab>postinstall

so bascially new prototype file (only few lines) should look as below


#ident  "%W%"
i       pkginfo
i       copyright
i       request
i       postinstall

x none $OPTDIR 0755 $OWNER $GROUP

!search $INSDIR
f none $OPTDIR/docs.jar 0644 $OWNER $GROUP
x none $OPTDIR/appvar 0755 $OWNER $GROUP
x none $OPTDIR/appvar/upload 0755 $OWNER $GROUP

and the fixes which perl script is already doing..


hope you can help me with this...

regards
ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gripe
gripe

> I have to enhance pl script so that before fixing anything

Why do you have this requirement? If you see the line 'i     request' do you want to stop processing and ONLY change that line or do you want to continue on with the normal changes on the script. If you want to continue on, this is a simple matter of adding another elsif clause to your if/else statement, like so:

          } elsif ($line =~ m|i\trequest|) {                  # match i followed by a tab followed by request
               print WRITEPERL "$line\ni\tpostinstall\n"; # print the i<tab>request line followed by a i<tab>postinstall line
          }

Hope this helps.