From bc021079cc1c0ad16ac8934f71452e8d909a0b6e Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Wed, 30 Aug 2023 18:34:44 +0300 Subject: [PATCH] Shellcheck warnings and errors --- deb-get | 160 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 62 deletions(-) diff --git a/deb-get b/deb-get index d46c99214..1191b44bb 100755 --- a/deb-get +++ b/deb-get @@ -112,6 +112,7 @@ HELP } function package_is_installed() { + # shellcheck disable=SC2076 if [[ " ${INSTALLED_APPS[*]} " =~ " ${1} " ]]; then return 0; fi return 1 } @@ -149,7 +150,8 @@ function create_etc_dir() { function unroll_url() { # Sourceforge started adding parameters - local TRIM_URL="$(curl -w "%{url_effective}\n" -I -L -s -S "${1}" -o /dev/null)" + local TRIM_URL + TRIM_URL="$(curl -w "%{url_effective}\n" -I -L -s -S "${1}" -o /dev/null)" echo "${TRIM_URL/\.deb*/.deb}" } @@ -163,7 +165,7 @@ function get_github_releases() { # Do not process github releases while generating a pretty list or upgrading if [ "${ACTION}" == "install" ] || [ "${ACTION}" == "update" ] || [ "${ACTION}" == "fix-installed" ]; then - if [ ! -e "${CACHE_FILE}" ] || test "$(find "${CACHE_FILE}" -mmin +${DEBGET_CACHE_RTN:-60})"; then + if [ ! -e "${CACHE_FILE}" ] || test "$(find "${CACHE_FILE}" -mmin +"${DEBGET_CACHE_RTN:-60}")"; then fancy_message info "Updating ${CACHE_FILE}" local URL="https://api.github.com/repos/${1}/releases" if [ -n "${2}" ]; then @@ -183,7 +185,7 @@ function get_website() { METHOD="website" CACHE_FILE="${CACHE_DIR}/${APP}.html" if [ "${ACTION}" == "install" ] || [ "${ACTION}" == "update" ] || [ "${ACTION}" == "fix-installed" ]; then - if [ ! -e "${CACHE_FILE}" ] || test "$(find "${CACHE_FILE}" -mmin +${DEBGET_CACHE_RTN:-60})"; then + if [ ! -e "${CACHE_FILE}" ] || test "$(find "${CACHE_FILE}" -mmin +"${DEBGET_CACHE_RTN:-60}")"; then fancy_message info "Updating ${CACHE_FILE}" if ! ${ELEVATE} wget -q --no-use-server-timestamps "${1}" -O "${CACHE_FILE}"; then fancy_message warn "Updating ${CACHE_FILE} failed. Deleting it." @@ -348,12 +350,13 @@ function remove_deb() { local FILE="${FILE:-${URL##*/}}" local STATUS="" + # shellcheck disable=SC2076 if package_is_installed "${APP}" || [[ " ${DEPRECATED_INSTALLED[*]} " =~ " ${APP} " ]]; then STATUS="$(dpkg -s "${APP}" | grep ^Status: | cut -d" " -f2-)" if [ "${STATUS}" == "deinstall ok config-files" ]; then REMOVE="purge" fi - ${ELEVATE} apt-get -q -y --autoremove ${REMOVE} "${APP}" + ${ELEVATE} apt-get -q -y --autoremove "${REMOVE}" "${APP}" remove_installed "${APP}" maint_supported_cache else @@ -394,8 +397,10 @@ function info_deb() { function validate_deb() { local FULL_APP="${1}" - export APP_SRC="$(echo "${FULL_APP}" | cut -d / -f 1)" - export APP="$(echo "${FULL_APP}" | cut -d / -f 2)" + APP_SRC="$(echo "${FULL_APP}" | cut -d / -f 1)" + export APP_SRC + APP="$(echo "${FULL_APP}" | cut -d / -f 2)" + export APP export DEFVER="" export ASC_KEY_URL="" export GPG_KEY_URL="" @@ -421,8 +426,10 @@ function validate_deb() { if [ "${APP_SRC}" == "00-builtin" ]; then deb_"${APP}" 2>/dev/null else + # shellcheck disable=SC1090 . "${ETC_DIR}/${APP_SRC}.d/${APP}" 2>/dev/null fi + # shellcheck disable=SC2076 if [[ " ${ARCHS_SUPPORTED} " =~ " ${HOST_ARCH} " ]] && { [ -z "${CODENAMES_SUPPORTED}" ] || [[ " ${CODENAMES_SUPPORTED} " =~ " ${UPSTREAM_CODENAME} " ]]; } && { [ "${METHOD}" != ppa ] || [ "${UPSTREAM_ID}" == ubuntu ]; }; then if [ -z "${DEFVER}" ] || [ -z "${PRETTY_NAME}" ] || [ -z "${SUMMARY}" ] || [ -z "${WEBSITE}" ]; then @@ -514,6 +521,7 @@ function validate_deb() { function list_debs() { if [ "${1}" == --include-unsupported ]; then + # shellcheck disable=2031 disable=2030 if [ "${2}" = --raw ]; then local OLD_IFS="$IFS" IFS=$'\n' @@ -525,14 +533,13 @@ function list_debs() { echo "${INSTALLED_APPS[*]}" IFS="$OLD_IFS" elif [ "${2}" = --not-installed ]; then - local NOT_INSTALLED_APPS=($( - IFS=$'\n' - echo "${APPS[*]}" | cut -d / -f 2 - )) - - for APP in ${INSTALLED_APPS[@]}; do + local NOT_INSTALLED_APPS=() + IFS=$'\n' + echo "${APPS[*]}" | cut -d / -f 2 | + while IFS="" read -r line; do NOT_INSTALLED_APPS+=("$line"); done + for APP in "${INSTALLED_APPS[@]}"; do for i in "${!NOT_INSTALLED_APPS[@]}"; do - if [[ ${NOT_INSTALLED_APPS[i]} = $APP ]]; then + if [[ ${NOT_INSTALLED_APPS[i]} = "$APP" ]]; then unset 'NOT_INSTALLED_APPS[i]' fi done @@ -545,7 +552,8 @@ function list_debs() { else local PAD=' ' for FULL_APP in "${APPS[@]}"; do - local APP="$(echo "${FULL_APP}" | cut -d / -f 2)" + local APP + APP="$(echo "${FULL_APP}" | cut -d / -f 2)" if package_is_installed "${APP}"; then printf "%s %s [ installed ]\n" "${APP}" "${PAD:${#APP}}" else @@ -573,6 +581,7 @@ function list_debs() { # because we need to update the cache files this one slow time for FULL_APP in "${APPS[@]}"; do validate_deb "${FULL_APP}" + # shellcheck disable=SC2076 if [[ " ${ARCHS_SUPPORTED} " =~ " ${HOST_ARCH} " ]] && { [ -z "${CODENAMES_SUPPORTED}" ] || [[ " ${CODENAMES_SUPPORTED} " =~ " ${UPSTREAM_CODENAME} " ]]; } && { [ "${METHOD}" != ppa ] || [ "${UPSTREAM_ID}" == ubuntu ]; }; then if [ "${2}" == --raw ]; then echo "${APP}" @@ -715,7 +724,7 @@ function update_repos() { # Faster by some margin if we are hitting github # Otherwise revert to old-style for a bespoke hosted repo - pushd ${ETC_DIR}/${REPO}.d >/dev/null + pushd "${ETC_DIR}/${REPO}.d" >/dev/null || return awk -F/ '/github/ {print "# fetching github repo"; print "GITREPO="$4"/"$5;\ @@ -726,9 +735,9 @@ function update_repos() { print "${ELEVATE} wget ${WGET_VERBOSITY} -N -B \"${REPO_URL}/packages/\" -i \"${ETC_DIR}/${REPO}.repo.tmp\" -P \"${ETC_DIR}/${REPO}.d\""; print "${ELEVATE} rm \"${ETC_DIR}/${REPO}.repo.tmp\"" } ' \ - <<<${REPO_URL} | bash - + <<<"${REPO_URL}" | bash - - popd >/dev/null + popd >/dev/null || return done refresh_supported_cache_lists & } @@ -755,9 +764,7 @@ function list_deprecated_apps() { function list_local_apps() { if [ -d "${ETC_DIR}/99-local.d" ]; then - for APP in $(find "${ETC_DIR}/99-local.d" -maxdepth 1 -type f -printf "%f\n"); do - echo "99-local/${APP}" - done + find "${ETC_DIR}/99-local.d" -maxdepth 1 -type f -printf "99-local/%f\n" fi } @@ -766,12 +773,14 @@ function print_etc_overrides() { local DEB_GET_SCRIPT_FILE="${0}" local NUM_OLDER_CONFLICTS=0 for APP in "${APP_CONFLICTS[@]}"; do - local FULL_APP="$( + local FULL_APP + FULL_APP="$( IFS=$'\n' echo "${APPS[*]}" | grep -m 1 "/${APP}$" )" fancy_message warn "Conflict detected, duplicate declaration of package ${APP}, using declaration from $(echo "${FULL_APP}" | cut -d / -f 1)" + # shellcheck disable=SC2076 if [[ " ${LOCAL_APPS[*]} " =~ " ${FULL_APP} " ]] && [ "${DEB_GET_SCRIPT_FILE}" -nt "${ETC_DIR}/99-local.d/${APP}" ]; then ((NUM_OLDER_CONFLICTS++)) fi @@ -1105,11 +1114,13 @@ function fix_old_apps() { } function fix_installed() { - local line="$(grep -m 1 "^${APP} " "${ETC_DIR}/installed")" - local OLD_DEFVER="$(echo "${line}" | cut -d " " -f 2)" - local OLD_METHOD="$(echo "${line}" | cut -d " " -f 3)" + local line OLD_DEFVER OLD_METHOD + line="$(grep -m 1 "^${APP} " "${ETC_DIR}/installed")" + OLD_DEFVER="$(echo "${line}" | cut -d " " -f 2)" + OLD_METHOD="$(echo "${line}" | cut -d " " -f 3)" if [ "${DEFVER}" != "${OLD_DEFVER}" ]; then remove_installed "${APP}" + # shellcheck disable=SC2076 if [[ " apt ppa " =~ " ${OLD_METHOD} " ]]; then remove_repo --remove-repo fi @@ -1200,7 +1211,7 @@ function add_apt_repo() { ${ELEVATE} gpg --yes --dearmor "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" ${ELEVATE} rm "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" elif [ -n "${GPG_KEY_ID}" ]; then - ${ELEVATE} gpg --no-default-keyring --keyring /usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg --keyserver keyserver.ubuntu.com --recv ${GPG_KEY_ID} + ${ELEVATE} gpg --no-default-keyring --keyring "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" --keyserver keyserver.ubuntu.com --recv "${GPG_KEY_ID}" else #GPG_KEY_URL ${ELEVATE} wget -q "${GPG_KEY_URL}" -O "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" fi @@ -1221,7 +1232,8 @@ function ppa_to_apt() { local -r PPA_PERSON="$(echo "${PPA_ADDRESS}" | cut -d / -f 1)" local -r PPA_ARCHIVE="$(echo "${PPA_ADDRESS}" | cut -d / -f 2)" export APT_REPO_URL="https://ppa.launchpadcontent.net/${PPA_ADDRESS}/ubuntu/ ${UPSTREAM_CODENAME} main" - export ASC_KEY_URL="https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x$(curl -s "https://api.launchpad.net/devel/~${PPA_PERSON}/+archive/ubuntu/${PPA_ARCHIVE}" | grep -o -E "\"signing_key_fingerprint\": \"[0-9A-F]+\"" | cut -d \" -f 4)" + ASC_KEY_URL="https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x$(curl -s "https://api.launchpad.net/devel/~${PPA_PERSON}/+archive/ubuntu/${PPA_ARCHIVE}" | grep -o -E "\"signing_key_fingerprint\": \"[0-9A-F]+\"" | cut -d \" -f 4)" + export ASC_KEY_URL export APT_LIST_NAME="${PPA_PERSON}-ubuntu-${PPA_ARCHIVE}-${UPSTREAM_CODENAME}" } @@ -1237,13 +1249,14 @@ function maint_supported_cache() { ;; reinstall | install) local PAD=' ' - local cache_line=$(printf "%s %s [ installed ]\n" "${APP}" "${PAD:${#APP}}") + local cache_line + cache_line=$(printf "%s %s [ installed ]\n" "${APP}" "${PAD:${#APP}}") # # First remove the bare entry - ${ELEVATE} sed -i -e '/^${APP}$/d' ${CACHE_DIR}/supported.list + ${ELEVATE} sed -i -e "/^${APP}\$/d" ${CACHE_DIR}/supported.list # Replace it with a flagged one cat ${CACHE_DIR}/supported.list - <<<"${cache_line}" | ${ELEVATE} sort -t " " -k 1 -u -o ${CACHE_DIR}/supported.list # should be there but safest to be sure - grep -q -w ${APP}$ ${CACHE_DIR}/supported_apps.list || + grep -q -w "${APP}\$" ${CACHE_DIR}/supported_apps.list || cat ${CACHE_DIR}/supported_apps.list - <<<"${APP}" | ${ELEVATE} sort -t " " -k 1 -u -o ${CACHE_DIR}/supported_apps.list ;; esac @@ -1284,14 +1297,19 @@ if ! command -v lsb_release 1>/dev/null; then fancy_message fatal "lsb_release not detected. Quitting." fi -export HOST_CPU="$(uname -m)" +HOST_CPU="$(uname -m)" +export HOST_CPU case ${HOST_CPU} in -aarch64 | armv7l | x86_64) export HOST_ARCH="$(dpkg --print-architecture)" ;; +aarch64 | armv7l | x86_64) + HOST_ARCH="$(dpkg --print-architecture)" + export HOST_ARCH + ;; *) fancy_message fatal "${HOST_CPU} is not supported. Quitting." ;; esac -readonly USER_AGENT="Mozilla/5.0 (X11; Linux ${HOST_CPU}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" -readonly USER_HOME="${HOME}" +# Unused variables? +#readonly USER_AGENT="Mozilla/5.0 (X11; Linux ${HOST_CPU}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" +#readonly USER_HOME="${HOME}" OS_ID=$(lsb_release --id --short) case "${OS_ID}" in @@ -1317,11 +1335,11 @@ else fancy_message fatal "os-release not found. Quitting" fi -UPSTREAM_ID="$(grep "^ID=" ${OS_RELEASE} | cut -d'=' -f2)" +UPSTREAM_ID="$(grep "^ID=" "${OS_RELEASE}" | cut -d'=' -f2)" # Fallback to ID_LIKE if ID was not 'ubuntu' or 'debian' if [ "${UPSTREAM_ID}" != ubuntu ] && [ "${UPSTREAM_ID}" != debian ]; then - UPSTREAM_ID_LIKE="$(grep "^ID_LIKE=" ${OS_RELEASE} | cut -d'=' -f2 | cut -d \" -f 2)" + UPSTREAM_ID_LIKE="$(grep "^ID_LIKE=" "${OS_RELEASE}" | cut -d'=' -f2 | cut -d \" -f 2)" if [[ " ${UPSTREAM_ID_LIKE} " =~ " ubuntu " ]]; then UPSTREAM_ID=ubuntu @@ -1332,14 +1350,14 @@ if [ "${UPSTREAM_ID}" != ubuntu ] && [ "${UPSTREAM_ID}" != debian ]; then fi fi -UPSTREAM_CODENAME=$(grep "^UBUNTU_CODENAME=" ${OS_RELEASE} | cut -d'=' -f2) +UPSTREAM_CODENAME=$(grep "^UBUNTU_CODENAME=" "${OS_RELEASE}" | cut -d'=' -f2) if [ -z "${UPSTREAM_CODENAME}" ]; then - UPSTREAM_CODENAME=$(grep "^DEBIAN_CODENAME=" ${OS_RELEASE} | cut -d'=' -f2) + UPSTREAM_CODENAME=$(grep "^DEBIAN_CODENAME=" "${OS_RELEASE}" | cut -d'=' -f2) fi if [ -z "${UPSTREAM_CODENAME}" ]; then - UPSTREAM_CODENAME=$(grep "^VERSION_CODENAME=" ${OS_RELEASE} | cut -d'=' -f2) + UPSTREAM_CODENAME=$(grep "^VERSION_CODENAME=" "${OS_RELEASE}" | cut -d'=' -f2) fi # Debian 12+ @@ -1347,6 +1365,7 @@ if [ -z "${UPSTREAM_CODENAME}" ] && [ -e /etc/debian_version ]; then UPSTREAM_CODENAME=$(cut -d / -f 1 /etc/debian_version) fi +# shellcheck disable=SC2034 case "${UPSTREAM_CODENAME}" in buster) UPSTREAM_RELEASE="10" ;; bullseye) UPSTREAM_RELEASE="11" ;; @@ -1370,22 +1389,32 @@ else exit 1 fi +APPS=() +INSTALLED_APPS=() +APP_CONFLICTS=() +LOCAL_APPS=() + case "${ACTION}" in update | upgrade | show | reinstall | install | remove | purge | search | list | pretty_list | prettylist | csv_list | csvlist | csv | fix-installed) - APPS="$(list_local_apps)" - APPS="${APPS} -$(list_repo_apps)" - APPS="${APPS} + tempapps="$(list_local_apps) +$(list_repo_apps) $(declare -F | grep deb_ | sed 's|declare -f deb_|00-builtin/|g')" - readonly APP_CONFLICTS=($(echo "${APPS}" | cut -d / -f 2 | sort | uniq --repeated)) - APPS="$(echo "${APPS}" | sort -t / -k 2 -u)" - readonly LOCAL_APPS=($(echo "${APPS}" | grep "^99-local/")) + echo "${tempapps[@]}" | cut -d / -f 2 | sort | uniq --repeated | + while IFS="" read -r line; do APP_CONFLICTS+=("$line"); done + readonly APP_CONFLICTS + + tempapps="$(echo "${tempapps[@]}" | sort -t / -k 2 -u)" + + echo "${tempapps[@]}" | grep "^99-local/" | + while IFS="" read -r line; do LOCAL_APPS+=("$line"); done + readonly LOCAL_APPS + if [ -e "${ETC_DIR}/installed" ]; then - INSTALLED_APPS=($(cut -d " " -f 1 "${ETC_DIR}/installed")) - else - INSTALLED_APPS=() + cut -d " " -f 1 "${ETC_DIR}/installed" | + while IFS="" read -r line; do INSTALLED_APPS+=("$line"); done fi - APPS=(${APPS}) + + APPS=("${tempapps[@]}") ;; esac @@ -1399,12 +1428,15 @@ install | reinstall | remove | purge | show) fi print_etc_overrides DEPRECATED_APPS="$(list_deprecated_apps | sort -t / -k 2 -u)" + DEPRECATED_INSTALLED=() + pkglist=$(echo "${DEPRECATED_APPS}" | cut -d / -f 2 | tr "\n" " ") if [ -n "${DEPRECATED_APPS}" ]; then - readonly DEPRECATED_INSTALLED=($(dpkg-query -f '${db:Status-abbrev}:${Package}\n' -W $(echo "${DEPRECATED_APPS}" | cut -d / -f 2 | tr "\n" " ") 2>/dev/null | grep "^ii " | cut -d : -f 2)) - else - readonly DEPRECATED_INSTALLED=() + dpkg-query -f '${db:Status-abbrev}:${Package}\n' -W "${pkglist[@]}" 2>/dev/null | + grep "^ii " | cut -d : -f 2 | + while IFS="" read -r line; do DEPRECATED_INSTALLED+=("$line"); done fi - DEPRECATED_APPS=(${DEPRECATED_APPS}) + readonly DEPRECATED_INSTALLED + DEPRECATED_APPS=("${DEPRECATED_APPS[*]}") print_deprecated ;; esac @@ -1458,6 +1490,7 @@ install | reinstall) fancy_message fatal "${APP} is not supported on ${HOST_ARCH}." fi + # shellcheck disable=SC2076 if [ -n "${CODENAMES_SUPPORTED}" ] && ! [[ "${CODENAMES_SUPPORTED[*]}" =~ "${UPSTREAM_CODENAME}" ]]; then fancy_message fatal "${APP} is not supported on ${OS_ID_PRETTY} ${UPSTREAM_CODENAME^}." fi @@ -1477,6 +1510,7 @@ list) list_opt_1="" list_opt_2="" while [ -n "${1}" ]; do + # shellcheck disable=SC2076 if [ "${1}" == --include-unsupported ]; then list_opt_1=--include-unsupported elif [[ " --raw --installed --not-installed --only-unsupported " =~ " ${1} " ]]; then @@ -1580,12 +1614,11 @@ update) update_repos "$@" #if [ "${1}" != --repos-only ]; then if [[ "$*" != *"--repos-only"* ]]; then - APPS="$(list_local_apps)" - APPS="${APPS} -$(list_repo_apps)" - APPS="${APPS} -$(declare -F | grep deb_ | sed 's|declare -f deb_|00-builtin/|g')" - APPS=($(echo "${APPS}" | sort -t / -k 2 -u)) + APPS=() + echo "$(list_local_apps) +$(list_repo_apps) +$(declare -F | grep deb_ | sed 's|declare -f deb_|00-builtin/|g')" | + sort -t / -k 2 -u | while IFS="" read -r line; do APPS+=("$line"); done for APP in "${INSTALLED_APPS[@]}"; do FULL_APP="$( IFS=$'\n' @@ -1598,7 +1631,9 @@ $(declare -F | grep deb_ | sed 's|declare -f deb_|00-builtin/|g')" remove_installed "${APP}" fi done - INSTALLED_APPS=($(cut -d " " -f 1 "${ETC_DIR}/installed")) + INSTALLED_APPS=() + cut -d " " -f 1 "${ETC_DIR}/installed" | + while IFS="" read -r line; do INSTALLED_APPS+=("$line"); done update_debs fi ;; @@ -1613,10 +1648,11 @@ fix-installed) fi elevate_privs if [ "${1}" = --old-apps ]; then - for APP in $(dpkg-query -f '${db:Status-abbrev}:${Package}\n' -W $( + pkglist=$( IFS=$'\n' echo "${APPS[*]}" | cut -d / -f 2 | tr "\n" " " - ) 2>/dev/null | grep "^ii " | cut -d : -f 2); do + ) + for APP in $(dpkg-query -f '${db:Status-abbrev}:${Package}\n' -W "${pkglist[@]}" 2>/dev/null | grep "^ii " | cut -d : -f 2); do validate_deb "$( IFS=$'\n' echo "${APPS[*]}" | grep -m 1 "/${APP}$"