Perl script not executing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script not executing
# 1  
Old 04-29-2009
Question Perl script not executing

Hi All,
I have been given a perl script to modify but which is not running completely.And it is not showing any errors also.

The script is :

Code:
 
#!/usr/local/bin/perl
print "Which transaction? ";
print "\n";
print "1 - Inquiry";
print "\n";
print "2 - Delete Bank";
print "\n";
print "3 - Edit Bank";
print "\n";
print "4 - Enroll and Add Bank";
print "\n";
print "5 - Modify Default Bank";
print "\n";
print "6 - Submit Payment";
print "\n";
$goodChoice = "false";
while ($goodChoice eq "false") {
$choice = <STDIN>;
chomp $choice;
if ($choice eq "1") {
  $agent = "com.americanexpress.as.payment.app.agent.InquiryAgent";
  $goodChoice = "true";
}
elsif ($choice eq "2") {
  $agent = "com.americanexpress.as.payment.app.agent.DeleteBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "3") {
  $agent = "com.americanexpress.as.payment.app.agent.EditBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "4") {
  $agent = "com.americanexpress.as.payment.app.agent.EnrollAddBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "5") {
  $agent = "com.americanexpress.as.payment.app.agent.ModifyDefaultBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "6") {
  $agent = "com.americanexpress.as.payment.app.agent.SubmitPaymentAgent";
  $goodChoice = "true";
}
else {
  print "Please type a number, 1 - 6";
  print "\n";
}
}
$filename = "report.log";
open(FILEHANDLE, $filename);
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);



Can anyone please explain by what all is happening in this script.I am a beginner and couldn't understand all these commands.
Also i need to collect the output of this script in some output file.How to do that.


Thanks in advance.
U
# 2  
Old 04-29-2009
The first half of the program is a menu driven type where user has benn given various options menu to to choose one of them - from 1 to 6 depending on his need.
The second half of the program seems to be opening a report log file and searching for a line containing the word 'agent' and then printing the line and repeating the same for all the lines thr out the file...
# 3  
Old 04-29-2009
It basically prints the the 2nd line after each line that it finds that matches the menu item selected. So whatever you select it looks it compares a line to that selection If that selection is in the input line it goes to the next line in the file. It then sets the variable to print the line and goes to the next line. Then it prints this next line and resets the variable and starts over again. So each time it finds a match it skips the next line and prints the line after that. It does this until you get to the end of file.

To output the lines to a file you would have to open an output file and intead of just print $line you would use print outfile $line.
So you would add a line after the open (FILEHANDE, $filename); as

open (OUTFILE, ">/temp/outputfile");

Then add that to the print line such as

print OUTFILE $line;

Then close the OUTFILE after you close the input file such as

close(FILEHANDLE);
close(OUTFILE);
# 4  
Old 04-29-2009
Hi All,
Thanks for your quick replies.
I just have one doubt ,if in place of report.log in the above code if i have a file which is zipped (i.e with .Z extension ) then this perl script will fail.
Is there any way i can unzip this file inside the script itself (like using zcat or something) so that i can run this script on a zipped file??

Thanks
U
# 5  
Old 04-29-2009
Perl doesn't really handle the Z compression but you can use the operating system commands on the files. So you could use zcat to output the data to the input file report.log. Or you could use a list to replace the input file with the list of data from the zcat command. So you could use somehting like

@inputdata=`zcat inputfile.Z`;

or use the system command like

system("zcat inputfile.Z > report.log");

Anything like that would work out for you.
# 6  
Old 04-29-2009
Hi Bubbajoe,
Thanks again for your reply and apologies for my ignorance.
How exactly to use the specified line in above code.The problem is i cant try executing the code until it is completely perfect.

Is it like this:

Code:
@inputdata=`zcat inputfile.Z`;
open(FILEHANDLE, @inputdata);
open (OUTFILE, ">/temp/outputfile");
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print OUTFILE $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);
close(OUTFILE);



or is it like this:

Code:
system("zcat inputfile.Z > report.log");
$filename = "report.log";
open(FILEHANDLE, @inputdata);
open (OUTFILE, ">/temp/outputfile");
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print OUTFILE $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);
close(OUTFILE);

Thanks in advance
# 7  
Old 04-29-2009
First do this, only this:

Code:
@inputdata=`zcat inputfile.Z`;
print "$_\n" for @inputdata;

report back what gets printed. Solve the problems one step at a time instead of trying to do too many things at once.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in executing select query from perl script

Hi, I have a perl snippet that call a select query with a bind variable,when i compile the script I'm unable to get the query output. The same query when i fire in sqlplus fetches few rows. The query takes bit time to fetch results in sqlplus. my $getaccts = $lda->prepare("select distinct ... (1 Reply)
Discussion started by: rkrish
1 Replies

2. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

3. Shell Programming and Scripting

Problem executing perl script on remote server

Hello, I am running in to a problem running a perl script on a remote server. I can run a simple script test.pl which contains just a print statment without issue by running ssh root@1.2.3.4 perl test.pl However, I have a more complex script that does not execute as expected. I think I... (3 Replies)
Discussion started by: colinireland
3 Replies

4. Shell Programming and Scripting

Error in executing Perl script

Hello All I am facing an issue The unix script is running fine in unix environment which uses ssh connection but when I try to run the same in informatica environment (same server where I was running the unix script manually successfully) its showing the below error command-line line 0:... (11 Replies)
Discussion started by: Pratik4891
11 Replies

5. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

6. Shell Programming and Scripting

Executing AWK in a perl script using 'system'...

I have a simple perl script that looks similar to this: #!/usr/bin/perl/ # Have a lot of PERL code in the front of this script. #Would now like to execute a system command using AWK system (qq(cd /location && awk '/full/ {print $1;exit}' /myfile)); The system command in my perl script... (4 Replies)
Discussion started by: SysAdm2
4 Replies

7. Shell Programming and Scripting

perl script to send an email executing with errors

Hello , i am using RHEL5 , i have got a perl script that executes and sends an email correctly if i run it with ./sendemail.sh command. This is the script below #! /usr/bin/perl use Net::SMTP; print "starting email send ."; $smtp = Net::SMTP->new("192.168.0.1");... (2 Replies)
Discussion started by: kabazzi
2 Replies

8. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

9. Shell Programming and Scripting

Executing .profile from perl script

Hi, How can i execute .profile from a perl script I need this - i am trying to run perl script from crontab and it looses the environment variables Please provide help Your help is greatly appreciated Thanks (1 Reply)
Discussion started by: prekida
1 Replies

10. Shell Programming and Scripting

Problem executing setuid script in perl-5.8.6

Hi, I have a script (a.pl) that can be run by anyone. The script internally has to read a file and write into few files which are owned by user 'myUser'. Has to read the following file: -rwx------ 1 myuser myuser 4986 Aug 20 18:11 my.file Has to write into following files: ... (0 Replies)
Discussion started by: sarmakdvsr
0 Replies
Login or Register to Ask a Question