lundi 31 mai 2021

perl IF executing both conditions

I am new to perl scripting (i am data analyst trying to work on some scripts)

I am trying to modify a perl script. The logic of what I am trying to achieve is I have created a text file with some status in it. I want to read the file and decide which subroutine will run based on it. I have manually created file with status. The if else command is not working. Next I want to change the status in same text file depending on what subroutine was executed.

unfortunately as on now nothing works :P

here is the script (not the original as for security reasons)

-------XXX--------------

#!/usr/bin/perl

my $filename1="/usr/local/tmp/status.txt";



open Input, $filename1;
undef $/;
$StepInfo= <Input>;
close Input;
$/ = "\n";
print "value $StepInfo";

#Step 1 -
if ($StepInfo =  "Fresh" ){
        step1();
#       exit();
}

if ($StepInfo =  "stp_fail"){
        step2();
#       exit();
}


sub stepcheck {
        my ($argument1) = @_;
        my $filename1= shift;
        open(FH, '>', $filename1);
        print FH "$argument1\n";
        close FH;
}

sub step1 {
        my $StepInfo= shift;
        print "Step1 completed\n";
        print $StepInfo;
        stepcheck ('dh_fail');
}

sub step2 {
        my $StepInfo= shift;
        print "Step2 completed\n";
        print $StepInfo;
        stepcheck ('Test');
}

#open($FH, '>', $filename1);
#print $FH "Fresh\n";
#close ($FH);

#perl -i -pe 'y|\r||d' script.pl

-----XXX----------

I set status to "stp_fail". when i execute the script I am expecting step2 to be executed. I see the its reading the right status but not executing the correct step rather both of them

-bash-4.2$ ./test_script.pl

value stp_fail

Step1 completed

Step2 completed

additionally the script is not changing the status in the text file. rather it creates 2 new files named Test and dh_fail.

kindly assist in what part i am doing wrong. thanks a lot in advance.

Aucun commentaire:

Enregistrer un commentaire