forked from Botspot/wor-flasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal-run
executable file
·92 lines (65 loc) · 2.32 KB
/
terminal-run
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
#!/bin/bash
#$1 is the command to be run.
#$2 is the title.
commands="$1"
title="$2"
#set DEBUG variable to 1 to display which terminal is being used
[ -z "$DEBUG" ] && DEBUG=0
#add a line to the terminal's command-to-run that saves the terminal's PID to a known filename
temp_pid_file="$(mktemp -u)"
commands="echo "\$""\$" > $temp_pid_file
$commands"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
if [ -f /usr/bin/lxterminal ];then
lxterminal --title="$title" -e bash -c "$commands" &
[ $DEBUG == 1 ] && echo lxterminal
elif [ -f /usr/bin/xfce4-terminal ];then
xfce4-terminal --title="$title" -x bash -c "$commands" &
[ $DEBUG == 1 ] && echo xfce4-terminal
elif [ -f /usr/bin/mate-terminal ];then
mate-terminal --title="$title" -x bash -c "$commands" &
[ $DEBUG == 1 ] && echo mate-terminal
elif [ -f /usr/bin/xterm ];then
xterm -T "$title" -e bash -c "$commands" &
[ $DEBUG == 1 ] && echo xterm
elif [ -f /usr/bin/konsole ];then
konsole -p tabtitle="$title" -e bash <(echo "$commands") &
[ $DEBUG == 1 ] && echo konsole
elif [ -f /usr/bin/terminator ];then
terminator -T "$title" -x bash -c "$commands" &
[ $DEBUG == 1 ] && echo terminator
elif [ -f /usr/bin/gnome-terminal ];then
gnome-terminal --title "$title" -x bash -c "$commands" &
[ $DEBUG == 1 ] && echo gnome-terminal
elif [ -f /usr/bin/qterminal ];then
qterminal -e bash <(echo "$commands") &
[ $DEBUG == 1 ] && echo qterminal
elif [ -f /usr/bin/x-terminal-emulator ];then
$(readlink -f /usr/bin/x-terminal-emulator) -e bash -c "$commands" &
[ $DEBUG == 1 ] && echo x-terminal-emulator
else
echo "Failed to locate any terminal emulators!!!"
exit 1
fi
#A terminal should now be launching and in a few seconds the pid file should appear
if [ ! -z "$temp_pid_file" ];then
#echo "Waitng for terminal to close..."
#wait until pid file exists and give up in 5 seconds
attempts=0
while [ ! -f "$temp_pid_file" ];do
if [ "$attempts" -gt 5 ];then
error "terminal-run: No terminal detected as it never created the pid file within 5 seconds."
fi
sleep 1
attempts=$((attempts + 1))
done #pid file now exists
terminalrun_pid="$(cat "$temp_pid_file")"
#now, wait until the pid stops (terminal closes)
while [ -d /proc/${terminalrun_pid} ];do
sleep 1
done
rm -f "$temp_pid_file"
fi