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.
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
}
}
#!/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]
#!/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.
#!/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 .
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.