-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.sh
42 lines (32 loc) · 1.63 KB
/
element.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
#!/bin/bash
# create PSQL variable to query database
PSQL="psql -x --username=freecodecamp --dbname=periodic_table --tuples-only -c"
# if no argument:
if [[ -z $1 ]]
then
echo Please provide an element as an argument.
#if there is an argument:
else
# if it is not a number
if [[ ! $1 =~ ^[0-9]+$ ]]
then
# search element by name or symbol
GET_ELEMENT_SELECTED=$($PSQL "SELECT atomic_number, name, symbol, type, atomic_mass, melting_point_celsius, boiling_point_celsius FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE symbol ILIKE '$1' OR name ILIKE '$1'")
# if it is a number
else
#search element by atomic_number
GET_ELEMENT_SELECTED=$($PSQL "SELECT atomic_number, name, symbol, type, atomic_mass, melting_point_celsius, boiling_point_celsius FROM elements INNER JOIN properties USING(atomic_number)INNER JOIN types USING(type_id) WHERE atomic_number=$1")
fi
# if element is not found
if [[ -z $GET_ELEMENT_SELECTED ]]
then
echo I could not find that element in the database.
# if element is found
else
# output result with element information
echo $GET_ELEMENT_SELECTED | while read COL BAR ATOMIC_NUMBER COL BAR NAME COL BAR SYMBOL COL BAR TYPE COL BAR ATOMIC_MASS COL BAR MELTING_POINT COL BAR BOILING_POINT
do
echo "The element with atomic number $ATOMIC_NUMBER is $NAME ($SYMBOL). It's a $TYPE, with a mass of $ATOMIC_MASS amu. $NAME has a melting point of $MELTING_POINT celsius and a boiling point of $BOILING_POINT celsius."
done
fi
fi