shell-utils/sum.sh

34 lines
785 B
Bash
Executable File

#!/bin/bash
# Directory containing the .sh files
src_dir="./src"
# Check if the directory exists
if [ ! -d "$src_dir" ]; then
echo "Directory $src_dir does not exist."
exit 1
fi
# Determine the command to use for calculating SHA-256
if command -v sha256sum >/dev/null 2>&1; then
hash_cmd="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
hash_cmd="shasum -a 256"
else
echo "Neither sha256sum nor shasum command is available."
exit 1
fi
# 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
echo "SHA-256 hashes have been saved in .sha256 files."