Could someone have a look over my salon.sh please?
Screenshots below appear to suggest that the script is behaving as expected (with expected sample output on right), and the database is being updated accordingly, but the tests below are not passing… (all others are passing).
Maybe I’m missing something obvious?
#!/bin/bash
PSQL="psql -X --username=freecodecamp --dbname=salon --tuples-only -c"
echo -e "\n~~~~~ MY SALON ~~~~~\n"
echo -e "\nWelcome to My Salon, how can I help you?\n"
SERVICE_MENU() {
if [[ $1 ]]
then
echo -e "\n$1\n"
fi
#get services
SERVICES=$($PSQL "SELECT * FROM services ORDER BY service_id")
echo -e "$SERVICES" | while read SERVICE_ID BAR SERVICE
do
echo "$SERVICE_ID) $SERVICE"
done
read SERVICE_ID_SELECTED
if [[ ! $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]]
then
SERVICE_MENU "I could not find that service. What would you like today?"
else
SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id=$SERVICE_ID_SELECTED")
#check if valid service_id
if [[ -z $SERVICE_NAME ]]
then
SERVICE_MENU "I could not find that service. What would you like today?"
else
#get customer info
echo -e "\nWhat's your phone number?"
read CUSTOMER_PHONE
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone = '$CUSTOMER_PHONE'")
# if customer doesn't exist
if [[ -z $CUSTOMER_NAME ]]
then
# get new customer name
echo -e "\nI don't have a record for that phone number, what's your name?"
read CUSTOMER_NAME
# insert new customer
INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE')")
fi
# get customer_id
CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
# ask appointment time
echo -e "\nWhat time would you like your $(echo $SERVICE_NAME | sed -r 's/^ *| *$//g'), $(echo $CUSTOMER_NAME | sed -r 's/^ *| *$//g')?"
#check time format
while [[ ! $SERVICE_TIME =~ ^([0-9]{2}\:[0-9]{2})$ ]]
do
echo -e "\nPlease enter time in format hh:mm"
read SERVICE_TIME
done
#if valid insert into appointments table
INSERT_APPOINTMENT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")
echo -e "\nI have put you down for a $(echo $SERVICE_NAME | sed -r 's/^ *| *$//g') at $(echo $SERVICE_TIME | sed -r 's/^ *| *$//g'), $(echo $CUSTOMER_NAME | sed -r 's/^ *| *$//g')."
fi
fi
}
SERVICE_MENU