-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.sh
57 lines (50 loc) · 1.98 KB
/
elements.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
#! /bin/bash
INPUT=$1
PSQL="psql -X --username=freecodecamp --dbname=periodic_table --tuples-only -c"
function MAIN(){
if [[ -z $INPUT ]]; then
echo "Please provide an element as an argument."
else
FIND_IF_EXISTS
if [[ $EXISTS != true ]] ; then
NOT_FOUND
else
FIND_DATA
SHOW_RESULT $ATOMIC_NUMBER $NAME $SYMBOL $TYPE $MASS $MELTING $BOILING
fi
fi
}
function NOT_FOUND() {
echo "I could not find that element in the database."
}
function FIND_IF_EXISTS() {
if [[ $INPUT =~ ^[0-9]+$ ]]; then
NAME=$($PSQL "select name from elements where atomic_number = $INPUT")
if [[ -z $NAME ]] ; then
EXISTS=false
else
ATOMIC_NUMBER=$INPUT
EXISTS=true
fi
else
ATOMIC_NUMBER=$($PSQL "select atomic_number from elements where name = '$INPUT' or symbol = '$INPUT'")
if [[ -z $ATOMIC_NUMBER ]] ; then
EXISTS=false
else
EXISTS=true
fi
fi
}
function FIND_DATA() {
NAME=$($PSQL "select name from elements where atomic_number = $ATOMIC_NUMBER")
SYMBOL=$($PSQL "select symbol from elements where atomic_number = $ATOMIC_NUMBER")
TYPE=$($PSQL "select type from properties inner join types using(type_id) where atomic_number = $ATOMIC_NUMBER;")
MASS=$($PSQL "select atomic_mass from properties inner join types using(type_id) where atomic_number = $ATOMIC_NUMBER;")
MELTING=$($PSQL "select melting_point_celsius from properties inner join types using(type_id) where atomic_number = $ATOMIC_NUMBER;")
BOILING=$($PSQL "select boiling_point_celsius from properties inner join types using(type_id) where atomic_number = $ATOMIC_NUMBER;")
}
function SHOW_RESULT(){
A=$1 ; N=$2 ; S=$3 ; T=$4 ; M=$5 ; MP=$6 ; BP=$7
echo "The element with atomic number $A is $N ($S). It's a $T, with a mass of $M amu. $N has a melting point of $MP celsius and a boiling point of $BP celsius."
}
MAIN