2

Problem is to read one character from the user (this may be 'Y', 'y', 'N', 'n'). If the character is Y or y display YES. If the character is N or n display NO.

Original Problem

Here's my code:

read option
if ["$option"=="y"] || ["$option"=="Y"]
then
    echo "YES"
else
    echo "NO"
fi

It throws an Error (stderr)

solution.sh: line 2: [Y=y]: command not found
solution.sh: line 2: [Y=Y]: command not found
Ganesh Pandey
  • 5,216
  • 1
  • 33
  • 39

3 Answers3

3

Leave space before ] and after [ in if statement.. if [ "$option"="y" ] || [ "$option"="Y" ]

RBH
  • 572
  • 4
  • 11
3
read option
if [ "$option" == "y" ] || [ "$option" == "Y" ]
then
echo "YES"
elif [ "$option" == "n" ] || [ "$option" == "N" ]   
then
echo "NO"
else
echo "?"
fi
onur
  • 5,647
  • 3
  • 23
  • 46
2

You can use this in BASH:

[[ "$option" == [Yy] ]] && echo "YES" || echo "NO"
anubhava
  • 761,203
  • 64
  • 569
  • 643