List Info

Thread: Shell script for checking checksums




Shell script for checking checksums
user name
2006-11-13 17:04:46
Hello all,

   Before I burn a CD or a DVD, I create an MD5SUM and an
SHA1SUM file
on the image. These files - obviously - contain the output
of "md5 * >
MD5SUM" and "sha1 * > SHA1SUM".

   Anyhow, a friend of mine told me that his Ubuntu system's
md5sum can
automatically check the checksums for him. I - on the other
hand - to
check them manually, which is deadly boring.

   So I thought I would change that with the little shell
programming I
know. I came up with this:

-----------------------------
#!/bin/sh
MSG="No error(s) encountered! Yay!"
for l in `cat $1 | sed 's/.*(//; s/).*= /:/'`
do
        # extract file name
        MD5PATHNAME=${l%%}
        MD5CHECKSUM=${l##*:}

        F=`md5 "$MD5PATHNAME" | sed 's/.*(//;
s/).*= /:/'`
        CHECKSUM=${F##*:}
        if [ "$MD5CHECKSUM" =
"$CHECKSUM" ]; then
                echo "$MD5CHECKSUM = $CHECKSUM
($MD5PATHNAME) -- ok!"
        else
                echo "$MD5CHECKSUM != $CHECKSUM
($MD5PATHNAME) -- error!"
                MSG="ERRORS ENCOUNTERED!! Oh,
noo!"
        fi

done

echo $MSG
-----------------------------

   This works fine -- as long as there are no spaces in the
file name.
Problem is -- I have lots of spaces in lots of file names. I
understand
that the problem lays in that "for l in
<blah>" treats a space as a file
name separator, and I am guessing that this is a design flaw
which can
not be worked around. I did try to modify the sed command to
replace the
file name:

This is a file name.txt

   ..with:

This is a file name.txt

   ..but that didn't work, so I tried replacing it with:

"This is a file name.txt"

   ..but that didn't work either. So, my first attempt at
shell hacking
failed miserably. :-(  So, does anyone have a working
solution which
does what I want?

-- 
Kind regards,
Jan Danielsson


Shell script for checking checksums
user name
2006-11-13 17:40:52
Hi,

Jan Danielsson --> netbsd-help (2006-11-13 18:04:46
+0100):
[...]
>    Anyhow, a friend of mine told me that his Ubuntu
system's md5sum can
> automatically check the checksums for him. I - on the
other hand - to
> check them manually, which is deadly boring.

The cksum(1) tools shipping with NetBSD 4 and -current have
a `-c
<file>' option which does what you want.


>    So I thought I would change that with the little
shell programming I
> know. I came up with this:
[...]
>    This works fine -- as long as there are no spaces in
the file name.
> Problem is -- I have lots of spaces in lots of file
names. I understand
> that the problem lays in that "for l in
<blah>" treats a space as a file
> name separator, and I am guessing that this is a design
flaw which can
> not be worked around.

You could start by reading the checksum file line by line:

	while read line; do
		# now `$line' is the next line read from $1
	done <$1

To parse the md5(1) output, note that everything between the
first
opening backet '(' and the last closing bracket ')' is the
filename,
and the last word of the line is the checksum.


HTH, Jukka

-- 
bashian roulette:
$ ((RANDOM%6)) || rm -rf ~
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )