read -n1 input
if (($input == "y")) || (($input == "Y"))
then
echo "YES"
elif (($input == "n")) || (($input == "N"))
then
echo "NO"
fi
When the input is entered as "N", it returns "YES", what could be the problem?
read -n1 input
if (($input == "y")) || (($input == "Y"))
then
echo "YES"
elif (($input == "n")) || (($input == "N"))
then
echo "NO"
fi
When the input is entered as "N", it returns "YES", what could be the problem?
you should use simple square brackets to test if something is true. double parenthesis are for arithmetic:
if [ "$input" = "y" ] || [ "$input" = "Y" ]
Notes:
=
instead of == (bash and some other shells allow == as a synonym, but it's not portable)
(thanks @GordonDavisson)Some related questions:
Use [
/ ]
(which tests for logic expressions) instead of ((
/ ))
(which is used to evaluate arithmetic expressions):
read -n1 input
if [ "$input" = "y" ] || [ "$input" = "Y" ]
then
echo "YES"
elif [ "$input" = "n" ] || [ "$input" = "N" ]
then
echo "NO"
fi
Side note: There is a neat workaround to make a case-insensitive comparison:
read -n1 input
if [ "${input,,}" = "y" ]
then
echo "YES"
elif [ "${input,,}" = "n" ]
then
echo "NO"
fi