my $sumInfo = `md5sum test.txt --quiet -c checkFile`
This works well enough provided that "checkFile" exists; and that it's formatted in accord with md5sum's conventions.
However, I'm wanting to run md5sum to do some automated file verification work, and need to glean any errors which might be thrown along the way when:
"checkFile"doesn't exist
"checkFile"is not properly formatted
Surprisingly, md5sum seems to exit with an "OK" code whether or not the above criteria are met; despite its passing an error message to the terminal. In other words, all looks as though the check was performed successfully on "test.txt" even if "checkFile"is damaged or missing. This causes any downstream detection logic to be a good bit less than robust.
When I consider the snippet I don't understand it.
I know the following:
md5sum test.txt
That creates a hash and output it to the stdout
md5sum -c checkFile
That checks the signature for every hash/file in checkFile
--quiet is just a flag to suppress some verbosity
But what does md5sum test.txt --quiet -c checkFile do.
Creates a hash for test.txt and check the integrity of what every hash mentioned is in checkFile? I think not.
---------- Post updated at 06:41 PM ---------- Previous update was at 06:31 PM ----------
Never mind. I see what it is doing.
It takes test.txt and checkFile as hash-files because of the -c regardless of the position of the flag
---------- Post updated at 10:54 PM ---------- Previous update was at 06:41 PM ----------
Perhaps you might benefit of implementing your own md5checks using native perl instead of making a system call.
Here's a tiny example of how to check the sums of some files in test.md5
You need to have the module Digest::MD5
This can easily be modified to your needs; to make it quiet or to give you a true or false return
#!/usr/bin/perl
# Author: Aia
# Date: 5-22-2014
# check_md5.pl
# reads a md5 hash file pair formatted file and checks its sum
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
my $md5_file = "test.md5";
my %groups;
open my $fh_in, '<', $md5_file or die "Can't open $md5_file: $!\n";
while (my $line = <$fh_in>) {
chomp $line;
my ($key, $value) = split(" ", $line);
$groups{$key} = $value;
}
close ($fh_in);
for my $key (keys %groups) {
open my $fh, '<', $groups{$key} or die "Can't open $groups{$key}: $!\n";
binmode ($fh);
my $md5 = Digest::MD5->new;
$md5->addfile($fh);
close ($fh);
if ( $key eq $md5->hexdigest ) {
print "$groups{$key}: OK\n";
}
else {
print "$groups{$key}: FAILED\n";
}
}