-
Notifications
You must be signed in to change notification settings - Fork 23
/
terminal
executable file
·122 lines (102 loc) · 1.97 KB
/
terminal
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
122
#!/bin/bash
ALIAS_FILE=~/.term_sharedAliases
main() {
case $1 in
start|list|connect|stop|setup|setupAliases)
$1 ${@:2}
;;
*)
help
;;
esac
}
start() {
name=$1
commands=${@:2}
previous=$(findByName $name)
if [ -z "$previous" ]; then
screen -dmS $name
if [ -z "$commands" ];then
screen -rx $name
else
screen -rx $name bash $commands
fi
else
connect $previous
fi
}
list() {
screen -ls
}
connect() {
screen -rx $1
}
stop() {
name=$(findByName $1)
if [ -z "$name" ]; then
echo No running session for \"$1\"
else
screen -X -S $name quit
fi
}
findByName() {
echo $(screen -ls | grep -m 1 -oEi "(\w+\.$1)")
}
setup() {
sudo apt-get -y install screen
}
setupAliases() {
createAliasFile
setupAliases_onFile ~/.bashrc
setupAliases_onFile ~/.zshrc
}
createAliasFile() {
echo Creating alias file for shared terminal \"$ALIAS_FILE\"
cat >$ALIAS_FILE<<TEXT
#if [ -z "\$STY" ] && [ "\$SSH_CONNECTION" != "" ]; then
# screen -x shared
## tmux attach-session -t ssh || tmux new-session -s ssh
# exit
#fi
function exit {
if [ -z "\$STY" ] ; then
builtin exit
else
echo This commands is disabled during terminal sharing.
echo If you want to quit and close all connections use
echo screen -x quit
echo If you just want to deatach use keyboard shotcut:
echo [ctrl] + [a] + [v]
fi
}
TEXT
}
setupAliases_onFile() {
echo Setting up import on \"$1\".
if [ -f $1 ]; then
cat >>$1<<TEXT
if [ -f $ALIAS_FILE ]; then
. ~/.term_sharedAliases
fi
TEXT
else
echo File \"$1\" not found.
fi
}
help() {
cat <<TEXT
Terminal sharing
Commands:
start <name> [commands [args]]
- start a new terminal with given name. This will allow others to connect to the same terminal.
list
- list shared terminals.
connect <name>
- connect o a terminal with given name.
stop <name>
- stop a running shared terminal.
setup
- install needed tools
TEXT
}
main $@