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.
This commit is contained in:
Darren 'Tadgy' Austin 2023-09-29 13:40:00 +01:00
commit a18f4d7cbf

View file

@ -1,12 +1,14 @@
#!/bin/bash - not strictly necessary, but helps nano with syntax highlighting.
# Source bash specific set up, # Source bash specific set up,
[[ -f ~/.bashrc ]] && . ~/.bashrc [[ -f "$HOME/.bashrc" ]] && . "$HOME/.bashrc"
# Environment. # Environment.
export EDITOR="nano" export EDITOR="nano"
export LANG="en_GB.UTF-8" export LANG="en_GB.UTF-8"
export LC_COLLATE="POSIX" # 'C' causes issues with some applications 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 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 PAGER="less"
export PATH="/opt/sbin:/usr/local/sbin:/usr/sbin:/sbin:$PATH" export PATH="/opt/sbin:/usr/local/sbin:/usr/sbin:/sbin:$PATH"
export VISUAL="$EDITOR" export VISUAL="$EDITOR"
@ -15,23 +17,29 @@ hash lesspipe >/dev/null 2>&1 && eval "$(SHELL=/bin/sh lesspipe)"
# Platform specific set up. # Platform specific set up.
PLATFORM="$(uname -s)" PLATFORM="$(uname -s)"
if [[ "$PLATFORM" = "Linux" ]]; then if [[ "$PLATFORM" = "Linux" ]]; then
# This is a hack to work around a problem with elogind/pam not creating # shellcheck disable=SC2155
# a gnupg directory under /run/user/0 for the gpg-agent socket. export GPG_TTY="$(tty)"
(( $(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
export I_WANT_A_BROKEN_PS=1 export I_WANT_A_BROKEN_PS=1
export LYNX_CFG="$HOME/.lynx.cfg" export LYNX_CFG="$HOME/.lynx.cfg"
export LYNX_LSS="$HOME/.lynx.lss" export LYNX_LSS="$HOME/.lynx.lss"
export MANPAGER="less" export MANPAGER="less"
export MANPATH="$HOME/.local/share/man:$MANPATH" 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 elif [[ "$PLATFORM" = "Darwin" ]]; then
export LSCOLORS="ExGxdxdxCxDxDxbcacbeae" export LSCOLORS="ExGxdxdxCxDxDxbcacbeae"
export MANPAGER="less -Mis -PM'Page %dt$'" export MANPAGER="less -Mis -PM'Page %dt$'"
export MANPATH="/opt/local/share/man:$MANPATH" export MANPATH="/opt/local/share/man:$MANPATH"
export PATH="/opt/local/sbin:/opt/local/bin:$PATH" export PATH="/opt/local/sbin:/opt/local/bin:$PATH"
else else
echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2 printf "%s: %s\\n" "${BASH_SOURCE##*/}" "unsupported platform: $PLATFORM" >&2
fi fi
unset PLATFORM unset PLATFORM
@ -46,46 +54,44 @@ hash screen >/dev/null 2>&1 && {
export SCREENDIR="$HOME/.screen-${HOSTNAME%%.*}" export SCREENDIR="$HOME/.screen-${HOSTNAME%%.*}"
if [[ -n "$SSH_TTY" ]]; then if [[ -n "$SSH_TTY" ]]; then
if [[ -n "$STY" ]]; then if [[ -n "$STY" ]]; then
echo "Screen $STY, window $WINDOW." # shellcheck disable=SC2154
echo printf "%s\\n\\n" "Screen $STY, window $WINDOW."
else else
SCREENS="$(screen -ls | grep '[[:alpha:]]' | grep -E -v '^([[:digit:]]+|No) Socket(s)?')" SCREENS="$(screen -ls | grep '[[:alpha:]]' | grep -E -v '^([[:digit:]]+|No) Socket(s)?')"
if [[ -n "$SCREENS" ]]; then if [[ -n "$SCREENS" ]]; then
echo "$SCREENS" printf "%s\\n\\n" "$SCREENS"
echo
else else
echo "No screens." printf "%s\\n\\n" "No screens."
echo
fi fi
unset SCREENS unset SCREENS
fi fi
elif [[ -n "$STY" ]]; then elif [[ -n "$STY" ]]; then
echo "Screen $STY, window $WINDOW." # shellcheck disable=SC2154
echo printf "%s\\n\\n" "Screen $STY, window $WINDOW."
else else
TTY="$(tty | cut -d/ -f3-)" TTY="$(tty | cut -d/ -f3-)"
SCREENS="$(screen -list | grep -F "${HOSTNAME%%.*}" | grep -F "${TTY//\//-}")" SCREENS="$(screen -ls | grep -F "${HOSTNAME%%.*}" | grep -F "${TTY//\//-}")"
case "$(echo ${SCREENS:--n} | wc -l)" in # This has to be an echo, not printf.
case "$(echo "${SCREENS:--n}" | wc -l)" in
0) 0)
# No screens found - start a new instance, if on a tty. # No screens found - start a new instance if on a tty.
[[ "$TTY" == *tty* ]] && sleep 1 && screen [[ "$TTY" == tty* ]] && screen
;; ;;
1) 1)
# Just one screen - reconnect if it's not dead. # Just one screen - reconnect if it's not dead.
if (( $(echo "$SCREENS" | grep "Dead" | wc -l) == 1 )); then if (( $(printf "%s" "$SCREENS" | grep -c -F 'Dead') == 1 )); then
echo "Found dead screen for $TTY:" printf "%s:\\n" "Found dead screen for $TTY"
echo "$SCREENS" | sed -e 's/^/ /g' printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g'
else else
sleep 1
screen -dr "${TTY//\//-}.${HOSTNAME%%.*}" screen -dr "${TTY//\//-}.${HOSTNAME%%.*}"
fi fi
;; ;;
*) *)
# Multiple screens - output a list # Multiple screens - output a list
echo "Multiple screens found for $TTY:" printf "%s:\\n" "Multiple screens found for $TTY"
echo "$SCREENS" | sed -e 's/^/ /g' printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g'
;; ;;
esac esac
unset SCREENS
fi fi
unset SCREENS TTY
} }