I’m trying to pull in the variables to list the services offered. For some reason the service id, bar, and name are all getting passed to the first variable SERVICE_ID so when I run it in the terminal the ) is at the end of the row. This did not happen to me when I did the previous bike shop lesson and I can’t see anything that’s different.
It would only let me embed one image, so here’s the result from the terminal:
Could you share your full code please, rather than screenshots? It may be your that your PSQL variable doesn’t have the correct flags for the output which you are expecting.
Sure, here it is. It should be the same variable used in previous lessons, but just in case:
! /bin/bash
PSQL="psql --username=freecodecamp --dbname=salon --no-align --tuples-only -c"
echo -e "\n~~~~My Salon~~~~"
MAIN_MENU ()
{
if [[ $1 ]]
then
echo -e "\n$1"
fi
echo -e "\nWhich service would you like to schedule?"
SERVICE_LIST=$($PSQL "SELECT service_id, name FROM services ORDER BY service_id")
echo "$SERVICE_LIST" | while read SERVICE_ID BAR NAME
do
echo "$SERVICE_ID) $NAME"
done
}
MAIN_MENU
Remove the --no-align
flag and see if that fixes the issue.
I loaded up my code to show you how the format changes with unaligned v aligned output.
I included an extra echo command for illustration purposes:
echo -e "$SERVICES" | while read SERVICE_ID BAR SERVICE
do
echo -e "\n$SERVICE_ID $BAR $SERVICE"
echo "$SERVICE_ID) $SERVICE"
done
UNALIGNED:
As I understand it, unaligned output displays with no spaces, so
SERVICE_ID
will read 1|cut
as a single value.
ALIGNED:
When you remove the --no-align
flag the output is aligned and includes spaces. In this case, it means that you read the variables correctly and your script output works in your context.
…and now that I look back at the bike shop lesson, I see that flag was not used there, either. Thank you! That’s exactly what the issue was.