forked from KareeemBeltagy/Bash-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_row2
49 lines (45 loc) · 1.69 KB
/
insert_row2
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
#!/bin/usr/env bash
currentuser=$(whoami) #get the currnet user name
if [[ $currentuser == $(grep -w "$currentuser" DB_admins.db) ]]
then
echo "Select one of the following DataBases"
ls DataBases #list avaliable databases
read -r db_check #store name of database to delete
owner=$(cat ./DataBases/"$db_check"/owner.txt | awk '{print $1}') #get DB owner
#check the owner and current user
if [[ "$currentuser" == "$owner" ]]
then
echo "select one of the following table to insert data"
ls ./DataBases/"$db_check"
read -r tname
head -1 ./DataBases/"$db_check"/"$tname".table
NF=$(grep "%" ./DataBases/"$db_check"/"$tname".table | awk -F, '{print NF}')
echo "enter number of records to insert"
read -r records
echo "enter data separetly with the same order"
for (( r=1; r<=records; r++))
do
for (( c=1; c<=NF; c++ ))
do
read -r col
value_exists=$(awk -F',' -v col="$col" '{if ($1 == col) print "true"}' "./DataBases/$db_check/$tname.table")
while [[ $value_exists == "true" && $c == 1 ]]
do
echo "repeated value, Firsrt column must be unique Enter new value"
read -r col
value_exists=$(awk -F',' -v col="$col" '{if ($1 == col) print "true"}' "./DataBases/$db_check/$tname.table")
done
echo -n "$col" >> ./DataBases/"$db_check"/"$tname".table
if [[ $c -lt $NF ]] #typing comma after each column name but the last one
then
echo -n , >> ./DataBases/"$db_check"/"$tname".table
fi
done #end of loop of inner loop
echo "" >> ./DataBases/"$db_check"/"$tname".table
done #end of outter loop
else
echo "Only Databse owner can cretae table"
fi
else
echo "Only admin user can run this program"
fi