[relational database -periodic table] How to use regex in case statements (bash script)

See the code below and the comments there in.

#! /bin/bash

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

OUTPUT_ELEMENT_INFO() {
  echo "line6: $1" | grep -E [0-9]+ # Regex work correctly here.
  if [[ $1 ]]
  then
    case $1 in
      [0-9]+) SELECT_BY_ATOMIC_NUMBER $1 ;; # "./element.sh 1", I want it to go through here.
      [A-Z][a-z]?) SELECT_BY_SYMBOL $1 ;;
      [A-Z][a-z]+) SELECT_BY_NAME $1 ;;
      *) EXIT ;; # But it goes this way.
    esac
  else
    echo "Please provide an element as an argument."
  fi
}

SELECT_BY_ATOMIC_NUMBER() {
  echo "ATOMIC_NUMBER $1"
}

SELECT_BY_SYMBOL() {
  echo "SYMBOL $1"
}

SELECT_BY_NAME() {
  echo "NAME $1"
}

EXIT() {
  echo "I could not find that element in the database."
}

OUTPUT_ELEMENT_INFO $1

Here is the result of the execution in the terminal.

γ‚Ήγ‚―γƒͺγƒΌγƒ³γ‚·γƒ§γƒƒγƒˆ 2023-01-22 184719

I would like to know the correct way to write regular expressions in a case statement.

Thank you for your cooperation.

Solution: Regular expressions cannot be used in CASE. Only wildcards could be used.

I was able to solve the problem myself.
Here is the result of the correction.

Mod edit: solution redacted 

I will keep the topic for my future friends who have the same problem and for myself.

This project is used for certification purposes and therefore sharing code is not allowed. I have redacted it.

My apologies.
Thank you for the correction.

1 Like