From acdf356312031a5753bfa9b35a3dcbda398ce50f Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 28 Jul 2024 11:00:56 -0400 Subject: [PATCH] fixup! --- src/sum.sh | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/sum.sh b/src/sum.sh index c2a3e2a..fa22739 100644 --- a/src/sum.sh +++ b/src/sum.sh @@ -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 - # Skip if no .sh files are found - [ -e "$file" ] || continue + echo "Using hash command: ${hash_cmd[*]}" - # Calculate the SHA-256 hash and save it to a .sha256 file - $hash_cmd "$file" | awk '{ print $1 }' > "$file.sha256" - done - } + # 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 - # Process the src directory - process_directory "$base_dir" + # Print the file being processed for debugging + echo "Processing file: $file" + + # 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." }