Skip to content

Instantly share code, notes, and snippets.

@amhendley
Last active March 4, 2017 10:16
Show Gist options
  • Save amhendley/33f22393ffee05c825da8acfbb207aa2 to your computer and use it in GitHub Desktop.
Save amhendley/33f22393ffee05c825da8acfbb207aa2 to your computer and use it in GitHub Desktop.
Quick and easy script created to be able to get the MD5 digest of a file on either Linux or SunOS
#!/bin/bash
sysname=`uname -s`
if [ "$sysname" = "SunOS" ]; then
r=`which md5sum | grep '/md5sum'`
if [ $r ]; then HAS_MD5SUM="1"; else HAS_MD5SUM="0"; fi
r=`which digest | grep '/digest'`
if [ $r ]; then HAS_DIGEST="1"; else HAS_DIGEST="0"; fi
else
r=`which md5sum &>/dev/null`; if [ "$?" = "0" ]; then HAS_MD5SUM="1"; else HAS_MD5SUM="0"; fi
r=`which digest &>/dev/null`; if [ "$?" = "0" ]; then HAS_DIGEST="1"; else HAS_DIGEST="0"; fi
fi
if [ "$HAS_MD5SUM" = "1" ]; then
md5sum $1 | cut -d ' ' -f1
elif [ "$HAS_DIGEST" = "1" ]; then
digest -a md5 $1
else
echo "Unable to generate file signature as no digest command could be found"
exit 1
fi
@amhendley
Copy link
Author

If your wondering why the different code paths between Linux and SunOS was mainly due to the fact that under SunOS, the pipe out to /dev/null did not work. Hence the usage of grep under SunOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment