forked from mo7amedsalah/DBEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify_table.sh
executable file
·144 lines (113 loc) · 2.42 KB
/
modify_table.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
#!/bin/bash
clear
new_arr=()
source "functions";
source $(pwd)/use_Database.sh;
function get_check_line
{
#must $ not have a space and ifs to separate files
names=$(awk 'NR>3' "$filename")
IFS=":" read -r -a all_arr <<< "$names"
echo "$names"
while true
do
echo "enter the row you want to edit"
read line
if [[ "$line" =~ ^[1-9]+$ ]]
then
line=$((line+3))
break
elif [[ -z $line ]]
then
echo "must not be empty"
elif [[ "$line" =~ ^[a-zA-Z]+$ ]]
then
echo "must be numeric "
else
echo "not valid"
fi
done
return $line
}
function get_data_of_table
{
get_check_line
line_ret=$?
edit=$(sed -n "$line_ret p" "$filename")
IFS=":" read -r -a edit_arr <<< "$edit"
len="${#edit_arr[@]}"
for ((j=0;j<len;j++))
do
while true
do
echo "your old value is ${edit_arr[$j]}"
echo "enter your new value : "
read value
if [[ -z $value ]]
then
value=${edit_arr[$j]}
break
fi
if [ ${col_types[$j]} = "number" ]
then
reg="^[0-9]+$"
if [[ "$value" =~ $reg ]]
then
check_pk "$value"
repeat="$?"
if [ "$repeat" -eq 1 ]
then
echo "repeated"
elif [ ! "$repeat" -eq 1 ]
then
echo "not repeated data"
new_arr+=("$value")
sed -i "$line s/${edit_arr[$j]}/$value/g" "$filename"
break
else
echo "not valid"
fi
fi
elif [ ${col_types[$j]} = "string" ]
then
reg="^[a-zA-Z]+$"
if [[ "$value" =~ $reg ]]
then
echo "you entered a valid value"
new_arr+=("$value")
sed -i "$line s/${edit_arr[$j]}/$value/g" "$filename"
break
elif [[ -z $value ]]
then
value=${edit_arr[$j]}
break
fi
else
echo "not valid, please enter a valid value"
fi
done
done
echo "${new_arr[@]}"
printf "\n"
echo "your record is edited "
}
while true
do
echo "enter your file:"
read filename
if [[ -f "$filename" ]]
then
get_structure_of_table
get_data_of_table
for i in "${col_names[@]}"; do
printf '%s' "$i |"
done
printf "\n"
get_All_Data
break
else
echo "your file does not exist"
fi
done
cd ..
source "DatabaseEngine.sh";