Shell script for checking checksums

Jan Danielsson <[email protected]>
Newsgroups gmane.os.netbsd.help
Organization La cosa nostra
Message-ID <[email protected]>
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
signature.asc (application/pgp-signature, 186 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (NetBSD)

iD8DBQFFWKWubgs4ia4rq9IRClIcAJ9+pidIPdhAkAMHNA7mmzKP1JwH5wCfVxln
gS/zqgOQyfzkvXbgfC+8il4=
=jYSo
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.