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.
I would like to know the correct way to write regular expressions in a case statement.
Thank you for your cooperation.