issue with if loop in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issue with if loop in perl
# 1  
Old 01-08-2008
issue with if loop in perl

Hi

I have a log file, I am having problem with "if else" loop in my perl script which does, find a string in that file ,If that string is found append to success.txt else append it to failed.txt.

problem is: else part of loop it is not working

I am adding problem part of the script.

=====================================

sub log2960 {

$s1 = $c1;
@c2960 = ("cp c2960-lanbase-mz \/ftpboot\/$u\/c2960-lanbase-mz",
"cp c2960-lanlite-mz \/tftpboot\/$u\/c2960-lanlite-mz",
"cp c2960-th0-mz \/tftpboot\/$u\/c2960-th0-mz",
"cp c2960-th-mz \/tftpboot\/$u\/c2960-th-mz",
);

if ( $s1 == "b" ) {

# log c2960
$log = "\/users\/$u\/c2960-log.txt";
open(FILE, $log) || die "cud not read $log";
while (<FILE>) {

if (/$c2960[0]/) {
print "$c2960[0]\n";
open(OUTFILE,">>$of1") || die "cud not write";
print OUTFILE "c2960-lanbase-mz\n";
close(OUTFILE);
last;
[problem] }else{
open(OUTFILE,">>$of2") || die "cud not write"; print OUTFILE "c2960-lanbase-mz\n";
close(OUTFILE);
last;
}

} #End of while
close(FILE);
#crypto log c2960

$log = "\/users\/$u\/c2960-crypto-log.txt";
open(FILE, $log) || die "cud not read $log";
while (<FILE>) {

if (/$c2960[5]/) {
print "$c2960[5]\n";
open(OUTFILE,">>$of1") || die "cud not write";
print OUTFILE "c2960-lanlitek9-mz\n";
close(OUTFILE);
last;
[problem] } else {


print "NO $c2960[5]\n";
open(OUTFILE,">>$of2") || die "cud not write"; print OUTFILE "c2960-lanlitek9-mz\n"; close(OUTFILE);
last;
}

}
close(FILE);
return;
}
}
==========================================

Could anybody please suggest other way, if it does not work.

Thanks

Amit Smilie
# 2  
Old 01-08-2008
Can you transform your posted code to a complete program that demonstrates just the issue while being short, so that others can copy onto their systems to run and trace. Without seeing the content of the variables, it is impossible for anyone to help you debug.

And you have not even mentioned in what way the else part is not working. It's not running as expected? It is being executed in a wrong way? So, be specific with what you are expecting.
# 3  
Old 01-08-2008
Hi All,

Find below the full script.

problem is with "if ($t == 1)" within subroutine "sub c2960"


======== script ==================

#!/usr/bin/perl

$u=`whoami`;
chomp($u);

#print "$u\n";
$op1=$ARGV[0];
$op2=$ARGV[1];


lc($op1);
lc($op2);


$of1="\/users\/$u\/success.txt";
$of2="\/users\/$u\/failed.txt";
chomp($of1);
chomp($of2);

system("rm -f \/users\/$u\/success.txt \/users\/$u\/failed.txt \/users\/$u\/bldstatus.txt");


$view = `cleartool pwv`;
chomp($view);
@v = split(/\:+/,$view);
chomp($v[2]);
$vw = $v[2];

open(NOTFILE,">$of1") || die "cud not write $of1";
print NOTFILE "Status of Build on $vw, option selected:$op1\n";
print NOTFILE "---------------------------------------------\n\n";
print NOTFILE "Images Successfully build\n";
print NOTFILE "-------------------------\n\n";
close(NOTFILE);

open(NOTFILE,">$of2") || die "cud not write $of2";
print NOTFILE "Images Failed to build\n";
print NOTFILE "-------------------------\n";
close(NOTFILE);


chomp($options);


if ( ( $op1 eq "all" ) && ( $op2 eq "" ) ) {


print "Building c2960 images........\n";
`make -j4 -C .\/obj-ppc-c2960\/ c2960-lanbase-mz > \/users\/$u\/c2960-log.txt`;
`make -j4 -C .\/obj-ppc-c2960\/ c2960-lanlitek9-mz > \/users\/$u\/c2960-crypto-log.txt`;

print "Building c2960 images........done.\n";
$c1="b";
&log2960($c1);
&maillog($vw,$op1,$u);
exit 1;
}

## Mail function ##

sub maillog {

$a = $vw;
$b = $op1;
$c = $u;

`cat \/users\/$c\/success.txt \/users\/$c\/failed.txt > \/users\/$c\/bldstatus.txt`;
`mailx -s "Build Completed.with option $b on view: $a " $c\@cisco.com < \/users\/$c\/bldstatus.txt`;
exit 1;

} ## end of maillog


