-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB_Functions.sh
executable file
·128 lines (96 loc) · 2.91 KB
/
DB_Functions.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
#! /usr/bin/bash
source ./Table_menu.sh
Create_db() {
db="DB/$1"
if [ $# -ne 1 ]; then
echo "Invalid Input, User did't input anything!"
else
#Check if the dir & database already exit
if [[ $1 =~ ^[./] ]]; then
echo "Invalid Input, Directory Already exits"
elif [[ $1 == *\\* ]] ; then
echo -e "Error. Invalid Input, canncot contain backslash.\c"
#elif [[ $1 =~ " " ]]; then
#echo "Invalid Input, cannot contain spaces"
# Check if the length of the name is within limits (1-64 characters)
#elif [[ ${#db} < 1 || ${#db} > 64 ]]; then
elif [[ ${#1} -lt 1 || ${#1} -gt 64 ]]; then
echo "Invalid Input, Input length must be 1-64 charachter"
#check if the data base already exits
elif [ -d $db ]; then
echo " Database '$1' Already exits"
#check if the database name starts with or underscore(_)
elif [[ $1 =~ ^[a-zA-Z_].* ]] ; then
# Check if the name contains letter or (_) or numbers !!
if [[ $1 =~ [a-zA-Z0-9_]*$ ]]; then
# if true !! then create a data base
mkdir -p $db
echo "Database '$1' created!"
fi
else
echo "Invalid Input"
fi
fi
}
List_db() {
list=$(ls DB | wc -l)
# Check if the DIR EMPTY !
if [[ $list == 0 ]]; then
echo "Directory is Empty"
else
ls DB
fi
}
Connect_db() {
database="DB/$1"
#check dir exits
if [ -d $database ]; then
# if its true then connect to the database
cd $database
echo "Connected to database $1."
#call table_menu fun
table_menu
else
echo "Error: Database $1 does not exist."
echo "Suggestion, Do you want creat database $1 "
select choice in "YES" "NO"
do
case $choice in
"YES")
Create_db $1
break;;
"NO")
break;;
*)
echo "Error. Invalid choice"
esac
done
fi
}
Drop_db() {
db="DB/$1"
#check on number of arg sent
if [ $# -eq 1 ]; then
if [ -d $db ]; then
echo "Warning. Do you want to drop database $1."
# procdeure added to make the user rethink again
select choice in "YES" "NO"
do
case $choice in
"YES")
rm -r $db
echo "Database $1 Successfully dropped"
break;;
"NO")
break;;
*)
echo "Invalid Choice"
esac
done
else
echo "Error. Invalid Database"
fi
else
echo "Delete one database at a time"
fi
}