Skip to content

Instantly share code, notes, and snippets.

@jeremyfiel
Last active November 1, 2021 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyfiel/d49c1d5faaba0494c38c5174c6f86f97 to your computer and use it in GitHub Desktop.
Save jeremyfiel/d49c1d5faaba0494c38c5174c6f86f97 to your computer and use it in GitHub Desktop.
#!/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