I am stuck getting this to pass. If I run the test multiple times, different subtasks (from 1.1:15 until the end) will not pass. I cannot figure out what part of the code might cause the failed subtask to change each and every run.
Help would be appreciated.
My script:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=salon --tuples-only -c"
# Salon appointments script
echo -e "\n~~~~~ MY SALON ~~~~~\n"
echo -e "Welcome to My Salon, how can I help you?\n"
# LIST_SERVICES
SERVICE_INFO=$($PSQL "SELECT service_id, name from services order by service_id")
echo "$SERVICE_INFO" | while read SERVICE_ID BAR SERVICE_NAME; do
echo "$SERVICE_ID) $SERVICE_NAME"
done
read SERVICE_ID_SELECTED
if [[ $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]];then
SERVICE_SELECTION_NAME=$($PSQL "SELECT name from services where service_id = $SERVICE_ID_SELECTED " | sed -r 's/^ *| *$//g')
fi
while [[ -z $SERVICE_SELECTION_NAME ]]; do
echo -e "\nI could not find that service. What would you like today?"
# LIST_SERVICES
SERVICE_INFO=$($PSQL "SELECT service_id, name from services order by service_id")
echo "$SERVICE_INFO" | while read SERVICE_ID BAR SERVICE_NAME; do
echo "$SERVICE_ID) $SERVICE_NAME"
done
# read selection
read SERVICE_ID_SELECTED
# if input is not a number
if [[ ! $SERVICE_ID_SELECTED =~ ^[0-9]+$ ]];then
continue
else
SERVICE_SELECTION_NAME=$($PSQL "SELECT name from services where service_id = $SERVICE_ID_SELECTED " | sed -r 's/^ *| *$//g')
fi
done
echo -e "\nWhat's your phone number?"
read CUSTOMER_PHONE
# Lookup customer name
CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone = '$CUSTOMER_PHONE' ")
if [[ -z $CUSTOMER_NAME ]]; then
echo -e "\nI don't have a record for that phone number, what's your name?"
read CUSTOMER_NAME
# Insert customer record
INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE')")
# echo IR = $INSERT_CUSTOMER_RESULT
fi
# get customer_id
CUSTOMER_ID=$($PSQL "SELECT customer_id from customers WHERE phone = '$CUSTOMER_PHONE'")
echo -e "\nWhat time would you like your $SERVICE_SELECTION_NAME, $CUSTOMER_NAME?"
read SERVICE_TIME
# Add Appointment
INSERT_APPOINTMENT_RESULT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")
if [[ -z INSERT_APPOINTMENT_RESULT ]]; then
Main_MENU "Error try again"
else
echo -e "\nI have put you down for a $SERVICE_SELECTION_NAME at $SERVICE_TIME, $CUSTOMER_NAME.\n"
fi