Linux - Script - Menu Template

  • menu always in the center of prompt window
  • pre-define methods
    • print line
    • make choice
    • see you
    • press to continue
1
2
3
printLine "+----------------------------------+"
printLine "| See you next time.. |"
printLine "+----------------------------------+"
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
#!/bin/ksh

#Head
option=0
errMsg=""
column=$(tput cols)
x=0
width=36
y=$((($column-$width)/2))
nextionAction=~/xxx.sh

printLine(){
((x++))
tput cup $x $y
echo $1
}

makeChoice(){
((x++))
tput cup $x $y
echo -n "Please choose (0-exit): "
read option
}

seeYou(){
((x++))
printLine "+----------------------------------+"
printLine "| See you next time.. |"
printLine "+----------------------------------+"
printLine ""
}

PressContinue(){
((x++))
tput cup $x $y
echo -n "Press to continue.. "
read q
}

#Body
clear

while :
do
#If error exists, display it
if [$errMsg!=""]; then
printLine "$errMsg"
fi

#show menu
printLine "+----------------------------------+"
printLine "| Menu |"
printLine "+----------------------------------+"
printLine "| |"
printLine "| 1. xxx |"
printLine "| |"
printLine "| 2. xxxxxx |"
printLine "| |"
printLine "+----------------------------------+"
printLine ""

makeChoice

#clear the error message
errMsg=""

#logic
case $option in
0) seeYou; exit ;;
1) $nextAction para1 ;;
2) $nextAction para2 ;;
*) errMsg="Invalid option. Try again.." ;;
esac

#set x back to 0 for next display
x=0

done