Last active
November 1, 2021 02:17
-
-
Save jeremyfiel/d49c1d5faaba0494c38c5174c6f86f97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PSQL='psql -U freecodecamp -d periodic_table --tuples-only -c' | |
if [[ $1 ]] | |
then | |
ELEMENT=$1 | |
else | |
echo "Please provide an element as an argument." | |
read ELEMENT | |
fi | |
MAIN() { | |
# if no input, exit the program | |
if [[ -z $ELEMENT ]] | |
then | |
EXIT | |
# if input is only digits, search by atomic_number | |
elif [[ $ELEMENT =~ ^[0-9]+$ ]] | |
then | |
# get element by atomic number | |
GET_ELEMENT_BY_ATOMIC_NUMBER=$($PSQL "SELECT * FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE atomic_number = '$ELEMENT';") | |
#if no result if found, exit the program | |
if [[ -z $GET_ELEMENT_BY_ATOMIC_NUMBER ]] | |
then | |
EXIT | |
else | |
# return search by atomic_number | |
echo $GET_ELEMENT_BY_ATOMIC_NUMBER | while read TYPE_ID BAR ATOMIC_NUMBER BAR SYMBOL BAR NAME BAR ATOMIC_MASS BAR MELTING_POINT BAR BOILING_POINT BAR TYPE | |
do | |
echo -e "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 | |
# if input string length is less or equal to two characters, search by symbol | |
elif [[ ${#ELEMENT} -le 2 ]] | |
then | |
# get element by symbol | |
GET_ELEMENT_BY_SYMBOL=$($PSQL "SELECT * FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE LOWER(symbol) = LOWER('$ELEMENT');") | |
# if no result found, exit the program | |
if [[ -z $GET_ELEMENT_BY_SYMBOL ]] | |
then | |
EXIT | |
else | |
# return search by symbol | |
echo $GET_ELEMENT_BY_SYMBOL | while read TYPE_ID BAR ATOMIC_NUMBER BAR SYMBOL BAR NAME BAR ATOMIC_MASS BAR MELTING_POINT BAR BOILING_POINT BAR TYPE | |
do | |
echo -e "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 | |
# if input string length is greater than 2 characters, search by name | |
elif [[ ${#ELEMENT} -gt 2 ]] | |
then | |
# get element by name | |
GET_ELEMENT_BY_NAME=$($PSQL "SELECT * FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE LOWER(name) = LOWER('$ELEMENT');") | |
# if no result found, exit the program | |
if [[ -z $GET_ELEMENT_BY_NAME ]] | |
then | |
EXIT | |
else | |
# return search by name | |
echo $GET_ELEMENT_BY_NAME | while read TYPE_ID BAR ATOMIC_NUMBER BAR SYMBOL BAR NAME BAR ATOMIC_MASS BAR MELTING_POINT BAR BOILING_POINT BAR TYPE | |
do | |
echo -e "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 | |
else | |
# default to exit for any other invalid input | |
EXIT | |
fi | |
} | |
EXIT() { | |
echo "I could not find that element in the database." | |
} | |
MAIN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment