-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmenu.sh
237 lines (208 loc) · 5.1 KB
/
bmenu.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
!/bin/bash
####################################
# Script : bmenu.sh
# Author : PlanetSizeCpu
# Date : 03-03-1992
# Use : Menus manager shell
# Remarks: It uses "tput" command for screen controls
####################################
# Environment settings
####################################
# Ending program settings
end()
{
echo $CLS
echo $NOR'\n MENU: Menu management in shell, by @planetsizecpu 1992'
echo
exit
}
# Argument defaults
ARG1=$1
if (test -n "$ARG1" )
then MENU0=$ARG1.m
else MENU0="menu.m"
fi
# Decode terminal screen sequences
getterm()
{
CLS=`tput clear`
HOM=`tput home`
NOR=`tput sgr0`
REV=`tput rev`
BLI=`tput blink`
BOL=`tput bold`
}
# Traps
trap "tput sgr0 ; echo '\n MENU Interrupted, press (CTRL+D) to exit shell.' ; exit" 2
DATE=`date +%a%t%d%t%m%t%y`
####################################
# Functions definition
####################################
# Read and show options for current menu
getmenu()
{
if [ -z "$MENU0" ]
then end
fi
if [ ! -r "$MENU0" ]
then echo 'Non existent menu !' ; exit 2
else
# Paint menu title
TIT=`awk ' NR==1 {print " "$0" " ; exit } ' $MENU0`
echo $CLS$REV" "$DATE$NOR
echo $HOM$REV$TIT$NOR
# Paint exit option if apply
SAL=`awk 'BEGIN {FS=","} NR==2 {print $1 ; exit }' $MENU0`
case $SAL in
"0") : ;;
*) echo ' 0 - Exit ' ;;
esac
# Paint menu options
awk 'BEGIN {FS=","} /[1-9]?,/ {printf "%3d %3s %-49s\n",$1," - ",$2 }' $MENU0
echo $REV" "$NOR
fi
# Look for biggest option number
maxop=`awk 'BEGIN {FS=","} /[1-9]?,/ {maxop=$1} END{print maxop+1}' $MENU0`
}
# Ask for Enter Key
askpause()
{
echo
echo -n 'Press (ENTER) to continue . . .'
read pause
}
# Ask for arguments
askargum()
{
echo
echo -n 'Enter command arguments: '
read commandA
}
# Ask end evaluate menu option
askmenu()
{
echo
echo -n $REV' SELECT OPTION: '
read option
echo $NOR
##################################################################
# Test if there is an option
if [ -z "$option" ]
then getmenu ; askmenu
fi
##############################################################
# Special case: ksh (AIX korn shell)
if [ $option = "ksh" ]
then
echo
echo "KORN COMMAND LINE"
echo
uname -a
echo
date
echo
who am i
echo
ksh
fi
##############################################################
# Special case: bash (linux shell)
if [ $option = "bash" ]
then
echo
echo "BASH COMAND LINE"
echo
uname -a
echo
date
echo
who am i
echo
bash
fi
##################################################################
# Test for zero option to go back or exit
if [ $option = "0" -o $option = "00" ]
then
if [ -n $MENU1 ]
then
MENU0=$MENU1;MENU1=$MENU2;MENU2=$MENU3;MENU3=$MENU4;
MENU4=$MENU5;MENU5=$MENU6;MENU6=$MENU7;MENU7=$MENU8;
MENU8="" ; getmenu ; askmenu
else
exit 0
fi
fi
##################################################################
# Test if option exists in menu
result=`grep -c $option, $MENU0`
if [ $result -lt 1 ]
then
getmenu
askmenu
fi
##################################################################
# Test for option number in range
if [ $option -gt 0 -a $option -lt $maxop ]
then
# Search command line
comline=`awk 'BEGIN{FS=","} $1==ARGV[2] {print FNR+1;exit}' $MENU0 $option`
# Read command
commandS=`awk ' NR==ARGV[2] {print $1;exit}' $MENU0 $comline`
# Read argument
commandA=`awk ' NR==ARGV[2] {print $2;exit}' $MENU0 $comline`
# Read whole command line
commandL=`awk ' NR==ARGV[2] {print $0;exit}' $MENU0 $comline`
else
getmenu
askmenu
fi
}
####################################
# Main procedures
####################################
getterm
while true
do
getmenu
askmenu
##############################################################
# Execute option
executed="0"
##############################################################
# Sspecial case: pause
if [ $commandS = "pause" ]
then
askpause ; executed="1"
fi
##############################################################
# Special case: menu MENU (go down submenu MENU) (max 8 levels)
if [ $commandS = "menu" ]
then
MENU8=$MENU7;MENU7=$MENU6;MENU6=$MENU5;MENU5=$MENU4;
MENU4=$MENU3;MENU3=$MENU2;MENU2=$MENU1;MENU1=$MENU0;
MENU0=$commandA".m" ; executed="1"
fi
##############################################################
# Special case: % (ask command % arguments)
if [ $commandS = "%" ]
then
askargum ; commandL=$commandA
fi
##############################################################
# Special case: comando % (ask arguments for command)
if [ -n "$commandA" ]
then
if [ $commandA = "%" ]
then
askargum ; $commandS $commandA ; executed="1"
fi
fi
##############################################################
# If not any previous then execute command line
if [ $executed = "0" ]
then
ksh -c "$commandL"
fi
done
exit