Periodic Table DB- problem with symbol argument

I’m working on the Relational Database Certification, doing the Periodic Table Database exercise.

I have finished all instances of the exercise except for the following:

" If you run ./element.sh 1 , ./element.sh H , or ./element.sh Hydrogen , it should output The element with atomic number 1 is Hydrogen (H). It's a nonmetal, with a mass of 1.008 amu. Hydrogen has a melting point of -259.1 celsius and a boiling point of -252.9 celsius. "

I get the desired input when I pass a number and a full element name as an argument, but I can not get the symbol (i.e. ‘H’) argument to work.

here are the if statements of my code:

# if word or symbol
  if [[ ! $1 =~ ^[0-9]+$ ]]
    then
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.name='$1'")
    else
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.name='^$1' ORDER BY atomic_number")
  fi

  # if number
  if [[ $1 =~ ^[0-9]+$ ]]
    then
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.atomic_number=$1")
  fi

LINK TO EXERCISE

I don’t know what condition to put in order for the script to recognize the symbol argument, any ideas are welcome.

Thank you for reading!

use ^[A-Z]$|^[A-Z][a-z]$
<!-- 
 here ^ means carot which means starting
 $ means ending
[A-Z] all capital letters
[a-z] all small letters
| means or
-->

I tried changing the if statement to that expression but it still doesn’t work.

Now I’ve changed my code to the following:

#!/bin/bash

PSQL="psql -X --username=freecodecamp --dbname=periodic_table --tuples-only -c"

if [[ $1 ]]
  then
  if [[ $1 =~ ^[0-9]+$ ]]
    then
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.atomic_number=$1")
    else
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.name LIKE '^$1' OR elements.name LIKE '$1%' ORDER BY atomic_number")
  fi

  if [[ -z $ELEMENT ]]
    then
    echo "I could not find that element in the database."
    else
    echo $ELEMENT | while IFS=" | " read AUTOMIC_NUM SYMBOL NAME ATOMIC_MASS MPC BPC TYPE
    do
    echo -e "The element with atomic number $AUTOMIC_NUM is $NAME ($SYMBOL). It’s a $TYPE, with a mass of $ATOMIC_MASS amu. $NAME has a melting point of $MPC celsius and a boiling point of $BPC celsius."
    done
  fi
  else
  echo -e "Please provide an element as an argument."
fi

And these are the outputs:
FOR COMMAND ./element.sh Hydrogen :
“The element with atomic number 1 is Hydrogen (He). It’s a nonmetal, with a mass of 1.008 amu. Hydrogen has a melting point of -259.1 celsius and a boiling point of -252.9 celsius.”

FOR COMMAND ./element.sh 1 :
“The element with atomic number 1 is Hydrogen (He). It’s a nonmetal, with a mass of 1.008 amu. Hydrogen has a melting point of -259.1 celsius and a boiling point of -252.9 celsius.”

but…
FOR COMMAND ./element.sh H :
“The element with atomic number 1 is Hydrogen (He). It’s a nonmetal 2 | He | Helium | 4.0026 | -272.2 | -269 | nonmetal, with a mass of 1.008 amu. Hydrogen has a melting point of -259.1 celsius and a boiling point of -252.9 celsius.”

I don’t understand what the issue is.

i modified the code, it is working

#!/bin/bash

PSQL="psql -X --username=freecodecamp --dbname=periodic_table --tuples-only -c"

if [[ $1 ]]
  then
  if [[ $1 =~ ^[0-9]+$ ]]
    then
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.atomic_number=$1")
    else
    ELEMENT=$($PSQL "SELECT atomic_number, symbol, elements.name, atomic_mass, melting_point_celsius, boiling_point_celsius, types.type FROM properties JOIN elements USING(atomic_number) JOIN types USING(type_id) WHERE elements.name LIKE '%$1' OR elements.name LIKE '$1%' ORDER BY atomic_number")
  fi

  if [[ -z $ELEMENT ]]
    then
    echo "I could not find that element in the database."
    else
    echo "$ELEMENT" | while IFS=" | " read AUTOMIC_NUM SYMBOL NAME ATOMIC_MASS MPC BPC TYPE
    do
    echo -e "The element with atomic number $AUTOMIC_NUM is $NAME ($SYMBOL). It’s a $TYPE, with a mass of $ATOMIC_MASS amu. $NAME has a melting point of $MPC celsius and a boiling point of $BPC celsius."
    done
  fi
  else
  echo -e "Please provide an element as an argument."
fi
1 Like

AMAZING! thank you so much. :pray:
ps. only needed to add a LIMIT 1 clause at the end of second PSQL command