Im writing a script that prompts the user to pick one of three choices, depending on the choice the prompt should echo back a line.
"!/bin/bash
first=chocolate
second=vanilla
third=strawberry
echo "Which would you choose?"
read -p "chocolate,vanilla or strawberry?"
if [ $first ];
then
echo "Chocolate! good choice"
else
echo "Not a valid choice"
if [ $second ];
then
echo "vanilla! good choice"
else
echo "Not a valid choice"
if [ $third ];
then
echo "Strawberry! good choice"
else
echo "Not a valid choice"
fi
But it doesnt work, I type in one of the choices in the prompt when it asks for it but I get back :line 28: syntax error: unexpected end of line
I dont know whats wrong, i have spacing between the brackets and I end the code with fi.
#!
not"!
. Aside from that, it's hard to guess the source of the error since the fragment you posted appears to have fewer than 28 lines. Regardless, it would be more idiomatic to use a case statement. Since you didn't provide an identifier in yourread
command, its result will be available in$REPLY
by default. – steeldriver Oct 30 '23 at 02:13https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Oct 30 '23 at 02:24