-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteTable.sh
executable file
·81 lines (64 loc) · 2.04 KB
/
deleteTable.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
#!/bin/bash
check_permission() {
user=$(whoami)
databases=$(ls "Databases")
if [ -z "$databases" ]; then
echo -e "not found any database\n"
return 1
fi
# Filter databases where the user is the owner or an admin
filtered_databases=""
for database in $databases; do
owner=$(stat -c '%U' "Databases/$database")
admins_file="admins.txt"
if [ "$owner" = "$user" ] || grep -qw "$user" "$admins_file"; then
filtered_databases="$filtered_databases $database"
fi
done
if [ -z "$filtered_databases" ]; then
echo -e "you can't do any database because you are not the owner or admin.\n"
return 1
fi
echo -e "All databases in the system:\n"
echo "*************************************"
PS3="Enter the number of the database : "
select database_name in $filtered_databases; do
if [ -n "$database_name" ]; then
break
else
echo -e "Invalid selection. Please enter a number.\n"
fi
done
return 0
}
delete_table(){
if ! check_permission; then
return 1
fi
local tables=$(ls "Databases/$database_name")
if [ -z "$tables" ]; then
echo -e "No tables found in Database '$database_name'.\n"
return 1
fi
PS3="Enter the number of the table you want to delete: "
select table_name in $tables; do
if [ -n "$table_name" ]; then
break
else
echo -e "Invalid selection. Please enter a number.\n"
return 1
fi
done
rm "Databases/$database_name/$table_name"
if [ "$(stat -c '%U' "Databases/$database_name")" == "$user" ];then
who="owner"
elif grep -qw "$user" "$admins_file";then
who="admin"
fi
/bin/bash log.sh "delete Table" "$database_name" "$user" "$who"
echo "Table $table_name has been successfully deleted from Database $database_name."
return 0
}
delete_table
/bin/bash database.sh
exit 1