Skip to content

Instantly share code, notes, and snippets.

@FlyingFathead
Last active July 2, 2024 10:00
Show Gist options
  • Save FlyingFathead/9f39b1b97e5db958026e894913171d6a to your computer and use it in GitHub Desktop.
Save FlyingFathead/9f39b1b97e5db958026e894913171d6a to your computer and use it in GitHub Desktop.
Verify tarballs locally against a md5 checksum (if available) and check for tarball integrity with `tar`
#!/bin/bash
# Function to print usage
usage() {
echo "Usage: $0 [-c <checksum file>] <tarball>"
echo " -c <checksum file> : Optional, specify the checksum file to verify the tarball against"
exit 1
}
# Check if no arguments were provided
if [ $# -eq 0 ]; then
usage
fi
# Parse options
CHECKSUM_FILE=""
while getopts ":c:" opt; do
case ${opt} in
c )
CHECKSUM_FILE=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
usage
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
usage
;;
esac
done
shift $((OPTIND -1))
# Check if tarball is provided
TARBALL=$1
if [ -z "$TARBALL" ]; then
usage
fi
# If no checksum file is provided, look for a matching .md5 file
if [ -z "$CHECKSUM_FILE" ]; then
CHECKSUM_FILE="${TARBALL}.md5"
if [ ! -f "$CHECKSUM_FILE" ]; then
echo "No checksum file provided and no matching checksum file found: $CHECKSUM_FILE"
CHECKSUM_FILE=""
fi
fi
# Verify the tarball integrity
echo "Verifying the tarball integrity..."
tar -tvf "$TARBALL" > /dev/null
if [ $? -eq 0 ]; then
echo "Tarball integrity check passed."
else
echo "Tarball integrity check failed."
exit 1
fi
# Verify checksum if found
if [ -n "$CHECKSUM_FILE" ]; then
echo "Verifying the checksum..."
if [ -f "$CHECKSUM_FILE" ]; then
md5sum -c "$CHECKSUM_FILE"
if [ $? -eq 0 ]; then
echo "Checksum verification passed."
else
echo "Checksum verification failed."
exit 1
fi
else
echo "Checksum file not found: $CHECKSUM_FILE"
exit 1
fi
fi
echo "All checks passed successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment