choose y or n

Hi,
I have written a choice based shell script some thing like this:

if (y)
execute code
....
fi

else if(n)
terminating

the problem with the above scripting is it will work as far as the options are y or n.
but i want to reiterate the same code when the user inputs something else other than y or n.
please can some one tell me How can i achieve this in shell script.

Use elif:

if [ "$x" = y ]
then
   : do y
elif [ "$x" = n ]
then
   : do n
elif [ "$x" = q ]
then
   : do q
fi

or a case statement:

case $x in
     y) : do y
        ;;
     n) : do n
        ;;
     q) : do q
        ;;
esac
1 Like