-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoscreen.sh
executable file
·121 lines (112 loc) · 2.66 KB
/
autoscreen.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# autoscreen
# auto start a GNU screen session without inception
#
# Copyright (C) 2013 Mara Kim
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
### USAGE ###
# source this file at the very END (important!)
# of your shell's .*rc file
# automatic screen start
_autoscreen() {
if [ "$1" = "-h" -o "$1" = "--help" ]
then
printf 'Usage: Source this script at the END of your .bashrc with:
source /path/to/autoscreen.sh [HOSTNAME] [STARTWAIT] [EXITWAIT]
Automatically start a GNU screen session without inception.
If HOSTNAME is not empty, check that $HOSTNAME = HOSTNAME before starting (wildcards allowed).
Waits STARTWAIT seconds before start (default 2).
Waits EXITWAIT seconds before exit (default 1).
Option GNU long option Meaning
-h --help Show this message
'
return 1
fi
if [ "$1" ]
then
local MYTEST="$1"
else
local MYTEST=""
fi
if [ "$2" ]
then
local STARTWAIT="$2"
else
local STARTWAIT=3
fi
if [ "$3" ]
then
local EXITWAIT="$3"
else
local EXITWAIT=2
fi
typeset -i i STARTWAIT
typeset -i i EXITWAIT
local READC
local RGOOD
local LENGTH
if [[ -z "$STY" && -z "$TMUX" && "$TERM" != "dumb" && (-z "$MYTEST" || "$HOSTNAME" = $MYTEST) ]]
then
printf "Starting screen. ^C to cancel... "
# countdown
for((i=STARTWAIT;i>0;i--))
do
printf '%b' $i
read -rsd '\0' -N 1 -t 1 READC && RGOOD=1
LENGTH=${#i}
for((j=0;j<LENGTH;j++))
do
printf ' '
done
if [ "$RGOOD" ]
then
break
fi
done
printf '0\n'
RGOOD=""
# start screen session
# clear screen
screen -D -RR && clear &&
printf "Screen terminated. Exiting. ^C to cancel... " &&
# countdown
for((i=EXITWAIT;i>0;i--))
do
printf '%b' $i
read -rsd '\0' -N 1 -t 1 READC && RGOOD=1
LENGTH=${#i}
for((j=0;j<LENGTH;j++))
do
printf ' '
done
if [ "$RGOOD" ]
then
break
fi
RGOOD=""
done &&
printf '0\n'
else
return 1
fi
# close shell or connection after successful execution
exit
}
# interactive screen start
autoscreen () {
screen -D -RR && clear &&
printf "Screen terminated.\n"
}
# start autoscreen on source
_autoscreen "$@"