perl help, please

Top Page
Attachments:
Message as email
+ (text/plain)
+ env-changes.txt (text/plain)
+ new-vars.txt (text/plain)
+ wavegen_SB.txt (text/plain)
+ (text/plain)
Delete this message
Reply to this message
Author: David
Date:  
To: PLUG
Subject: perl help, please
I'm trying to write a perl script that will

- read a txt file with name=value pairs
- loop through a shell script (see attached .txt file)
- grab the contents of a particular function
- replace the name=value pairs from the function with the value from the
txt file
- grab the corresponding case statement from the shell script
- search and replace the values in the case statement
- write it all out to a new file in the correct order.

I need to be able to run this script on about 200 ksh scripts, mostly
with the same name=value pairs. However, there are many scripts with
different/extra name=value pairs.

ex:

It needs to take the abqenv () function, copy the whole thing to newenv
(), replace all the value of all the variables with the new value that
it gets from the txt file. Then put that new function *after* the
abqapps function.

next, it needs to grab each abqapps) sections (under the case statement)
and do the same thing.

Unfortunately, it's cutting off the "abqenv () {" and "}" lines from
the abqenv part. argh!


I have attached the perl script I have so far. Like I said, it mostly
works.


Please don't laugh too much at the ugliness of my perl. I'm no perl
monk by any stretch.

Any tips/help would be appreciated.

Thanks
David


--
"I find your lack of faith disturbing."
--Darth Vader
#!/usr/bin/perl

my @abqcrap;

print "What file will we use for the new values?\n";
chomp($nvars = <STDIN>);

print "Is this the prod user or the dev user?\n";
chomp($user = <STDIN>);

if    ( $user eq "prod" ) {
    $oldenv="abqenv";
    $newenv="sysenv";
    $olduser="abqapps";
    $newuser="sysapps";
    $oldsid="apabq";
    $newsid="apsys";
} else {
    $oldenv="devaenv";
    $newenv="devsenv";
    $olduser="devaapps";
    $newuser="devsapps";
    $oldsid="apabqd";
    $newsid="apsysd";
}


# print "olduser = $olduser\n";

sub domojo2 {
    $abqsids="@abqsids";
    @search = ($olduser, $oldsid, $oldenv);
    @replace = ($newuser, $newsid, $newenv); 
    foreach (0..$#search) { 
        $abqsids =~ s/$search[$_]/$replace[$_]/g;
    }
    push (@newfile, $abqsids);

    
    #  Since this line is in the file twice, we need to clear out these variables or we end
    #  up with the line repeated 
    undef $abqsids;
    undef @abqsids;
}


sub domojo {
    $abqenv = "@abqenv"; # convert it to a string, so we can split it
    @abqenv2 = split(/;/, $abqenv); # take the ";" out of each line 


    for ($i=0;$i<=$#abqenv2;$i=$i+2) {     # We need to increment by 2, not by 1;
        push (@varpairs, $abqenv2[$i]); #@argh="EXEPATH=${PATHPROD}/${BINCOAD}      LOGPATH=${PRODLOG}/CO/adage    AP_USERNAME=M2/M2_DBA@abqp     WHSE=003"
        push (@exports, $abqenv2[$i+1]); # All the "export XXX" lines. need for putting it back together later
    }

    
    while (<@varpairs>) {
        @varpairs = split(/=/); # Take out teh "="
        push (@varsonly, $varpairs[0]); # Only the variable names
        push (@valuesonly, $varpairs[1]); # Only the values of the variables
        }


    for ($i=0;$i<=$#varsonly;$i++) {


        # Give them the value that is in the environment vars file
        print "Whats the new value for $varsonly[$i] : ($User_Preferences{$varsonly[$i]})\n";
        chomp ($answ = <STDIN>);
        if ($answ eq "") {    # If they just hit 'return', the new value will be what the value from the environemtn vars file is.
            $answ = $User_Preferences{$varsonly[$i]};    
        }
        print "answ = $answ\n";
        push (@newvalueonly, $answ);
        }


    push (@newloc, "$newenv () {\n");

    
    for ($i=0;$i<=$#varsonly;$i++) {
        $string = "      @varsonly[$i]=@newvalueonly[$i]; @exports[$i];\n";
        push (@newloc, $string);
        undef $string;
    }
    push (@newloc, "}\n\n");
}


$todo=@ARGV[0];
open ( FILE, "notdone/$todo" ) or die "cannot open $file: $!";
$foundAbq=0;

# Read in the new environment vars from a file

open ( CONFIG, "$nvars" ) or die "cannot open $file: $!";
while (<CONFIG>) {
    chomp;                    # no newline
    s/#.*//;                # no comments
    s/^\s+//;                # no leading white
    s/\s+$//;                # no trailing white
    next unless length;        # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    #my ($var, $value) = split(/=/, $_, 2);
    $User_Preferences{$var} = $value;
}



