mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-03 09:48:29 +00:00
79 lines
1.3 KiB
Bash
Executable File
79 lines
1.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# x-sha256sum-matcher
|
|
#
|
|
# Check if two files are the same
|
|
#
|
|
# Ismo Vuorinen <https://github.com/ivuorinen> 2023
|
|
# MIT License
|
|
|
|
set -euo pipefail
|
|
|
|
# ENV Variables
|
|
: "${VERBOSE:=0}" # VERBOSE=1 x-sha256sum-matcher file1 file2
|
|
|
|
# Return sha256sum for file
|
|
# $1 - filename (string)
|
|
get_sha256sum()
|
|
{
|
|
sha256sum "$1" | head -c 64
|
|
}
|
|
|
|
# Print message if VERBOSE is enabled
|
|
# $1 - message (string)
|
|
msg()
|
|
{
|
|
[[ "$VERBOSE" -eq 1 ]] && echo "$1"
|
|
}
|
|
|
|
# Print error message and exit
|
|
# $1 - error message (string)
|
|
error()
|
|
{
|
|
msg "(!) ERROR: $1"
|
|
exit 1
|
|
}
|
|
|
|
# Validate input arguments
|
|
validate_inputs()
|
|
{
|
|
if [ "$#" -ne 2 ]; then
|
|
echo "Usage: $0 file1 file2"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Check if file exists
|
|
# $1 - filename (string)
|
|
check_file_exists()
|
|
{
|
|
local filename=$1
|
|
if [ ! -f "$filename" ]; then
|
|
error "File does not exist: $filename"
|
|
fi
|
|
}
|
|
|
|
# Main function
|
|
main()
|
|
{
|
|
local file_1=$1
|
|
local file_2=$2
|
|
|
|
validate_inputs "$file_1" "$file_2"
|
|
check_file_exists "$file_1"
|
|
check_file_exists "$file_2"
|
|
|
|
local file_1_hash
|
|
local file_2_hash
|
|
|
|
file_1_hash=$(get_sha256sum "$file_1")
|
|
file_2_hash=$(get_sha256sum "$file_2")
|
|
|
|
if [ "$file_1_hash" != "$file_2_hash" ]; then
|
|
error "Files do not match"
|
|
else
|
|
msg "(*) Success: Files do match"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|