-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.sh
executable file
·50 lines (40 loc) · 1.52 KB
/
insert.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
#!/bin/bash
# script to insert data into chosen file
# takes 3 parameters $database $table and $columns
# columns must be a tuple separated by commas with no whitespace
# individual items in tuple must not contain commas or spaces
if [ ! "$#" -eq 3 ]; then # if number of parameters is incorrect
echo "Error: must pass 3 parameters"
exit 30
elif [ ! -d "$1" ] && [ ! -e "$1/$2" ]; then # if neither DB nor table exists
echo "Error: DB and table do not exist"
exit 32
elif [ ! -d "$1" ]; then # if DB does not exist
echo "Error: DB does not exist"
exit 34
elif [ ! -e "$1/$2" ]; then # if table does not exist
echo "Error: table does not exist"
exit 36
else
# target file checking complete
# now must check that number of fields to be inserted matches the number of columns in existing table
# find number of columns in table
file="$1"/"$2"
columns=$( head -n 1 "$file" ) # assign first line of file to variable columns
columns2="${columns//,/ }"
arrcolumns=($columns2) # create array from columns, replacing all instances of comma , with a space
numcols="${#arrcolumns[@]}" # number of elements of columns array assigned to variable numcols
# we then perform the same operation on the data to be inserted
data="$3"
data2="${data//,/ }"
arrfields=($data2)
numfields="${#arrfields[@]}"
if [ "$numcols" -eq "$numfields" ]; then
echo "OK: data inserted"
echo "$3">>"$file" # append target file with data in third parameter
exit 0
else
echo "Error: number of columns in tuple does not match schema"
exit 38
fi
fi