0

I cant for the life of me figure out why the second function is not working. I have tried using else and elif but i either get a syntax error or my second function does not show. This is just a simple bash script. Please, i need to know what i am doing wrong..

function yes() {
echo "Good boy"
}

function no() {
echo "Bad Boy"
}

echo " Did you eat this pillow? [y,n]" ; tput sgr0
read $answer

if [ "$answer" != "y" ];
then
yes

elif [ "$answer" != "n" ];
then
no

else 
exit

fi
3
  • 1
    Change read $answer to read answer. Commented Jul 22, 2017 at 4:41
  • The parameter of read must be a variable name. In read answer, "answer" is a name. In read $answer, $answer is the value of the variable named "answer". Commented Jul 22, 2017 at 8:35
  • I cant believe i missed such a simple thing Commented Jul 22, 2017 at 17:35

1 Answer 1

1

You should remove the $ sign of the first "answer" variable. What's more, read supports display a prompt before you input characters, so you should change your script like this:

#!/bin/bash
function yes() {
    echo "Good boy"
}

function no() {
    echo "Bad Boy"
}

read -p " Did you eat this pillow? [y,n]" answer

if [ "$answer" != "y" ]
then
    yes
elif [ "$answer" != "n" ]
then
    no
else
    exit
fi
Sign up to request clarification or add additional context in comments.

1 Comment

@Red5tar Please accept my answer if you feel helpful. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.