fix: handle multiple files correctly in find output

This commit is contained in:
Anna Sholz 2025-03-10 11:43:11 +03:00 committed by GitHub
parent 898350e271
commit 80fd9e1a91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,13 +3,19 @@
directory="$1" # The directory to search in
filename="$2" # The filename to search for
# Find the file in the directory
found_files=$(find "$directory" -type f -name "$filename")
# Ensure both arguments are provided
if [ -z "$directory" ] || [ -z "$filename" ]; then
echo "Usage: $0 <directory> <filename>" >&2
exit 1
fi
# Find the file and store results in an array
mapfile -t found_files < <(find "$directory" -type f -name "$filename")
# Check if any files were found
if [ -z "$found_files" ]; then
if [ "${#found_files[@]}" -eq 0 ]; then
echo "Error: No files named '$filename' found in directory '$directory'." >&2
exit 1
exit 1
else
echo "$found_files"
fi
printf "%s\n" "${found_files[@]}"
fi