This commit is contained in:
Colin 2024-07-28 11:00:56 -04:00
parent 883b3e05a5
commit acdf356312
1 changed files with 27 additions and 27 deletions

View File

@ -1,45 +1,45 @@
# /etc/profile.d/sum.sh
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This script should be sourced, not executed."
exit 1
fi
#!/bin/bash
# Function to generate SHA-256 hashes for .sh files in the src directory
calculate_hashes() {
local base_dir="./src"
local src_dir="./src"
# Check if the base directory exists
if [ ! -d "$base_dir" ]; then
echo "Base directory $base_dir does not exist."
# Check if the directory exists
if [ ! -d "$src_dir" ]; then
echo "Directory $src_dir does not exist."
return 1
fi
# Determine the hash command to use
local hash_cmd=""
# Determine the command to use for calculating SHA-256
local hash_cmd
if command -v sha256sum >/dev/null 2>&1; then
hash_cmd="sha256sum"
hash_cmd=("sha256sum")
elif command -v shasum >/dev/null 2>&1; then
hash_cmd="shasum -a 256"
hash_cmd=("shasum" "-a" "256")
else
echo "Neither sha256sum nor shasum is available."
echo "Neither sha256sum nor shasum command is available."
return 1
fi
# Function to process the src directory
process_directory() {
local dir="$1"
for file in "$dir"/*.sh; do
echo "Using hash command: ${hash_cmd[*]}"
# Loop through all .sh files in the directory
for file in "$src_dir"/*.sh; do
# Skip if no .sh files are found
[ -e "$file" ] || continue
# Calculate the SHA-256 hash and save it to a .sha256 file
$hash_cmd "$file" | awk '{ print $1 }' > "$file.sha256"
done
}
# Print the file being processed for debugging
echo "Processing file: $file"
# Process the src directory
process_directory "$base_dir"
# Calculate the SHA-256 hash and save it to a .sha256 file
result=$("${hash_cmd[@]}" "$file" 2>&1)
if [ $? -ne 0 ]; then
echo "Failed to execute ${hash_cmd[*]} for $file: $result"
continue
fi
echo "$result" | awk '{ print $1 }' > "$file.sha256"
done
echo "SHA-256 hashes have been saved in .sha256 files."
}