For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > script help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author script help
Tony Heal

2007-08-04, 7:01 pm

Below is a script I have been working on that verifies if another script has created the MD5 hash file correctly. The
below works with

if ( $checkFile = $file )

but not if

if ( $checkFile != $file )



and I do not understand why that is.



Can someone explain what I am doing wrong?



Thanks

Tony







#!/usr/bin/perl

# AUTHOR Tony Heal - theal@pace2020.com

use strict;

use warnings;

use Digest::MD5 qw(md5);

use Sys::Hostname;



my $filesize;

my $testPrefix = "200708040030";



#verify the MD5 hash use Digest::MD5 my $digest = file_md5($file); if orig md5 == new then good

my $host = hostname;

open(MD5FILE, "</opt/backup/$testPrefix-$host-md5-checksums.txt") or die "Can not open MD5-checksum file. $!";

my %MD5HASH;

while(<MD5FILE> )

{

if ( /^([0-9A-Za-z]*?)(\s*)([0-9]{12}-(.)*)/ )

{

my $key = $1;

my $value = $3;

$key =~ s/^(\s+)(.*?)(\s+)$/$2/;

$value =~ s/^(\s+)(.*?)(\s+)$/$2/;

$MD5HASH{$key} = $value;

}

}

close MD5FILE;



opendir(BACKUP, "/opt/backup" ) or die "Can not open directory '/opt/epace/backup/'. $!";

my @md5FileList = readdir(BACKUP);

closedir BACKUP;



while (<@md5FileList> )

{

my $file = $_;

if ( $file =~ /$testPrefix/ )

{

my $digest = md5 $file;

my $checkFile = $MD5HASH{$digest};

if ( $checkFile = $file )

{

print "$file has a good MD5 match\n";

}

}

}



/opt/backup/$testPrefix-$host-md5-checksums.txt:



f522c8928669f423c64fd63e7dc58f4a 200708040030-dump

5e7980aebafac3023bce54093978db5f 200708040030-sample-dump

c02c0b360a51e7af704648431c367415 200708040030-conf.tar.gz

0598e857f3e2575fb4a9a542429f347b 200708040030-reports.tar.gz

0a91aab9779829118f29e8076ada2264 200708040030-logs.tar.gz

a9cf42eff4ab11a200e7891f9a9e95cb 200708040030-scripts.tar.gz

6d060f40e0b3bd3309ec93b23799254f 200708040030-custom.tar.gz

acd9d62f1bd034ff4381a76b10f1ac0a 200708040030-client-info.tar.gz

7a321bff85167b425b3b3c3b4f2f728f 200708040030-crons.tar.gz




Mr. Shawn H. Corey

2007-08-04, 7:01 pm

Tony Heal wrote:
> Below is a script I have been working on that verifies if another script has created the MD5 hash file correctly. The
> below works with
>
> if ( $checkFile = $file )


This assigns the content of $file to $checkFile and executes the block if it's content is not false (in the Perl sense).

>
> but not if
>
> if ( $checkFile != $file )


This numerically compares the contents of $file and $checkFile and executes the block if they are the same number.

>
>
>
> and I do not understand why that is.
>
>
>
> Can someone explain what I am doing wrong?


You are using an assignment and a numeric compare where you should be using a string compare. See `perldoc perlop` and search for 'Equality Operators'.


--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com