-
Notifications
You must be signed in to change notification settings - Fork 12
/
dsh.sh
104 lines (87 loc) · 2.46 KB
/
dsh.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
#!/bin/bash
main() {
Cyan=$'\e[0;36m';
Magenta=$'\e[0;35m';
Blue=$'\e[0;34m';
Yellow=$'\e[0;33m';
Green=$'\e[0;32m';
Red=$'\e[0;31m';
ColorOff=$'\e[0m';
clear
local char=
local input=
local -a history=( )
local -i histindex=0
trap ctrl_c INT
dir=$(pwd | sed "s|/home/$USER|~|")
while (true)
do
echo -n "${Yellow}<${Red}$(whoami)${Yellow}@${Green}$(</etc/hostname)${Yellow}>: ${dir}>${Red}# ${ColorOff}" ; while IFS= read -r -n 1 -s char
do
if [ "$char" == $'\x1b' ]; then
while IFS= read -r -n 2 -s rest
do
char+="$rest"
break
done
fi
if [ "$char" == $'\x1b[D' ]; then
pos=-1
elif [ "$char" == $'\x1b[C' ]; then
pos=1
elif [[ $char == $'\177' ]]; then
input="${input%?}"
syntax
elif [ "$char" == $'\x1b[A' ]; then
# Up
if [ $histindex -gt 0 ]; then
histindex+=-1
input=$(echo -ne "${history[$histindex]}")
echo -ne "\r\033[K${Yellow}<${Red}$(whoami)${Yellow}@${Green}$(</etc/hostname)${Yellow}>: ${dir}>${Red}# ${ColorOff}${history[$histindex]}"
fi
elif [ "$char" == $'\x1b[B' ]; then
# Down
if [ $histindex -lt $((${#history[@]} - 1)) ]; then
histindex+=1
input=$(echo -ne "${history[$histindex]}")
echo -ne "\r\033[K${Yellow}<${Red}$(whoami)${Yellow}@${Green}$(</etc/hostname)${Yellow}>: ${dir}>${Red}# ${ColorOff}${history[$histindex]}"
fi
elif [ -z "$char" ]; then
# Newline
echo
history+=( "$input" )
histindex=${#history[@]}
break
else
echo -n "$char"
input+="$char"
syntax
fi
done
if [ "$input" == "exit" ]; then
break
# elif (<<<$input grep "^cd") then
else
# alias command="$input"
$input ; dir=$(pwd | sed "s|/home/$USER|~|")
fi
input=
done
}
syntax() {
if ("$input" &> /dev/null) then
echo -ne "\r\033[K${Yellow}<${Red}$(whoami)${Yellow}@${Green}$(</etc/hostname)${Yellow}>: ${dir}>${Red}# ${Green}${input}${ColorOff}"
else
echo -ne "\r\033[K${Yellow}<${Red}$(whoami)${Yellow}@${Green}$(</etc/hostname)${Yellow}>: ${dir}>${Red}# ${Red}${input}${ColorOff}"
fi
}
ctrl_c() {
echo
echo "${Red} Exiting and cleaning up..."
sleep 0.5
unset input
rm /tmp/chroot_dir.var &> /dev/null
clear
reboot_system
}
main