diff --git a/.bashrc b/.bashrc index ac3a573..669afe9 100644 --- a/.bashrc +++ b/.bashrc @@ -1,11 +1,13 @@ # Functions. _agent_prompt_command() { hash ssh-add >/dev/null 2>&1 && { - local SOCK + local OUTPUT SOCK declare -g _SSH_PROMPT_ERROR_ISSUED # Check the ssh agent socket is still alive. - ssh-add -l >/dev/null 2>&1 - if (($? >= 2)); then + # Need to work around an ssh-add bug here: ssh-add doesn't always return 2 on failure + # to contact the agent, so also check if any errors were produced. + OUTPUT=$(ssh-add -l >/dev/null) + if ((${PIPESTATUS[0]} >= 2)) || [[ ! -z "$OUTPUT" ]]; then # Auth socket has become unusable, search for a new one. SOCK="$(_find_agent_sock)" if ((${PIPESTATUS[0]} == 0)); then @@ -30,23 +32,23 @@ _agent_prompt_command() { else _SSH_PROMPT_ERROR_ISSUED=0 fi - unset SOCK +# unset SOCK } } _clean_agent_socks() { - local I SSH_AUTH_SOCK + local I OUTPUT SSH_AUTH_SOCK # Go through the array of sockets and validate each one. for ((I = 0; I < ${#SOCKS[@]}; I++)); do - SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l >/dev/null 2>&1 - (($? >= 2)) && { + OUTPUT="$(SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l >/dev/null)" + ((${PIPESTATUS[0]} >= 2)) || [[ ! -z "$OUTPUT" ]] && { unset SOCKS[$I] } done } _find_agent_sock() { - local I IFS=$'\n' REPLY SOCKS=() SSH_AUTH_SOCK="$SSH_AUTH_SOCK" + local I IFS=$'\n' OUTPUT REPLY SOCKS=() SSH_AUTH_SOCK="$SSH_AUTH_SOCK" # Load the array of sockets, minus the one we know is unusable. if ((${BASH_VERSINFO[0]} >= 4)); then mapfile -t SOCKS < <(egrep -v "^$SSH_AUTH_SOCK\$" ~/.ssh/agents 2>/dev/null) @@ -57,8 +59,8 @@ _find_agent_sock() { fi # Search backwards through the list to find an active socket. for ((I = (${#SOCKS[@]} - 1); I >= 0; I--)); do - SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l >/dev/null 2>&1 - (($? <= 1)) && [[ ! -z "${SOCKS[$I]}" ]] && { + OUTPUT="$(SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l >/dev/null)" + { ((${PIPESTATUS[0]} <= 1)) && [[ -z "$OUTPUT" ]]; } && [[ ! -z "${SOCKS[$I]}" ]] && { printf "%s" "${SOCKS[$I]}" return 0 }