Compare commits
2 Commits
06a955e042
...
943ae99ef0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
943ae99ef0 | ||
|
|
ba683a9cf9 |
101
miscellaneous/crc32_checksum_manager.sh
Normal file
101
miscellaneous/crc32_checksum_manager.sh
Normal file
@@ -0,0 +1,101 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script manages CRC32 checksums for files and directories.
|
||||
# It can create a checksum file for specified files or directories and verify them later.
|
||||
#
|
||||
# Usage:
|
||||
# To create a checksum file for a file or directory:
|
||||
# ./crc32_checksum_manager.sh --create /path/to/file_or_directory [output_file.txt]
|
||||
# To verify checksums using a checksum file:
|
||||
# ./crc32_checksum_manager.sh --verify /path/to/checksums.txt
|
||||
# Use -h or --help for usage information.
|
||||
|
||||
# Error Messages:
|
||||
# ERROR: INVALID_PATH: $path
|
||||
# Indicates that the specified path is neither a file nor a directory.
|
||||
# ERROR: CHECKSUM_FILE_NOT_FOUND: $checksum_file
|
||||
# Indicates that the specified checksum file does not exist.
|
||||
# ERROR: CHECKSUM_INVALID: $file
|
||||
# Indicates that the calculated checksum for the file does not match the expected checksum.
|
||||
# ERROR: MISSING_ARGUMENT: [specific_message]
|
||||
# Indicates that a required argument (file or directory or checksum file) is missing.
|
||||
# ERROR: INVALID_OPTION: $1
|
||||
# Indicates that an invalid option was provided to the script.
|
||||
|
||||
# Function to display a help message
|
||||
show_help() {
|
||||
echo "Usage: $0 [OPTION] [FILE_OR_DIRECTORY] [OUTPUT_FILE]"
|
||||
echo "Options:"
|
||||
echo " -c, --create Create a CRC32 checksum file for the specified file or directory."
|
||||
echo " Optionally specify an output file name for the checksums (default: checksums.txt)."
|
||||
echo " -v, --verify Verify the CRC32 checksums of the specified files or directory."
|
||||
echo " -h, --help Display this help message."
|
||||
}
|
||||
|
||||
# Function to create a CRC32 checksum file
|
||||
create_checksum() {
|
||||
local path="$1" # Path to the file or directory to create checksums for
|
||||
local output_file="${2:-checksums.txt}" # Name of the output file where checksums will be saved, defaulting to 'checksums.txt'
|
||||
|
||||
if [ -d "$path" ]; then # Check if the path is a directory
|
||||
# Find all files in the directory and calculate their CRC32 checksums
|
||||
find "$path" -type f -exec sh -c 'crc32 "$1" | awk "{print \$1, \"$1\"}"' _ {} \; > "$output_file"
|
||||
elif [ -f "$path" ]; then # Check if the path is a file
|
||||
# Calculate the CRC32 checksum for the file and save it to the output file
|
||||
crc32 "$path" > "$output_file"
|
||||
else # If the path is neither a file nor a directory
|
||||
echo "ERROR: INVALID_PATH: $path"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to verify CRC32 checksums
|
||||
verify_checksum() {
|
||||
local checksum_file="$1" # Path to the checksum file
|
||||
|
||||
if [ ! -f "$checksum_file" ]; then # Check if the checksum file exists
|
||||
echo "ERROR: CHECKSUM_FILE_NOT_FOUND: $checksum_file"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Read each line of the checksum file
|
||||
while read -r line; do
|
||||
checksum=$(echo "$line" | awk '{print $1}') # Extract the checksum from the line
|
||||
file=$(echo "$line" | awk '{print $2}') # Extract the file path from the line
|
||||
# Compare the calculated checksum with the stored checksum
|
||||
if [ "$(crc32 "$file")" != "$checksum" ]; then
|
||||
echo "ERROR: CHECKSUM_INVALID: $file"
|
||||
fi
|
||||
done < "$checksum_file" # Redirect the checksum file to the while loop
|
||||
}
|
||||
|
||||
# Check the script's arguments and execute the appropriate functions
|
||||
case "$1" in
|
||||
-c|--create)
|
||||
if [ -z "$2" ]; then # Check if a file or directory was provided
|
||||
echo "ERROR: MISSING_ARGUMENT: No file or directory specified."
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Call the create_checksum function with the provided path and optionally a custom output file
|
||||
create_checksum "$2" "$3"
|
||||
;;
|
||||
-v|--verify)
|
||||
if [ -z "$2" ]; then # Check if a checksum file was provided
|
||||
echo "ERROR: MISSING_ARGUMENT: No checksum file specified."
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
verify_checksum "$2" # Call the verify_checksum function with the provided checksum file
|
||||
;;
|
||||
-h|--help)
|
||||
show_help # Display the help message
|
||||
;;
|
||||
*)
|
||||
echo "ERROR: INVALID_OPTION: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user