sub log2960 {

$s1 = $c1;


if ( $s1 == "b" ) {

# log c2960
$log = "\/users\/$u\/c2960-log.txt";
open(FILE, $log) || die "cud not read $log";

while (<FILE>) {
$t = 0;
if (/cp c2960-lanbase-mz \/tftpboot\/$u\/c2960-lanbase-mz/) {
$t = 1;
print "$t\n";
}

if ($t == 1) {

print "$c2960[0]\n";
open(OUTFILE,">>$of1") || die "cud not write";
print OUTFILE "c2960-lanbase-mz\n";
close(OUTFILE);
last;

} elsif ( $t == 0 ) {

print "t = $t NO $c2960[0]\n";
open(OUTFILE,">>$of2") || die "cud not write";
print OUTFILE "c2960-lanbase-mz\n";
close(OUTFILE);
last;

} else { } #end of $t == 1 or $t == 0

} #End of while
close(FILE);
$log = "\/users\/$u\/c2960-crypto-log.txt";
open(FILE, $log) || die "cud not read $log";
while (<FILE>) {

$t = 0;
if (/cp c2960-lanlitek9-mz \/tftpboot\/$u\/c2960-lanlitek9-mz/) {
$t = 1;
}

if ( $t == 1 ) {

print "$c2960[5]\n";
open(OUTFILE,">>$of1") || die "cud not write";
print OUTFILE "c2960-lanlitek9-mz\n";
close(OUTFILE);
last;

} elsif ( $t == 0 ) {

print "NO $c2960[5]\n";
open(OUTFILE,">>$of2") || die "cud not write";
print OUTFILE "c2960-lanlitek9-mz\n";
close(OUTFILE);
last;

} else { }

} ## end of while crypto log
close(FILE);
return;
} #end of ( $s1 == "b" )
}
===============End of script==========

Please help

Thanks

Amit
# 4  
Old 01-08-2008
Quote:
Originally Posted by amitrajvarma
while (<FILE>) {

$t = 0;
if (/cp c2960-lanlitek9-mz \/tftpboot\/$u\/c2960-lanlitek9-mz/) {
$t = 1;
}

if ( $t == 1 ) {

print "$c2960[5]\n";
open(OUTFILE,">>$of1") || die "cud not write";
print OUTFILE "c2960-lanlitek9-mz\n";
close(OUTFILE);
last;

} elsif ( $t == 0 ) {

print "NO $c2960[5]\n";
open(OUTFILE,">>$of2") || die "cud not write";
print OUTFILE "c2960-lanlitek9-mz\n";
close(OUTFILE);
last;

} else { }

} ## end of while crypto log
close(FILE);
You still have not mentioned what is your expected behaviour. Please be reminded that your script is very customized and will not run on others' machines, you should provide enough information for others to help you.

Unless you provide us with your expectations, I will just offer some general advise:

As the if block depends on $t and in turn from the pattern matching above, you should first make sure the matching result is correct.

Next, if the file cannot be written into and shows 'cud not write', consider die'ing with

die $!

instead to have the exact error message printed for debug. I also advise you to print $of1 and $of2 and ensure the values are as expected, as I cannot understand why you chomp() a static string. It is harmless, but that does not make sense:

Code:
$of1="\/users\/$u\/success.txt";
$of2="\/users\/$u\/failed.txt";
chomp($of1);
chomp($of2);

# 5  
Old 01-09-2008
Hi All,

Sorry for you could not execute the script.

But Following is what my script does.

1. Does a build (log is stored in c2960.txt)
2. Check if the build is successfull by searching the logfile for a particular string
"cp c2960-lanlitek9-mz \/tftpboot\/$u\/c2960-lanlitek9-mz"
3. If found , write "c2960-lanlitek9-mz" to success.txt
4. If notfound, write "c2960-lanlitek9-mz" to failed.txt
5. Finally combine both success.txt and failed.txt to one single file & send mail to user.

Problem I am facing with my script is, inspite of build being successfull. it is writting string "c2960-lanlitek9-mz" to failed.txt.

Could you help in debugging? If not possible,

can anybody give me a script

which will search the logfile for a string, if string is found, write the string to Success.txt file. If not found write to Failed.txt

Thks

Amit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

2. Shell Programming and Scripting

Issue with while loop?

Hi, I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a... (11 Replies)
Discussion started by: svajhala
11 Replies

3. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

4. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

5. Shell Programming and Scripting

for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here Input file: space separated 2 columns I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked... (7 Replies)
Discussion started by: foal_newbie
7 Replies

6. Shell Programming and Scripting

until loop issue.

Hi, my script is waiting for 3 files to come to a folder for 30 min but even when all the files arrive in the folder it's still waiting for these three files. Files can come with in 2 min and I want it to start processing them immediately after all the files arrive in the folder. until ; do... (3 Replies)
Discussion started by: gurpartap
3 Replies

7. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. Shell Programming and Scripting

loop issue

I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below: host www184.domain.com { hardware ethernet 00:13:72:3B:B4:3A; fixed-address 192.168.0.184; }... (4 Replies)
Discussion started by: kkkk
4 Replies

9. Shell Programming and Scripting

while loop issue

Hi, Following is my code and the file FILE_LIST_EXCESS.txt has 40 file names in it while read LineIn do echo ${LineIn} `ftp -vin << END_INPUT >> ${PID}_DS_GET_Log.log 2>&1 open servername user userid password cd FileDir get ${LineIn} END_INPUT`... (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Help With A For Loop Issue

I was wondering how I can modify this for loop, so it only loops through the filenames that do not have an ".old" extension. for filename in $(ls "$1") do echo $filename | grep '\.old$' > /dev/null if then mv $1/$filename $1/$filename.old fi done (5 Replies)
Discussion started by: ralts01
5 Replies
Login or Register to Ask a Question