From a18f4d7cbfa91a373d896b24c2cfd64732db9943 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Fri, 29 Sep 2023 13:40:00 +0100 Subject: [PATCH] Various updates to .bash_profile, see full log. Add a #! line, even though it's not required. Use $HOME instead of ~. Quote LESS prompt. Rework GPG agent handling code to be more generic. Use printf rather than echo. A couple of code cleanups. Make things shellcheck safe and passed. --- .bash_profile | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/.bash_profile b/.bash_profile index 3f5614e..bda10c9 100644 --- a/.bash_profile +++ b/.bash_profile @@ -1,12 +1,14 @@ +#!/bin/bash - not strictly necessary, but helps nano with syntax highlighting. + # Source bash specific set up, -[[ -f ~/.bashrc ]] && . ~/.bashrc +[[ -f "$HOME/.bashrc" ]] && . "$HOME/.bashrc" # Environment. export EDITOR="nano" export LANG="en_GB.UTF-8" export LC_COLLATE="POSIX" # 'C' causes issues with some applications # export LC_CTYPE="POSIX" # Not sure why I set this in the first place... -export LESS="-RM -j.5 -i -PM?f%F:stdin. -- Page %dt of %D -- %lt/%L (%Pt\%)$" +export LESS="-RM -j.5 -i -PM'?f%F:stdin. -- Page %dt of %D -- %lt/%L (%Pt\%)$'" export PAGER="less" export PATH="/opt/sbin:/usr/local/sbin:/usr/sbin:/sbin:$PATH" export VISUAL="$EDITOR" @@ -15,23 +17,29 @@ hash lesspipe >/dev/null 2>&1 && eval "$(SHELL=/bin/sh lesspipe)" # Platform specific set up. PLATFORM="$(uname -s)" if [[ "$PLATFORM" = "Linux" ]]; then - # This is a hack to work around a problem with elogind/pam not creating - # a gnupg directory under /run/user/0 for the gpg-agent socket. - (( $(id -u) == 0 )) && [[ -e "$HOME/.gnupg/S.gpg-agent" ]] && mkdir -m 700 -p /run/user/0/gnupg && ln -sf "$HOME/.gnupg/S.gpg-agent" /run/user/0/gnupg - export GPG_TTY="$(tty)" && gpg-connect-agent updatestartuptty /bye >/dev/null 2>&1 + # shellcheck disable=SC2155 + export GPG_TTY="$(tty)" export I_WANT_A_BROKEN_PS=1 export LYNX_CFG="$HOME/.lynx.cfg" export LYNX_LSS="$HOME/.lynx.lss" export MANPAGER="less" export MANPATH="$HOME/.local/share/man:$MANPATH" - hash dircolors >/dev/null 2>&1 && eval "$(dircolors -b ~/.dir_colors)" + hash dircolors >/dev/null 2>&1 && eval "$(dircolors -b "$HOME/.dir_colors")" + hash gpgconf >/dev/null 2>&1 && { + GPG_SOCK_DIR="$(gpgconf --list-dirs | awk -F : '/socketdir:/ { print $2 }')" + # shellcheck disable=SC2174 + [[ ! -e "$GPG_SOCK_DIR" ]] && mkdir -m 700 -p "$GPG_SOCK_DIR" + [[ ! -e "$GPG_SOCK_DIR/S.gpg-agent" ]] && [[ -e "$HOME/.gnupg/S.gpg-agent" ]] && ln -sf "$HOME/.gnupg/S.gpg-agent" "$GPG_SOCK_DIR"/ + unset GPG_SOCK_DIR + } + hash gpg-connect-agent >/dev/null 2>&1 && gpg-connect-agent updatestartuptty /bye >/dev/null 2>&1 elif [[ "$PLATFORM" = "Darwin" ]]; then export LSCOLORS="ExGxdxdxCxDxDxbcacbeae" export MANPAGER="less -Mis -PM'Page %dt$'" export MANPATH="/opt/local/share/man:$MANPATH" export PATH="/opt/local/sbin:/opt/local/bin:$PATH" else - echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2 + printf "%s: %s\\n" "${BASH_SOURCE##*/}" "unsupported platform: $PLATFORM" >&2 fi unset PLATFORM @@ -46,46 +54,44 @@ hash screen >/dev/null 2>&1 && { export SCREENDIR="$HOME/.screen-${HOSTNAME%%.*}" if [[ -n "$SSH_TTY" ]]; then if [[ -n "$STY" ]]; then - echo "Screen $STY, window $WINDOW." - echo + # shellcheck disable=SC2154 + printf "%s\\n\\n" "Screen $STY, window $WINDOW." else SCREENS="$(screen -ls | grep '[[:alpha:]]' | grep -E -v '^([[:digit:]]+|No) Socket(s)?')" if [[ -n "$SCREENS" ]]; then - echo "$SCREENS" - echo + printf "%s\\n\\n" "$SCREENS" else - echo "No screens." - echo + printf "%s\\n\\n" "No screens." fi unset SCREENS fi elif [[ -n "$STY" ]]; then - echo "Screen $STY, window $WINDOW." - echo + # shellcheck disable=SC2154 + printf "%s\\n\\n" "Screen $STY, window $WINDOW." else TTY="$(tty | cut -d/ -f3-)" - SCREENS="$(screen -list | grep -F "${HOSTNAME%%.*}" | grep -F "${TTY//\//-}")" - case "$(echo ${SCREENS:--n} | wc -l)" in + SCREENS="$(screen -ls | grep -F "${HOSTNAME%%.*}" | grep -F "${TTY//\//-}")" + # This has to be an echo, not printf. + case "$(echo "${SCREENS:--n}" | wc -l)" in 0) - # No screens found - start a new instance, if on a tty. - [[ "$TTY" == *tty* ]] && sleep 1 && screen + # No screens found - start a new instance if on a tty. + [[ "$TTY" == tty* ]] && screen ;; 1) # Just one screen - reconnect if it's not dead. - if (( $(echo "$SCREENS" | grep "Dead" | wc -l) == 1 )); then - echo "Found dead screen for $TTY:" - echo "$SCREENS" | sed -e 's/^/ /g' + if (( $(printf "%s" "$SCREENS" | grep -c -F 'Dead') == 1 )); then + printf "%s:\\n" "Found dead screen for $TTY" + printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g' else - sleep 1 screen -dr "${TTY//\//-}.${HOSTNAME%%.*}" fi ;; *) # Multiple screens - output a list - echo "Multiple screens found for $TTY:" - echo "$SCREENS" | sed -e 's/^/ /g' + printf "%s:\\n" "Multiple screens found for $TTY" + printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g' ;; esac - unset SCREENS fi + unset SCREENS TTY }