From c261dca844020a8cb7c8ecfb2714895c1621d95b Mon Sep 17 00:00:00 2001 From: Urban Modig Date: Sun, 26 Oct 2025 23:45:10 +0100 Subject: [PATCH] truncating --- download.sh | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/download.sh b/download.sh index a00a8e9..553e28a 100755 --- a/download.sh +++ b/download.sh @@ -170,9 +170,30 @@ get_items() { # Expecting a single line: ["",""] JSON_PAIR_RE='^\["([^"]*)","([^"]*)"\]$' +#sanitize_filename() { +# local in=$1 +# local truncated=${in:0:80} +# # replace anything not alnum, underscore, dot or dash with underscore +# printf '%s' "$truncated" | sed 's/[^A-Za-z0-9_.-]/_/g' +#} + sanitize_filename() { local in=$1 - local truncated=${in:0:80} + local truncated + # find cutoff at first space after 80th character + if (( ${#in} > 80 )); then + local prefix=${in:0:80} + local remainder=${in:80} + if [[ $remainder == *' '* ]]; then + local next_space_pos + next_space_pos=$(expr index "$remainder" ' ') + truncated="${prefix}${remainder:0:next_space_pos}" + else + truncated=$in + fi + else + truncated=$in + fi # replace anything not alnum, underscore, dot or dash with underscore printf '%s' "$truncated" | sed 's/[^A-Za-z0-9_.-]/_/g' }