From a71fba4d0f73aead138cdf27f6bf375bc45ea76f Mon Sep 17 00:00:00 2001 From: tosu Date: Sat, 26 Aug 2023 19:53:22 +0200 Subject: [PATCH] Add default value for shell parameter expansion to avoid script failure upon unset variable --- downloadPOSIX.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/downloadPOSIX.sh b/downloadPOSIX.sh index f44879e..41cc1ca 100755 --- a/downloadPOSIX.sh +++ b/downloadPOSIX.sh @@ -21,9 +21,9 @@ IFS=" POSIXLY_CORRECT='1' COMMANDS_='builtin unalias unset read printf command exit type . tr mkdir wc sed grep xargs ffmpeg jq wget' # shellcheck disable=SC2086 -\unset -f -- ${COMMANDS_} 2>/dev/null +\unset -f -- ${COMMANDS_:-} 2>/dev/null # shellcheck disable=SC2086 -\unalias -- ${COMMANDS_} 2>/dev/null || true +\unalias -- ${COMMANDS_:-} 2>/dev/null || true command -v -- wget >/dev/null 2>&1 || { printf '\033[31m%s\033[m\n' "Please install wget" @@ -79,11 +79,11 @@ api-version=1.4-private EOM API_URL="$( - printf '%s' "${API_URL}" | + printf '%s' "${API_URL:-}" | tr -d -- '\t' )" -mkdir -p -- "${OUTDIR}" || { +mkdir -p -- "${OUTDIR:-}" || { printf '\033[31m%s\033[m\n' 'Aborting' exit 2 } @@ -95,44 +95,44 @@ m3u8_manifest_urls_and_metadata="$( wget --no-verbose \ --quiet \ --output-document - \ - --header="authorization: Bearer ${BASE64_BEARER_TOKEN}" \ - -- "${API_URL}" | + --header="authorization: Bearer ${BASE64_BEARER_TOKEN:-}" \ + -- "${API_URL:-}" | jq --raw-output \ '.value[] | .name + ">" + .media.duration[2:] + ">" + (.playbackUrls | map(select(.mimeType == "application/vnd.apple.mpegurl")) | .[].playbackUrl)' )" -[ -z "${m3u8_manifest_urls_and_metadata}" ] && { +[ -z "${m3u8_manifest_urls_and_metadata:-}" ] && { printf '\033[31m%s\033[m\n' 'Error GETting manifest urls (response empty) or Bearer token invalid/expired. Exiting.' exit 3 } idx='1' -total="$(printf '%s\n' "${m3u8_manifest_urls_and_metadata}" | wc -l)" +total="$(printf '%s\n' "${m3u8_manifest_urls_and_metadata:-}" | wc -l)" IFS=' ' -for m3u8_manifest_url_and_metadata in ${m3u8_manifest_urls_and_metadata} +for m3u8_manifest_url_and_metadata in ${m3u8_manifest_urls_and_metadata:-} do IFS='>' read -r title length m3u8_manifest_url <<- EOM - ${m3u8_manifest_url_and_metadata} + ${m3u8_manifest_url_and_metadata:-} EOM - length="$(printf '%s' "${length}" | sed -e 's/\..*S/S/g' -e 's/M/:/g' -e 's/H/:/g')" + length="$(printf '%s' "${length:-}" | sed -e 's/\..*S/S/g' -e 's/M/:/g' -e 's/H/:/g')" - filepath="${OUTDIR}/${title}.mp4" + filepath="${OUTDIR:-}/${title:-}.mp4" - printf '\033[32m%s\033[m\n' "Download '${filepath}' (${idx}/${total})" + printf '\033[32m%s\033[m\n' "Download '${filepath:-}' (${idx:-}/${total:-})" ffmpeg -hide_banner \ -loglevel error \ - -headers "authorization: Bearer ${BASE64_BEARER_TOKEN}" \ - -i "${m3u8_manifest_url}" \ + -headers "authorization: Bearer ${BASE64_BEARER_TOKEN:-}" \ + -i "${m3u8_manifest_url:-}" \ -progress - \ -c copy \ - "${filepath}" | + "${filepath:-}" | grep --line-buffered 'out_time=' | sed -u -e 's/out_time=//g' -e 's/\..*//g' -e 's/^-//g' | - xargs -I{} printf '%s\r' "{} of ${length} " + xargs -I{} printf '%s\r' "{} of ${length:-} " printf '\n\033[33m%s\033[m\n' "Done"