Subject: Shell script for checking checksums
To: None <netbsd-help@netbsd.org>
From: Jan Danielsson <jan.m.danielsson@gmail.com>
List: netbsd-help
Date: 11/13/2006 18:04:46
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigA1C687B4DD522320D99E7F4C
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
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=3D"No error(s) encountered! Yay!"
for l in `cat $1 | sed 's/.*(//; s/).*=3D\ /:/'`
do
# extract file name
MD5PATHNAME=3D${l%%:*}
MD5CHECKSUM=3D${l##*:}
F=3D`md5 "$MD5PATHNAME" | sed 's/.*(//; s/).*=3D\ /:/'`
CHECKSUM=3D${F##*:}
if [ "$MD5CHECKSUM" =3D "$CHECKSUM" ]; then
echo "$MD5CHECKSUM =3D $CHECKSUM ($MD5PATHNAME) -- ok!"
else
echo "$MD5CHECKSUM !=3D $CHECKSUM ($MD5PATHNAME) -- error=
!"
MSG=3D"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?
--=20
Kind regards,
Jan Danielsson
--------------enigA1C687B4DD522320D99E7F4C
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (NetBSD)
iD8DBQFFWKWubgs4ia4rq9IRClIcAJ9+pidIPdhAkAMHNA7mmzKP1JwH5wCfVxln
gS/zqgOQyfzkvXbgfC+8il4=
=jYSo
-----END PGP SIGNATURE-----
--------------enigA1C687B4DD522320D99E7F4C--