From ae111baa2bd348d1fe8f759fe0f1970720814e00 Mon Sep 17 00:00:00 2001 From: David Tomaschik Date: Wed, 26 Aug 2026 16:56:15 -0700 Subject: [PATCH] Script fixes --- .githooks/githooks.sh | 4 ++-- bin/install_tool | 13 ++++++++++-- bin/linux/backup.sh | 15 ++++++++----- bin/linux/i3lock.sh | 4 ++++ bin/linux/switch_virt.sh | 4 +++- deb-to-kali.sh | 8 +++---- dotfiles/config/i3status/build_config.sh | 16 ++++++++------ install.sh | 27 ++++++++++++++++-------- 8 files changed, 62 insertions(+), 29 deletions(-) diff --git a/.githooks/githooks.sh b/.githooks/githooks.sh index 4fa238a..4154f90 100755 --- a/.githooks/githooks.sh +++ b/.githooks/githooks.sh @@ -57,9 +57,9 @@ SUB_HOOK_DIR="${HOOKS_DIR}/${CALLED_AS}.d" if [ -d "$SUB_HOOK_DIR" ]; then # Sort files naturally so 01- runs before 02- - for script in $(ls "$SUB_HOOK_DIR" | sort); do - FULL_PATH="$SUB_HOOK_DIR/$script" + for FULL_PATH in "$SUB_HOOK_DIR"/*; do [ -x "$FULL_PATH" ] || continue + script=$(basename "$FULL_PATH") # Replay stdin if we captured it, otherwise execute normally if [ -n "$STDIN_DATA" ]; then diff --git a/bin/install_tool b/bin/install_tool index 56cc4e1..1ccd8fd 100755 --- a/bin/install_tool +++ b/bin/install_tool @@ -125,8 +125,17 @@ function deb_only { function get_latest_github_release_url { local repo="$1" local glob="$2" - curl -s "https://api.github.com/repos/${repo}/releases/latest" | \ - jq -r --arg rx "${glob}" '.assets[] | select(.name | test($rx)) | .browser_download_url' + require jq "jq is required to parse GitHub releases." + local response + response=$(curl -s "https://api.github.com/repos/${repo}/releases/latest") + + if ! echo "${response}" | jq -e '.assets' >/dev/null 2>&1; then + local msg + msg=$(echo "${response}" | jq -r '.message // "Unknown error fetching latest release."') + die "GitHub API error for ${repo}: ${msg}" + fi + + echo "${response}" | jq -r --arg rx "${glob}" '.assets[] | select(.name | test($rx)) | .browser_download_url' } function require { diff --git a/bin/linux/backup.sh b/bin/linux/backup.sh index 468c533..ecdb857 100755 --- a/bin/linux/backup.sh +++ b/bin/linux/backup.sh @@ -8,13 +8,18 @@ if [ "$(uname)" != "Linux" ]; then exit 1 fi -DEFAULT=`echo /media/${USER}/[bB]ackup/${USER}/` +shopt -s nullglob +matches=( /media/"${USER}"/[bB]ackup/"${USER}"/ ) +shopt -u nullglob +if [ ${#matches[@]} -gt 0 ]; then + DEFAULT="${matches[0]}" +else + DEFAULT="/media/${USER}/Backup/${USER}/" +fi DEST="${1:-${DEFAULT}}" function verify_dest { - arr=($1) - items=${#arr[@]} - if [ $items -ne 1 ] ; then + if [ -z "$1" ] ; then echo "Bad count of backup destinations." > /dev/stderr exit 1 fi @@ -25,7 +30,7 @@ function verify_dest { echo -n "Destination $dir does not end in a /, " > /dev/stderr echo "this is probably not what you want!" > /dev/stderr echo "Press a key to continue, or CTRL-C to cancel." > /dev/stderr - read + read -r fi } diff --git a/bin/linux/i3lock.sh b/bin/linux/i3lock.sh index 06de9aa..bb8bbd7 100755 --- a/bin/linux/i3lock.sh +++ b/bin/linux/i3lock.sh @@ -1,6 +1,10 @@ #!/bin/sh LOCKTIME="${SCREENSAVER_MIN:-5}" LOCKER="i3lock -c 000000" +# Cleanup previously leaked background agents +pkill -x xss-lock 2>/dev/null || true +pkill -x xautolock 2>/dev/null || true + # intentionally want word splitting below # do not quote this /usr/bin/xss-lock -- ${LOCKER} & diff --git a/bin/linux/switch_virt.sh b/bin/linux/switch_virt.sh index 40368f2..680110f 100755 --- a/bin/linux/switch_virt.sh +++ b/bin/linux/switch_virt.sh @@ -1,11 +1,13 @@ #!/bin/bash +set -e + if [ $# -lt 1 ] ; then echo "Usage: $0 " >&2 exit 1 fi -if [ `whoami` != "root" ] ; then +if [ "$(id -u)" -ne 0 ] ; then if which sudo >/dev/null 2>&1 ; then sudo "$0" "$@" exit diff --git a/deb-to-kali.sh b/deb-to-kali.sh index a2758fa..05f54be 100755 --- a/deb-to-kali.sh +++ b/deb-to-kali.sh @@ -1,12 +1,12 @@ #!/bin/bash -if [ `whoami` != "root" ] ; then +if [ "$(id -u)" -ne 0 ] ; then echo "This must be run as root." >&2 exit 1 fi -BASEDIR=`dirname $0` -if ! test -f ${BASEDIR}/keys/gpg/kali-repo.key ; then +BASEDIR="$(dirname "$0")" +if ! test -f "${BASEDIR}/keys/gpg/kali-repo.key" ; then echo "Couldn't find key, are you in the right place?" >&2 exit 1 fi @@ -15,6 +15,6 @@ cat >/etc/apt/sources.list.d/kali.list </dev/null | awk '{print $2}') + if [ -z "$i3ver" ] || [ "$(bc <<< "$i3ver < 2.11" 2>/dev/null)" -eq 0 ] ; then cat <<-EOF status_unk = "? UNK" diff --git a/install.sh b/install.sh index 2f494d8..6cc453a 100755 --- a/install.sh +++ b/install.sh @@ -114,15 +114,17 @@ install_known_hosts() { if [[ -x "${merge_script}" ]]; then # Use the robust awk script for merging. verbose "Merging known_hosts with authoritative script..." - "${merge_script}" "${skel_hosts}" "${user_hosts}" > "$tmpf" + if "${merge_script}" "${skel_hosts}" "${user_hosts}" > "$tmpf"; then + cat "$tmpf" >| "${user_hosts}" + fi else # Fallback to the old, less robust method if the script is missing. verbose "Warning: ${merge_script} not found or not executable. Using simple sort." - cat "${skel_hosts}" "${user_hosts}" | sort -u > "$tmpf" + if cat "${skel_hosts}" "${user_hosts}" | sort -u > "$tmpf"; then + cat "$tmpf" >| "${user_hosts}" + fi fi - # Safely replace the original file. - cat "$tmpf" >| "${user_hosts}" - rm "$tmpf" + rm -f "$tmpf" else # User does not have a known_hosts file, just copy the new one. cp "${skel_hosts}" "${user_hosts}" @@ -139,8 +141,7 @@ install_keys() { } read_saved_prefs() { - # Can't use basedir here as we don't have it yet - local pref_file="$(dirname "$0")/.installed-prefs" + local pref_file="${SCRIPT_DIR}/.installed-prefs" if [[ -f "${pref_file}" ]] ; then verbose "Loading saved skel preferences from ${pref_file}" # source is a bashism @@ -209,9 +210,17 @@ install_starship() { local install_path="${tmpd}/install.sh" if have_command curl ; then - curl -sSL --show-error -o "${install_path}" https://starship.rs/install.sh + if ! curl -fsSL --show-error -o "${install_path}" https://starship.rs/install.sh; then + echo "Failed to download starship installer!" >&2 + rm -rf "${tmpd}" + return 1 + fi elif have_command wget ; then - wget -q -O "${install_path}" --https-only https://starship.rs/install.sh + if ! wget -q -O "${install_path}" --https-only https://starship.rs/install.sh; then + echo "Failed to download starship installer!" >&2 + rm -rf "${tmpd}" + return 1 + fi else echo "No curl or wget available!!" >&2 rm -rf "${tmpd}"