-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.sh
More file actions
129 lines (113 loc) · 2.18 KB
/
menu.sh
File metadata and controls
129 lines (113 loc) · 2.18 KB
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
#! /bin/sh
#------------------------------------------------------
# Owner : Satinder Singh
#
# Description:
# This bash shell script contains functions to create menus.
# A menu option for exit is added automatically.
#------------------------------------------------------
#
# Usage:
# Source this file in your script and then use following
# functions to create and run menus.
#
# Example:
# - - - - - - - - -
#! /bin/bash
#
# source this file
#. ./menu.sh
#
#menu action function
#function addGrpDb
#{
# echo "Code menu handler here"
#}
#
# set menu heading
#menuTitle "News Manager"
#
# add menus
#addMenu "Add Group to db" addGrpDb
#addMenu "Delete Group from db" delGrpDb
#
# Start the main loop
#start
# - - - - - - - - -
#
# menuTitle <title> : Sets Heading of the menu
# addMenu <menu string> <menu action function name>
# if string and action are not given then default is added.
# start : Starts the infinite loop: showMenu, promptInput,run actions
#------------------------------------------------------
title="MENU"
totalItems=-1;
#menuTitle <title> : Sets Heading of the menu
function menuTitle
{
title=$1
}
function defaultAct
{
echo "Dummy action"
}
#addMenu <menu string> <menu action function name>
# if string and action are not given then default is created
# for them
function addMenu
{
n=$totalItems
n=$(( n + 1 ))
tlabel=$1
label=${tlabel:=menu$n}
tact=$2
act=${tact:=defaultAct}
labels[$n]=$label
actions[$n]=$act
echo "adding item $n . $label ${actions[$n]}"
totalItems=$n;
}
function showMenu
{
clear
echo "============================"
echo $title
echo "============================"
n=${totalItems}
while test $n -ge 0
do
echo "$n. ${labels[$n]}"
n=$(( n - 1 ))
done
}
function promptInput
{
n=${totalItems}
echo -n "Please Select Menu [0 - $n]:"
read i
return $i
}
function runAction
{
#totalItems=$(( totalItems - 1 ))
while true
do
showMenu
promptInput
i=$?
${actions[$i]}
echo -n "Press Enter to continue..."
read
done
}
#start : Starts the infinite loop: showMenu, promptInput,run actions
function start
{
runAction
}
function refresh
{
showMenu
}
#trap refresh SIGWINCH
addMenu Exit exit