Add default value for shell parameter expansion to avoid script failure upon unset variable

This commit is contained in:
tosu 2023-08-26 19:53:22 +02:00
parent 7c883023bf
commit a71fba4d0f
Signed by: tosu
GPG Key ID: C00746F2E0F36492
1 changed files with 17 additions and 17 deletions

View File

@ -21,9 +21,9 @@ IFS="
POSIXLY_CORRECT='1' POSIXLY_CORRECT='1'
COMMANDS_='builtin unalias unset read printf command exit type . tr mkdir wc sed grep xargs ffmpeg jq wget' COMMANDS_='builtin unalias unset read printf command exit type . tr mkdir wc sed grep xargs ffmpeg jq wget'
# shellcheck disable=SC2086 # shellcheck disable=SC2086
\unset -f -- ${COMMANDS_} 2>/dev/null \unset -f -- ${COMMANDS_:-} 2>/dev/null
# shellcheck disable=SC2086 # shellcheck disable=SC2086
\unalias -- ${COMMANDS_} 2>/dev/null || true \unalias -- ${COMMANDS_:-} 2>/dev/null || true
command -v -- wget >/dev/null 2>&1 || { command -v -- wget >/dev/null 2>&1 || {
printf '\033[31m%s\033[m\n' "Please install wget" printf '\033[31m%s\033[m\n' "Please install wget"
@ -79,11 +79,11 @@ api-version=1.4-private
EOM EOM
API_URL="$( API_URL="$(
printf '%s' "${API_URL}" | printf '%s' "${API_URL:-}" |
tr -d -- '\t' tr -d -- '\t'
)" )"
mkdir -p -- "${OUTDIR}" || { mkdir -p -- "${OUTDIR:-}" || {
printf '\033[31m%s\033[m\n' 'Aborting' printf '\033[31m%s\033[m\n' 'Aborting'
exit 2 exit 2
} }
@ -95,44 +95,44 @@ m3u8_manifest_urls_and_metadata="$(
wget --no-verbose \ wget --no-verbose \
--quiet \ --quiet \
--output-document - \ --output-document - \
--header="authorization: Bearer ${BASE64_BEARER_TOKEN}" \ --header="authorization: Bearer ${BASE64_BEARER_TOKEN:-}" \
-- "${API_URL}" | -- "${API_URL:-}" |
jq --raw-output \ jq --raw-output \
'.value[] | .name + ">" + .media.duration[2:] + ">" + (.playbackUrls | map(select(.mimeType == "application/vnd.apple.mpegurl")) | .[].playbackUrl)' '.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.' printf '\033[31m%s\033[m\n' 'Error GETting manifest urls (response empty) or Bearer token invalid/expired. Exiting.'
exit 3 exit 3
} }
idx='1' 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=' 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 do
IFS='>' read -r title length m3u8_manifest_url <<- EOM IFS='>' read -r title length m3u8_manifest_url <<- EOM
${m3u8_manifest_url_and_metadata} ${m3u8_manifest_url_and_metadata:-}
EOM 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 \ ffmpeg -hide_banner \
-loglevel error \ -loglevel error \
-headers "authorization: Bearer ${BASE64_BEARER_TOKEN}" \ -headers "authorization: Bearer ${BASE64_BEARER_TOKEN:-}" \
-i "${m3u8_manifest_url}" \ -i "${m3u8_manifest_url:-}" \
-progress - \ -progress - \
-c copy \ -c copy \
"${filepath}" | "${filepath:-}" |
grep --line-buffered 'out_time=' | grep --line-buffered 'out_time=' |
sed -u -e 's/out_time=//g' -e 's/\..*//g' -e 's/^-//g' | 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" printf '\n\033[33m%s\033[m\n' "Done"