-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSQLscr_heroes.sh
98 lines (87 loc) · 2 KB
/
PSQLscr_heroes.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
#!/bin/bash
### Create databae in PostgreSQL
userdb="postgres"
passowrdb="123"
host="localhost"
dbname="heroes"
tab1="marvel"
tab2="skill"
while : ; do
read -n1 -p "Do u want to create database? [y/n] " x
if [[ "$x" =~ [yY] ]]; then
echo
break
elif [[ "$x" =~ [nN] ]]; then
echo
echo "As you wish..."
exit
else echo
fi
done
echo "drop database if exists $dbname;
create database $dbname;
\c $dbname
CREATE TABLE $tab2 (
skill INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name_skill VARCHAR (15)
);
INSERT INTO $tab2 (name_skill) VALUES ('power'), ('speed'), ('invisibility'), ('magic');
CREATE TABLE $tab1 (
id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR (30),
skill INT,
date TIMESTAMP,
FOREIGN KEY (skill) REFERENCES skill (skill)
);" > $userdb"tmp".sql
sudo -u $userdb psql -f $userdb"tmp".sql
: > $userdb"tmp".sql
while true ; do
echo
read -p "Name of hero? " n
while : ; do
echo "What skill 1-4?
1-power
2-speed
3-invisibility
4-magic"
read -n1 s
if [[ "$s" =~ [0-4] ]]; then
echo
break
else echo
fi
done
echo "INSERT INTO $tab1 (name, skill) VALUES ('$n', '$s');" >> $userdb"tmp".sql
x=0
while : ; do
read -n1 -p "Another one hero [y/n] " x
if [[ "$x" =~ [yY] ]]; then
echo
break
elif [[ "$x" =~ [nN] ]]; then
echo
echo "As you wish..."
break 2
else echo
fi
done
done
sudo -u $userdb psql -d $dbname -f $userdb"tmp".sql
x=0
echo
while : ; do
read -n1 -p "Would you read table? [y/n] " x
if [[ "$x" =~ [yY] ]]; then
echo
echo "SELECT m.id, m.name, s.name_skill FROM marvel AS m JOIN skill AS s ON m.skill=s.skill;" > $userdb"tmp".sql
echo
sudo -u $userdb psql -d $dbname -f $userdb"tmp".sql
break
elif [[ "$x" =~ [nN] ]]; then
echo
echo "As you wish..."
break
else echo
fi
done
rm $userdb"tmp".sql