while (<FILE>) {
    #push (@newfile, $_);
    $prevabqcount = 0;
    $lastWasAbq=$foundAbq;
    if ( /^$oldenv/ .. /^}/ ) {     # we want the whole abqenv bit
        next if /^(\s)*$/; #  skip empty lines
        next if /^$oldenv \(\)*(.*){$/; # skip 'abqenv () {' lines
        next if /^$oldenv\(\)*(.*){$/; # skip abqenv() {' lines
        next if /(.*)#/; # skip commented lines
        next if /^}/; # skip where '}' is the first char on a line
        push (@abqenv,$_);       # put it all in teh @abqcrap array 
        #$abqcount = @abqcrap;
        $foundAbq=1;
    } else {
        $foundAbq=0;
    }
    if(($lastWasAbq == 1) && ($foundAbq == 0)) {
        domojo;
        $lastWasAbq = 0;
        push (@newfile, @newloc);
    }
    $prevabqcount2 = 0;
    $lastWasAbq2=$foundAbq2;
    if ( /^\s*$olduser/ .. /^\s*$oldenv\;\;/ ) {     # we want the whole abqenv bit
        next if /^(\s)*$/;      # skip empty lines
        next if /(.*)#/;        # skip commented lines
        push (@abqsids,$_);    # put it all in teh @abqsids array 
        #$abqcount = @abqcrap;
        #print "abqcrap = @abqcrap\n";
        $foundAbq2=1;
    } else {
        $foundAbq2=0;
    }
    if(($lastWasAbq2 == 1) && ($foundAbq2 == 0)) {
        domojo2;  # this does the s/r for abqapps) portion
        #print "abqcrap2=@abqcrap\n";
        $lastWasAbq2 = 0;
    }
    push (@newfile, $_);
    }


    #     print @newfile;

    
#}
open ( NEWFILE, ">notdone/$todo-new" ) or die "cannot open $file: $!";
print NEWFILE @newfile;
AD2ALL_QUEUE_NAME=test1
ATS_BID_QUEUENAME=ats-bid-poopname
EXEPATH=${PATHPROD}/${BINSBAD}
LOC=poop
LOGPATH=logpoop
WHSE=whsepoop
#!/bin/ksh
#Testing cvs

# ------------------------------------------------------------------------------

phxenv () {
EXEPATH=${PATHPROD}/${BINAZAD}; export EXEPATH;
LOGPATH=${PRODLOG}/AZ/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@prod1; export AP_USERNAME;
WHSE=002; export WHSE;
LOC=AZ; export LOC;
}

denenv () {
EXEPATH=${PATHPROD}/${BINCOAD}; export EXEPATH;
LOGPATH=${PRODLOG}/CO/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@denp; export AP_USERNAME;
WHSE=003; export WHSE;
LOC=CO; export LOC;
}

abqenv () {
EXEPATH=${PATHPROD}/${BINNMAD}; export EXEPATH;
LOGPATH=${PRODLOG}/NM/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@apabq; export AP_USERNAME;
WHSE=009; export WHSE;
LOC=NM; export LOC;
}

devpenv () {
EXEPATH=${PATHDEVL}/${BINAZAD}; export EXEPATH;
LOGPATH=${DEVLLOG}/AZ/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@devd; export AP_USERNAME;
WHSE=002; export WHSE;
LOC=AZ-DEV; export LOC;
}

devdenv () {
EXEPATH=${PATHDEVL}/${BINCOAD}; export EXEPATH;
LOGPATH=${DEVLLOG}/CO/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@dend; export AP_USERNAME;
WHSE=003; export WHSE;
LOC=CO-DEV; export LOC;
}

devaenv () {
EXEPATH=${PATHDEVL}/${BINNMAD}; export EXEPATH;
LOGPATH=${DEVLLOG}/NM/adage; export LOGPATH;
AP_USERNAME=XX/XX_DBA@apabqd; export AP_USERNAME;
WHSE=009; export WHSE;
LOC=NM-DEV; export LOC;
}


ps -ef | grep "ora_pmon_${ORACLE_SID}" | grep -v grep > /dev/null
if [ $? -ne 0 ]; then
   case "`echo ${SHAMUSER}`" in
      phxapps) TWO_TASK=prod1; export TWO_TASK;
               phxenv;;
      denapps) TWO_TASK=denp; export TWO_TASK;
               denenv;;
      abqapps) TWO_TASK=apabq; export TWO_TASK;
               abqenv;;
     devpapps) TWO_TASK=devd; export TWO_TASK;
               devpenv;;
     devdapps) TWO_TASK=dend; export TWO_TASK;
               devdenv;;
     devaapps) TWO_TASK=apabqd; export TWO_TASK;
               devaenv;;
            *) logger "You are trying to run dmq2wv under an invalid username.";
               exit 0;;
   esac
else
   case "`echo ${SHAMUSER}`" in
      phxapps) ORACLE_SID=prod1; export ORACLE_SID;
               phxenv;;
      denapps) ORACLE_SID=denp; export ORACLE_SID;
               denenv;;
      abqapps) ORACLE_SID=apabq; export ORACLE_SID;
               abqenv;;
     devpapps) ORACLE_SID=devd; export ORACLE_SID;
               devpenv;;
     devdapps) ORACLE_SID=dend; export ORACLE_SID;
               devdenv;;
     devaapps) ORACLE_SID=apabqd; export ORACLE_SID;
               devaenv;;
            *) logger "You are trying to run dmq2wv under an invalid username.";
               exit 0;;
   esac
fi



# +----------------------------------------------+
# | Setup project specific environment variables |
# +----------------------------------------------+

# --------------------- End of script ---------------------

---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss