Read command

Trying to use the read command. How do you add a 2nd option? In this example I'd like to offer two options, pre and post. If you answer pre, you get one output but if you answer post, you get another output.

echo Is this pre or post?
 read pre
 if [ "$pre" == "pre" ]
 then echo You have typed pre.
 
 fi

use elif

echo Is this pre or post?
read pre
if [ "$pre" == "pre" ]
 then echo You have typed pre.
elif [ "$pre" == "post" ]
 then echo You have typed pre.
else
echo "valid options are pre and post"
 fi

this logic does not work, have you tried it?

can you explain this..

How do you add a 2nd option?

---------- Post updated at 10:50 AM ---------- Previous update was at 10:46 AM ----------

$ ./t.sh
Is this pre or post : pre
This is pre

$ ./t.sh
Is this pre or post : post
This is post

$ ./t.sh
Is this pre or post : abc
valid options are pre and post

$ cat t.sh
#!/bin/bash

echo -n "Is this pre or post : "
read ans

if [ "${ans}" == "pre" ]
then
        echo "This is pre"
elif [ "${ans}" = "post" ]
then
        echo "This is post"
else
        echo "valid options are pre and post"
fi
1 Like

Saying "does not work" without showing us the diagnostics you get, without telling us what operating system you're using, and without telling us what shell you're using gives us VERY little information to guess at what might be wrong.

The code itkamaraj suggested is using the wrong comparison operator, but he just duplicated the code you were using (so if your code was working, the changes he suggested should also work).

Are you saying that the 3rd echo in the suggestion:

 then echo You have typed pre.

needed to be:

 then echo You have typed post.

? If so, we would hope that you could correct a typo like that on your own. Please give us some details about your environment and about what is not working. Help us help you.

1 Like

Yes it was the typo, works well thank you.

Did you consider the case ... esac command?