Files
fcc-files/element.sh
2024-11-03 01:48:12 +00:00

28 lines
1.4 KiB
Bash

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=periodic_table -t --no-align -c"
if [[ ! $1 ]]; then
echo "Please provide an element as an argument."
exit 0
fi
if [[ $1 =~ ^[0-9]+$ ]]; then
ELEMENT_RESULT=$($PSQL "SELECT elements.atomic_number, symbol, name, atomic_mass, melting_point_celsius, boiling_point_celsius, type FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE atomic_number = $1")
else
ELEMENT_RESULT=$($PSQL "SELECT elements.atomic_number, symbol, name, atomic_mass, melting_point_celsius, boiling_point_celsius, type FROM elements INNER JOIN properties USING(atomic_number) INNER JOIN types USING(type_id) WHERE symbol = '$1' OR name = '$1'")
fi
if [[ -z $ELEMENT_RESULT ]]; then
echo "I could not find that element in the database."
exit 0
fi
ATOMIC_NUMBER=$(echo "$ELEMENT_RESULT" | cut -d'|' -f1)
SYMBOL=$(echo "$ELEMENT_RESULT" | cut -d'|' -f2)
NAME=$(echo "$ELEMENT_RESULT" | cut -d'|' -f3)
ATOMIC_MASS=$(echo "$ELEMENT_RESULT" | cut -d'|' -f4)
MELTING_POINT=$(echo "$ELEMENT_RESULT" | cut -d'|' -f5)
BOILING_POINT=$(echo "$ELEMENT_RESULT" | cut -d'|' -f6)
TYPE=$(echo "$ELEMENT_RESULT" | cut -d'|' -f7)
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."