Perl SCript to read file content (if else statemenet)

Hi All,
I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this.

If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero)
else
if (content =0), it will update the content to 1 and do nothing.

The script will be run at every Saturday.

Can anyone advise?

What are the steps that needs to be performed in case if the content is 1

Try:

open(FH,"</path/to/your/file/filename");
my @file = <FH>;
close FH;
my $line;
foreach $line (@file) {
  if ( $line ~= m/1/) {
    #Do your thing.
  } elsif { $line ~= m/0/) {
    #Again Do your thing
  }
}
1 Like
#!/usr/bin/perl

open (FH, '>filereindex.txt') or die 'Could not open file';
my @line = <FH>;
  if ($line == 1) {
    print "NUMBER 1";
  } elsif ( $line == 0) {
   print FH 1;

  }

I m using the above,

if fileindex.txt is 0, then it will write 1 to the file: fileindex.txt [expected result that I want]
BUT
if fileindex.txt is 1, then it will do nothing, I want it to print NUMBER 1

---------- Post updated at 04:24 PM ---------- Previous update was at 04:21 PM ----------

#!/usr/bin/perl

open (FH, 'filereindex.txt') or die 'Could not open file';
my @line = <FH>;
  if ($line == 1) {
    print "NUMBER 1";
  } elsif ( $line == 0) {
   print FH 1;

  }

I remove the > from the code. IF filereindex.txt = 1, it will print NUMBER 1 [result that I want]
BUT
if filereindex.txt=0, it do nothing [i wanted it to write 1 to the filereindex.txt]

Did you try the code that I gave? Remove the comments and replace them with your print statements. And the code that you gave will not work.

You have declared line as an array and when you are checking you use it as scalar. Provide the index.

I have the below when run the one you gave.

#!/usr/bin/perl

open(FH,"<filereindex.txt");
my @file = <FH>;
close FH;
my $line;
foreach $line (@file){
  if($line ~= m/1/){
    print "NUMBER 1";

  }elsif($line ~= m/0/){
    print "ZERO";
  }
}

syntax error at ./biw.pl line 8, near "$line ~"
syntax error at ./biw.pl line 11, near "}elsif"
Execution of ./biw.pl aborted due to compilation errors

my file only have either 0 or 1.
if 1, then it will run reindexing on the application, and then change file content from 1 to 0 , then exit

else if file content=0, it will change file content from 0 to 1 and then exit.

Sorry my mistake. Pls replace ~= with =~

it work.

filereindex content = 1
So, it print Number 1, after the print, I would like to write 0 to the filereindex and exit.

I know the simple writing to a file, but no idea how to put in my script.

#!/usr/bin/perl
open (FH, '>filereindex.txt');
print FH 0;

Instead of print ZERO statement, put the last two lines of your code there and dont forget to close the file.

PS: Use code tags.

Thanks! sorry, was busy for other stuff.

Here my final script.

#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
use Data::Dumper;
use LWP::UserAgent;

open(FH,"<filereindex.txt");
my @file = <FH>;
close FH;
my $line;
foreach $line (@file)
{
    if($line =~ m/1/)
    {
       print "hello";
        open (FH, '>filereindex.txt');
        print FH 0;
        close FH;
        exit $error;
    } else {
        # We already checked for 1, this will be the 0.
        open (FH, '>filereindex.txt');
        print FH 1;
        close FH;
        exit;
    }
}

I can trigger it and it works fine in the command line, ./script.pl

But then when i place it in crontab, it dint do anything (Not working) , I have pipe the error to log file, and here the log,

readline() on closed filehandle FH at /opt/apps/jira/libexec/biweeklyreindex.pl
line 10 (#1)
(W closed) The filehandle you're reading from got itself closed sometime
before now. Check your control flow.

Can advise? Thanks

---------- Post updated 05-27-14 at 01:51 PM ---------- Previous update was 05-26-14 at 05:59 PM ----------

I edited the script with the below:

 open(FH,"<filereindex.txt") or die "Can't open file!"; 

and in the error log, I have the error "Can't open file! at script path .

Is there syntax error? Thanks!

Does the file exists? The error is coming because the script is unable to find the input file. Try giving the full path to the input file or else it will check for the input file in cwd.

1 Like

yes, i just thinking of this..Anyway, thank for your advise too. Going to try on it.Thanks!

---------- Post updated at 02:42 PM ---------- Previous update was at 02:25 PM ----------

Full path save my life :slight_smile: thanks!