-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.sh
32 lines (28 loc) · 1.16 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
#! /bin/bash
# if no argument provided
if [[ -z $1 ]]
then
# ask for argument
echo "Please provide an element as an argument."
else
# query matching number, symbol or name
PSQL="psql --username=freecodecamp --dbname=periodic_table -t --no-align -c"
QUERY="SELECT atomic_number, name, symbol, type, atomic_mass, melting_point_celsius, boiling_point_celsius FROM elements FULL JOIN properties USING (atomic_number) FULL JOIN types USING (type_id)"
if [[ $1 =~ ^[0-9]+$ ]]
then
# query matching atomic number
IFS='|' read NUMBER NAME SYMBOL TYPE MASS MELTING BOILING <<< $($PSQL "$QUERY WHERE atomic_number=$1;")
else
# query matching symbol or name
IFS='|' read NUMBER NAME SYMBOL TYPE MASS MELTING BOILING <<< $($PSQL "$QUERY WHERE symbol='$1' OR name='$1';")
fi
# if no match found
if [[ -z $NUMBER ]]
then
# say no match found
echo "I could not find that element in the database."
else
# match found, show element information
echo "The element with atomic number $NUMBER is $NAME ($SYMBOL). It's a $TYPE, with a mass of $MASS amu. $NAME has a melting point of $MELTING celsius and a boiling point of $BOILING celsius."
fi
fi