-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert
More file actions
67 lines (61 loc) · 2.14 KB
/
insert
File metadata and controls
67 lines (61 loc) · 2.14 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
#!/usr/bin/bash
cd $db_directory/$name
clear
while true; do
awk '{print NR,"==>",$0}' tables_name
read -p "choose number of table to insert into " table_num
length=$(awk 'END { print NR }' tables_name)
if [[ $table_num -le $length && $table_num -gt 0 ]]; then
table=$(sed -n "${table_num}p" tables_name)
data="data_"
meta="metadata_${table}"
table="${data}${table}"
first_row=$(head -n 1 "$table")
IFS=':' read -ra columns <<<"$first_row"
new_row=""
for key in "${!columns[@]}"; do
while true; do
read -p "'${columns[$key]}' " value
if [ $key -eq 0 ]; then
re='^[0-9]+$'
if ! [[ $value =~ $re && $value != "" ]]; then
echo "error: Not a number"
continue # Continue to the same loop (for key in "${!columns[@]}")
else
unique=$(grep "^$value:" "$table" | cut -d : -f 1)
if [ "$unique" == "$value" ]; then
echo "This ${columns[$key]} already exists"
continue # Continue to the same loop (for key in "${!columns[@]}")
else
value+=":"
new_row+=$value
break # Break the while loop if a valid number is entered
fi
fi
else
type=$(grep "${columns[$key]}" "$meta" | cut -d : -f 2)
if [[ "$type" == "integer" ]]; then
if ! [[ $value =~ $re ]]; then
echo "error: Not a number"
continue # Continue to the same loop (for key in "${!columns[@]}")
else
value+=":"
new_row+=$value
break # Break the while loop if a valid number is entered
fi
fi
value+=":"
new_row+=$value
break # Break the while loop for non-primary key columns
fi
done
done
length=$(echo "$new_row" | wc -c)
((last_element_index=length-1))
new_row_trimmed=${new_row::-1} # here we're eliminating the last :
echo "$new_row_trimmed" | tee -a "$table"
break
else
echo "Choose a valid table number"
fi
done