Files
static-pages/books.sh
T
hikari 6947821eaf
Security Scan and Upload / Security & DefectDojo Upload (pull_request) Successful in 49s
fix: use null-safe while loop for filename handling in scripts
2026-03-10 10:37:33 -07:00

37 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# Initialize an empty string to hold the list of books in JSON-like format
books=""
filecount=$(find "/mnt/c/Users/accou/Documents/iDrive/Cloud-Drive_accounts@nhcarrigan.com/Books" -type f | wc -l)
echo "Found $filecount files."
current=0
# Loop over each file found by find
while IFS= read -r -d $'\0' file; do
current=$((current + 1))
echo -ne "Processing $current/$filecount\r"
title=$(exiftool "$file" | grep "^Title\s*:" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
author=$(exiftool "$file" | grep "^Creator\s*:" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
if [ -z "$title" ]; then
# remove extension from the filename
title=$(basename "$file" | sed -e 's/\.[^.]*$//')
fi
if [ -z "$author" ]; then
author=$(exiftool "$file" | grep "^Author\s*:" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
fi
if [ -z "$author" ]; then
author="Unknown Author"
fi
# use jq to add the book to the list
books="$books$(jq -n --arg title "$title" --arg author "$author" '{title: $title, author: $author}'),"
done < <(find "/mnt/c/Users/accou/Documents/iDrive/Cloud-Drive_accounts@nhcarrigan.com/Books" -type f -print0)
# Remove trailing comma and add square brackets to complete the list
books="[${books%,}]"
# Write to ./books/books.json
echo "$books" > ./books/books.json
echo -ne "Done!\r"