0
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?

2 Answers2

4

you should use simple square brackets to test if something is true. double parenthesis are for arithmetic:

if [ "$input" = "y" ] || [ "$input" = "Y" ]

Notes:

  • put double quotes around your variables
  • put spaces before and after the brackets
  • use = instead of == (bash and some other shells allow == as a synonym, but it's not portable) (thanks @GordonDavisson)

Some related questions:

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Error (stderr) Solution.sh: line 3: [Y: command not found Solution.sh: line 3: [Y: command not found Solution.sh: line 6: [Y: command not found Solution.sh: line 6: [Y: command not found – Selman Keskin Mar 09 '21 at 15:15
  • you need to put spaces before and after your brackets – Chris Maes Mar 09 '21 at 15:17
  • 1
    In a `[ ]` test expression, you should double-quote variable references (i.e. `"$input"` instead of just `$input`) to avoid weird parsing, and use `=` instead of `==` (bash and some other shells allow `==` as a synonym, but it's not portable). – Gordon Davisson Mar 09 '21 at 18:17
3

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
NullDev
  • 6,739
  • 4
  • 30
  • 54
  • 1
    Since the question is tagged `bash` as well as `sh`: in `bash` it's [usually better](http://mywiki.wooledge.org/BashFAQ/031) to use `[[ ]]` instead of `[ ]`. – Thomas Mar 09 '21 at 15:03
  • its useless, it dont work like that, thank you for quick reply – Selman Keskin Mar 09 '21 at 15:07
  • i need a solution with paratesis – Selman Keskin Mar 09 '21 at 15:10
  • @SelmanKeskin please update the question with these additional details, eg, why does the answer need to use parenthesis? is this a class/homework assignment? (and if so, what are the detailed instructions of the assignment?) – markp-fuso Mar 09 '21 at 15:15
  • thank you for professional answer, its worked – Selman Keskin Mar 09 '21 at 15:23
  • 1
    In a `[ ]` test expression, you should double-quote variable references (i.e. `"$input"` instead of just `$input`) to avoid weird parsing, and use `=` instead of `==` (bash and some other shells allow `==` as a synonym, but it's not portable). – Gordon Davisson Mar 09 '21 at 18:17
  • @GordonDavisson Yes, you're absolutely right! I did both of those in the second "optimized" code. I wanted to leave OP's code as-is and just correct the `((` / `))`. I updated the answer now. Thank you! – NullDev Mar 09 '21 at 18:22