Hello,
I’m stuck. Help. Don’t know what is wrong. Help.
So I’m trying to check if the user inputs a valid service_id.
This part is not working:
SERVICE_ID_EXISTS=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_CHOSEN")
if [[ -z $SERVICE_ID_EXISTS ]]
then
MAIN_MENU "No service with the number: $SERVICE_ID_CHOSEN"
else
echo "test print --> $SERVICE_ID_EXISTS"
fi
I want to check if the user types a number that corresponds to a service and if a random number is given then return to MAIN MENU with a message.
I try to implement the steps from the Build a Bike Rental Shop course. There is a part that goes like:
# get bike availability
BIKE_AVAILABILITY=$($PSQL "SELECT available FROM bikes WHERE bike_id = $BIKE_ID_TO_RENT AND available = true")
# if not available
if [[ -z $BIKE_AVAILABILITY ]]
then
# send to main menu
MAIN_MENU "That bike is not available."
else
and it works fine, but in the salon script it doesn’t, because for some reason -z $SERVICE_ID_EXISTS is always false.
When I run the script I get the following results.
It seems like the column name and some bars from the services table (from the salon database) are saved in the $SERVICE_ID_EXISTS variable. Why is that happening?
The salon.sh so far:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=salon -c"
echo -e "\n~~~ Welcome to The Salon ~~~\n"
MAIN_MENU() {
if [[ $1 ]]
then
echo -e "\n$1"
fi
echo -e "\nHere are our services:"
LIST_OF_SERVICES=$($PSQL "SELECT * FROM services")
echo "$LIST_OF_SERVICES" | while read SERVICE_ID BAR NAME
do
if [[ $SERVICE_ID =~ ^[0-9]+$ ]]
then
echo "$SERVICE_ID) $NAME"
fi
done
echo -e "\nWhat would you like to do?"
echo -e "1) Choose a service\n2) Set an appointment\n3) Donate\n4) Leave the salon"
read MAIN_MENU_SELECTION
case $MAIN_MENU_SELECTION in
1) SERVICE_MENU ;;
2) APPOINTMENT_MENU ;;
3) DONATION_MENU ;;
4) EXIT ;;
*) MAIN_MENU "Please select a valid option." ;;
esac
}
SERVICE_MENU() {
echo -e "\nChoose a service (type service number):"
read SERVICE_ID_CHOSEN
if [[ ! $SERVICE_ID_CHOSEN =~ ^[0-9]+$ ]]
then
MAIN_MENU "This is not a valid service number."
else
SERVICE_ID_EXISTS=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_CHOSEN")
if [[ -z $SERVICE_ID_EXISTS ]]
then
MAIN_MENU "No service with number: $SERVICE_ID_CHOSEN"
else
echo "test print --> $SERVICE_ID_EXISTS"
fi
fi
}
APPONOINTMENT_MENU() {
echo "Appointment Meniu"
}
DONATION_MENU() {
echo "Donation Meniu"
}
EXIT() {
echo "Exit Meniu"
}
MAIN_MENU
The Build a Salon Appointment Scheduler project