In the Bike Rental Shop workshop (Relational Databases course), I'm stuck on this step:
"The variable will be `t` or empty. Below the `if not available` comment, add an `if` condition that checks if it's empty. Put the `send to main menu` comment in its statements area."
My code in bike-shop.sh matches the hint exactly:
```sh
# if not available
if [[ -z $BIKE_AVAILABILITY ]]
then
# send to main menu
fi
```
Here is my full bike-shop.sh:
```bash
#!/bin/bash
PSQL="psql -X --username=freecodecamp --dbname=bikes --tuples-only -c"
echo -e "\n~~~~~ Bike Rental Shop ~~~~~\n"
MAIN_MENU() {
if [[ $1 ]]
then
echo -e "\n$1"
fi
echo How may I help you?
echo -e "\n1. Rent a bike\n2. Return a bike\n3. Exit"
read MAIN_MENU_SELECTION
case $MAIN_MENU_SELECTION in
1) RENT_MENU ;;
2) RETURN_MENU ;;
3) EXIT ;;
*) MAIN_MENU "Please enter a valid option." ;;
esac
}
RENT_MENU() {
# get available bikes
AVAILABLE_BIKES=$($PSQL "SELECT bike_id, type, size FROM bikes WHERE available=true ORDER BY bike_id")
# if no bikes available
if [[ -z $AVAILABLE_BIKES ]]
then
# send to main menu
MAIN_MENU "Sorry, we don't have any bikes available right now."
else
# display available bikes
echo -e "\nHere are the bikes we have available:"
echo "$AVAILABLE_BIKES" | while read BIKE_ID BAR TYPE BAR SIZE
do
echo "$BIKE_ID) $SIZE\" $TYPE Bike"
done
# ask for bike to rent
echo -e "\nWhich one would you like to rent?"
read BIKE_ID_TO_RENT
# if input is not a number
if [[ ! $BIKE_ID_TO_RENT =~ ^[0-9]+$ ]]
then
# send to main menu
MAIN_MENU "That is not a valid bike number."
else
# get bike availability
BIKE_AVAILABILITY=$($PSQL "SELECT available FROM bikes WHERE bike_id=$BIKE_ID_TO_RENT AND available = true")
echo $BIKE_AVAILABILITY
# if not available
if [[ -z $BIKE_AVAILABILITY ]]
then
# send to main menu
fi
# send to main menu
fi
# send to main menu
fi
# send to main menu
}
RETURN_MENU() {
echo Return Menu
}
EXIT(){
echo -e "\nThank you for stopping in.\n"
}
MAIN_MENU
```
The failing test says: "Your MAIN_MENU function should have the correct if condition added"
I saved the file, reloaded the window, and checked it against the hint several times. Has anyone found what this test actually checks?
Thanks for the reply. I did notice that the test name mentions MAIN_MENU, but I don’t think the problem is there.
The step I’m on is adding the if [[ -z $BIKE_AVAILABILITY ]] block under the # if not available comment. That position is inside RENT_MENU, not MAIN_MENU. MAIN_MENU only prints the menu and handles the user’s selection. All the SQL queries happen in RENT_MENU, and the variable BIKE_AVAILABILITY is assigned there, just two lines above the new if block. So the block can’t belong in MAIN_MENU.
Since the earlier tests for MAIN_MENU passed, I can’t see what I could have missed there. That leaves two options: either I missed something small that makes no sense to me, or the test is checking the wrong function or is mislabeled.
Has anyone else seen this on this step, or does anyone know what the test actually checks?
I understand. Sometimes the tests are super picky about formatting, so no extra whitespaces/tabs/etc. I just completed the relational databases course myself and I remember a few steps being absolutely infuriating! Sadly I do not have my final script anymore to compare yours against. But here’s what I can share:
The check looks something like this:
assert(/#\s*if\s*not\s*available\s*\n\s*if\s*\[\[\s*-z\s*\$BIKE_AVAILABILITY\s*\]\]\s*\n\s*then/i.test(scriptFile));
Thank you so much for sharing the regex! It pointed me in the right direction. The test was checking that the three closing fi lines come directly after each other. I had misunderstood “put the comment in the statements area” and added a new # send to main menu comment instead of moving the existing one, so the leftover comments between the fi lines broke the match. After deleting them, the test passed. Really appreciate the help!