-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sh
242 lines (228 loc) · 6.47 KB
/
database.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
238
239
240
241
242
#!/bin/bash
#A shell script that lets the use create, view, and modify a database.
#Wesley Kwiecinski
#CSCI330 Section 1
#Assignment 6 - Automobile Database Shell Program
#Due 10/30/2020
cmd="" #current command
dbname="" #name of database to modify / view
cmdopts=() #Command options
#Creates a new database in the current directory with name dbname.
#Args: $1 - dbname, $2 - optional database label
new() {
if [ "$1" = "" ] #exit if database name is blank
then
echo "dbname is empty!"
return -1
fi
#Check if file exits
if [ -f "$1" ]
then
echo "$1 already exists!"
return -1
fi
touch $1 #create new file
#if label is empty, print untitled database to new file
if [ "$2" = "" ]
then
echo "Untitled databse" >> "$1"
else
echo $2 >> "$1" #otherwise print the command option
fi
echo "$1 created successfully."
return 0
}
#Adds a new record to an existing data file.
#Takes 5 parameters:
#$1 = database name to add to
#Args 2, 3, 5 must be greater than 0 chars
#$2 = Make of the car
#$3 = Model of the car
#$4 = Year of the car. Must be greater than 1870 and less than 2025
#$5 = Color of the car.
add() {
#check if database exists and is writable
if [ -w "$1" ]
then
#Check if the parameters are valid.
if [ -n "$2" ] && [ -n "$3" ] && [ -n "$5" ] && [ "$4" -gt 1870 ] && [ "$4" -lt 2025 ]; then
string="$2 $3 $4 $5"
sed -i '$ a '"$string"'' $1 #appends string to end of file.
else
echo "One of the parameters specified is invalid."
return -1
fi
else
echo "File is not writable or doesn't exist."
return -1
fi
chmod +rw $1 # change file permissions
echo "Record added to $1 successfully."
return 0
}
#Shows records in an existing database.
#Takes up to 4 arguments.
#$1 = database name
#$2 = how many records to show.
# all - shows all
# single - shows one record
# range - shows a range of records
#$3 = if $2 is range, lower bound. If $2 is single, show line at specified number.
#$4 = if $2 is range, upper bound.
show() {
#Check if database exists and is readable
if [ -r "$1" ]
then
if [ ! -s "$1" ]; then #Check if file is empty
echo "$1 is empty."
return 0
fi
max=$(wc -l < $1)
case $2 in
all) cat "$1" #Call cat then exit
return 0
;;
single) if [ "$3" -ge 1 ] && [ "$3" -le $max ] #check if value is greater than 0 and less than or equal line count
then
#call sed to print line
sed -n -e "$3 p" $1
return 0
else
#Output error message and exit function
echo "Not a valid line. (Did you use a number? The number may be outside the file range.)"
return -1
fi
;;
range) if [ "$3" -ge 1 ] && [ "$4" -ge 1 ] && [ "$3" -le $max ] && [ "$4" -le $max ] #Check if both values are greater than or equal to 0
then
sed -n -e "$3,$4 p" $1 #Print using range
return 0
else
echo "Invalid range. End range may be too high, or beginning is too low."
return -1 #Print error if range is invalid
fi
;;
*) echo "Use specifiers [ all, single, or range ]" #Print error message and return.
return -1
;;
esac
else
echo "File does not exist, or is not readable."
return -1
fi
}
#Deletes records from the specified database.
#Uses sed to delete records.
#Args: $1 = database name, must exist and be readable and writable.
# $2 = How many records to delete
# all - deletes every record except for the label.
# single - deletes a single record indicated by $3.
# range - deletes a range of addresses indicated by $3 and $4
# $3 = [single] - the line to delete, [range] - the beginning line to delete
# $4 = [range] - the last line to delete.
delete() {
#Check if file is readable and writable.
if [ -r "$1" ] && [ -w "$1" ]
then
if [ ! -s "$1" ]; then #Check if file is empty
echo "File is empty."
return -1
fi
max=$(wc -l < $1)
case $2 in
all) sed -i "1!d" $1 #Every line but first
echo "Deleted successfully."
return 0
;;
single) if [ "$3" -gt 0 ] && [ "$3" -le $max ]; then #Delete only if line is in the file
sed -i "$3d" $1 #delete a single line
echo "Line $3 deleted successfully."
return 0
else
echo "Could not find the specified line."
return -1
fi
;;
range) if [ "$3" -gt 0 ] && [ "$3" -le $max ] && [ "$4" -gt 0 ] && [ "$4" -le $max ]; then #Check if range is valid
sed -i "$3,$4d" $1 #Delete using a range
echo "Lines $3 to $4 deleted successfully."
return 0
else
echo "Range is invalid."
return -1
fi
;;
*) echo "Use specifiers [all, single, or range.]" #Display error if command is invalid
return -1
;;
esac
else
echo "File is not readable or writable, or it does not exist."
fi
return 0
}
#Counts and prints the number of rows in a database.
#Uses wc to get line totals.
#Args: $1 = database name
#Return: number of lines.
count() {
if [ -r "$1" ] #Check if file exists and can be read or if filename is empty
then
count=$(wc -l < $1) #store result of wc into variable
echo $count #print line total
return 0
else
echo "Missing databse! Does the database exist? Is it readable?"
return -1 #exit if file can't be found
fi
}
#Asks for the player to input a command if no args. Will continue after a command is completed
menu() {
#Loops and waits for user input. Calls a command based on the input.
while true; do
read -p "Enter a command. Type 'db help' for help. " dbname cmd cmdopts[0] cmdopts[1] cmdopts[2] cmdopts[3] cmdopts[4]
case $cmd in
new) new $dbname ${cmdopts[0]}
;;
add) add $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]} ${cmdopts[3]}
;;
show) show $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]}
;;
delete) delete $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]}
;;
count) count $dbname
;;
help) echo "Format: dbname [new, add, show, delete, count] args"
;;
*) return 0 #returns on invalid command
;;
esac
done
}
#$1 is always the database name
#calls a function based on the command
if [ "$#" -ge 1 ] #if there are command arguments...
then
#Store every command option into the array
dbname=$1
cmdopts[0]=$3
cmdopts[1]=$4
cmdopts[2]=$5
cmdopts[3]=$6
case $2 in #Call a function based on $2
new) new $dbname ${cmdopts[0]}
;;
add) add $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]} ${cmdopts[3]}
;;
show) show $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]}
;;
delete) delete $dbname ${cmdopts[0]} ${cmdopts[1]} ${cmdopts[2]}
;;
count) count $dbname
;;
*) echo "That is not a command!"
;;
esac
else
menu #Enter interactive mode
fi