Repository navigation
Expand file tree
/
Copy pathsysman
More file actions
209 lines (170 loc) · 3.72 KB
/
Copy pathsysman
File metadata and controls
209 lines (170 loc) · 3.72 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
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
#!/bin/bash
# No globbing
set -f
HISTFILE=~/.bash_history
# Absolute paths to executables - Specify the versions we want to use. We don't want to use whatever is in ${PATH} (as specified in the assignment)
ADVANCED=./advanced
BASIC=./basic
CAT=/usr/bin/cat
GREP=/usr/bin/grep
PRINTF=/usr/bin/printf
USERMAN=./userman
# Use the built in for echo, read and exit
ECHO="command echo"
EXIT="command exit"
READ="command read"
# Handle SIGINT interrupts
trap handler SIGINT
declare -i CHOICE
function handler() {
$PRINTF "\nThank you for using System Manager.\n"
$EXIT 3
}
function mainmenu() {
$CAT << EOF
Welcome to System Manager
-------------------------
1) basic menu options
2) advanced menu options
3) passwd file options
4) quit
EOF
}
function basicmenu() {
$CAT << EOF
Basic Tasks Menu
----------------
1) Show free memory
2) Show free disk space
3) Show disk quota
4) Show the groups I am a member of
5) Show the history of commands for bash
6) Quit this menu
EOF
}
function advancedmenu() {
$CAT << EOF
Advanced Tasks Menu
----------------
1) Print the number of CPUs available on the system
2) Print the maximum number of open files allowed per user
3) Backup all files that match a criterion
4) Quit this menu
EOF
}
function passwdmenu() {
$CAT << EOF
Passwd Tasks Menu
----------------
1) Print the userid of a specified user
2) Print the groupid of a specified user
3) Print the comment field for a specified user
4) Print the home directory of a specified user
5) Print the default shell of a specified user
6) Exit this menu
EOF
}
# Renders a menu and gets an integer choice (verified to be a valid choice)
# $1 is the menu to render, $2 is the prompt
function getchoice() {
local -i MENUOPTS
CHOICE=0
# Output the menu to a variable - find out how many options there are and only include lines starting with a single number and a )
MENUOPTS=$(( $($ECHO "$($1)" | $GREP '^[0-9])' -c) ))
#Continue until a valid entry has been made
while (( CHOICE <= 0 || CHOICE > MENUOPTS ));
do
# Display the menu
$1
# Display the prompt and get the next number
$PRINTF "%s" "$2"
$READ CHOICE
if (( CHOICE <= 0 || CHOICE > MENUOPTS )); then
$ECHO "Invalid choice. Please try again"
fi
done
}
CHOICE=0
while (( CHOICE != 4 ))
do
getchoice mainmenu "Please enter a choice :"
case ${CHOICE} in
1)
getchoice basicmenu "Please enter an option :"
case ${CHOICE} in
1)
$BASIC freemem
;;
2)
$BASIC diskfree
;;
3)
$BASIC quota
;;
4)
$BASIC groups
;;
5)
$BASIC history
;;
6)
continue
;;
esac
CHOICE=0
;;
2)
wildcard=""
destination=""
getchoice advancedmenu "Please enter your choice :"
case ${CHOICE} in
1)
$ADVANCED -p
;;
2) $ADVANCED -o
;;
3) $PRINTF "Enter the wildcard to find files that you would like to backup (e.g *.txt) >"
$READ wildcard
$PRINTF "Enter the destination directory to backup the files to >"
$READ destination
$ADVANCED -b "${wildcard}" "${destination}"
;;
4)
CHOICE=0
continue
;;
esac
CHOICE=0
;;
3)
username=""
getchoice passwdmenu "Please enter your selection :"
#If its quit this menu, then skip username related tasks
if [ ${CHOICE} = 6 ]; then
CHOICE=0
continue
fi
$PRINTF "Enter the username >"
$READ username
case ${CHOICE} in
1)
$USERMAN userid "${username}"
;;
2)
$USERMAN groupid "${username}"
;;
3)
$USERMAN comment "${username}"
;;
4)
$USERMAN home "${username}"
;;
5)
$USERMAN shell "${username}"
;;
esac
CHOICE=0
;;
esac
done
$PRINTF "\nThank you for using System Manager.\n"