Red5tar
Red5tar

Reputation: 9

working if else than with functions

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

Upvotes: 0

Views: 49

Answers (1)

NeilWang
NeilWang

Reputation: 367

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

Upvotes: 1

Related Questions