bashgrepmd5md5sum

MD5 Script and grep cmd


I found this neat little script on the Ubuntu website. The script verifies contents of CD by comparing it to an MD5 sum of an iso(9660) file. I don't understand why the script parses the string after the md5 sum is computed.

CSUM1=$(CHECKSUM "$1" | grep -om1 '^[0-9a-f]*')

How can I verify the the cmdline argument passed to the script is a real md5 hash (case insensitive and hex(0-9/a-f)). There're several snippets online showing how to verify md5 hashes. I want to verify and grep the MD5 hash if an .md5 file is passed as an argument or verify the hash if its not. I got the script to compute and compare...

# Compares the checksums of an iso9660 image and a burned disk.
# This script is released into the public domain by it's author.
if [ -n "$BASH" ]
then
  shopt -s expand_aliases
fi

if [ -n "$CHECKSUM" ]
then
  alias CHECKSUM="$CHECKSUM"
elif which md5deep &> /dev/null
then
  alias CHECKSUM='md5deep -e'
else
  alias CHECKSUM='md5sum'
fi

if [ -n "$2" ]
then
  DISKDEVICE="$2"
else
  DISKDEVICE='/dev/cdrom'
fi

CSUM1=$(CHECKSUM "$1" | grep -om1 '^[0-9a-f]*')
# extract cmdline arg
echo 'checksum for input image:' $CSUM1
SIZE=$(stat -c '%s' "$1")
BLOCKS=$(expr $SIZE / 2048)
CSUM2=$(dd if="$DISKDEVICE" bs=2048 count=$BLOCKS 2>/dev/null | CHECKSUM | grep -om1 '^[0-9a-f]*')
echo 'checksum for output disk:' $CSUM2

if [ "$CSUM1" = "$CSUM2" ]
then
  echo 'verification successful!'
else
  echo 'verification failed!'
fi

Solution

  • I don't understand why the script parses the string after the md5 sum is computed. CSUM1=$(CHECKSUM "$1" | grep -om1 '^[0-9a-f]*')

    It has to parse it because md5sum, et al don't just put out the hash

    $ md5sum lupu-528.005.iso
    8ad170c46b523436776398fa5ce39fa4 *lupu-528.005.iso
    

    How can I verify the the cmdline argument passed to the script is a real md5 hash

    With that script the intention is to pass a file, not a hash.