Solved

perl script quesion pls

Posted on 2004-11-02
275 Views
Last Modified: 2008-02-01
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
0
Question by:shivani
    2 Comments
     
    LVL 18

    Accepted Solution

    by:
    add another elsif:

        elsif( $line =~ m|^i\trequest| ) {
            # add postinstall line
            print WRITEPERL $line . "i\tpostinstall\n";
        }
    0
     
    LVL 13

    Expert Comment

    by: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.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now