1
read A
if [["$A" == 'Y' -o "$A" =='y']]
then echo "YES"
else echo "NO"
fi

I am very new to shell scripting.basically, i am trying to check if the input is Y or y. I am getting the following error which I am not able to debug.

solution.sh: line 2: [[Y: command not found

Thanks.

nu11p01n73R
  • 24,873
  • 2
  • 34
  • 48
Vaibhav Savala
  • 105
  • 1
  • 6
  • 16

2 Answers2

3

You are missing space after [[

The if should be like

if [[ "$A" == 'Y' || "$A" == 'y' ]]

The [[ ]] is an extended test command, like any command it should be separated from others by spaces

nu11p01n73R
  • 24,873
  • 2
  • 34
  • 48
1

Add proper space inside the square brackets and after the ==:

read A
if [ "$A" == 'Y' -o "$A" == 'y' ]; then 
    echo "YES";
else 
    echo "NO";
fi

Note that to use -o you ought to use test command, which uses single brackets.

If you use [[ ]], with double brackets, you must use || instead of -o:

read A
if [[ "$A" == 'Y' || "$A" == 'y' ]]; then 
    echo "YES";
else 
    echo "NO";
fi
Patrick Trentin
  • 6,721
  • 3
  • 20
  • 38
  • Do i have to use the ; symbol here? Even if i use it or not use it i am still getting errors. solution.sh: line 2: syntax error in conditional expression solution.sh: line 2: syntax error near `-o' solution.sh: line 2: `if [[ "$A" == 'Y' -o "$A" =='y' ]];then ' – Vaibhav Savala Jun 09 '16 at 08:06
  • @VaibhavSavala fixed – Patrick Trentin Jun 09 '16 at 08:11