48 lines
1.7 KiB
Bash
Executable file
48 lines
1.7 KiB
Bash
Executable file
#!/bin/bash - not strictly necessary, but helps nano with syntax highlighting.
|
|
|
|
hash screen >/dev/null 2>&1 && {
|
|
[[ -e "$HOME/.screenrc-${HOSTNAME%%.*}" ]] && export SCREENRC="$HOME/.screenrc-${HOSTNAME%%.*}"
|
|
export SCREENDIR="$HOME/.screen-${HOSTNAME%%.*}"
|
|
if [[ -n "$SSH_TTY" ]]; then
|
|
if [[ -n "$STY" ]]; then
|
|
printf "%s\\n\\n" "Screen $STY, window $WINDOW."
|
|
else
|
|
SCREENS="$(screen -ls | command grep '[[:alpha:]]' | command grep -E -v '^([[:digit:]]+|No) Socket(s)?')"
|
|
if [[ -n "$SCREENS" ]]; then
|
|
printf "%s\\n\\n" "$SCREENS"
|
|
else
|
|
printf "%s\\n\\n" "No screens."
|
|
fi
|
|
unset SCREENS
|
|
fi
|
|
elif [[ -n "$STY" ]]; then
|
|
printf "%s\\n\\n" "Screen $STY, window $WINDOW."
|
|
else
|
|
TTY="$(tty | cut -d/ -f3-)"
|
|
SCREENS="$(screen -ls | command grep -F "${HOSTNAME%%.*}" | command 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.
|
|
# Disabled this because screens should be started on chuckie not the desktop/laptop.
|
|
# [[ "$TTY" == tty* ]] && screen
|
|
:
|
|
;;
|
|
1)
|
|
# Just one screen - reconnect if it's not dead.
|
|
if (( $(printf "%s" "$SCREENS" | command grep -c -F 'Dead') == 1 )); then
|
|
printf "%s:\\n" "Found dead screen for $TTY"
|
|
printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g'
|
|
else
|
|
screen -dr "${TTY//\//-}.${HOSTNAME%%.*}"
|
|
fi
|
|
;;
|
|
*)
|
|
# Multiple screens - output a list
|
|
printf "%s:\\n" "Multiple screens found for $TTY"
|
|
printf "%s\\n" "$SCREENS" | sed -e 's/^/ /g'
|
|
;;
|
|
esac
|
|
fi
|
|
unset SCREENS TTY
|
|
}
|