An SO User
An SO User

Reputation: 25018

check if the input is a vowel

I am beginning with bash scripting and implementing most of what I have studied in other programming languages to bash. I have to check if the user has entered a vowel character. I came up with this:

#!/bin/bash
read -p "Enter something: " char
if [[ $char -eq [AEIOUaeiou]* ]]; then
    echo "vowel"
else
    echo "consonant"
fi  

but when I run it on IdeOne, I get the error:

prog.sh: line 3: [[: [AEIOUaeiou]*: syntax error: operand expected (error token is "[AEIOUaeiou]*")  

Can someone please tell me what is wrong here and how to go about correcting it?

I know that the other not-so-clean way is to have multiple conditions ||ed together. I am trying to avoid that.

Upvotes: 1

Views: 10403

Answers (1)

anubhava
anubhava

Reputation: 785491

You need to use == operator for this glob matching:

#!/bin/bash
read -p "Enter something: " char
if [[ "$char" == *[AEIOUaeiou]* ]]; then
    echo "vowel"
else
    echo "consonant"
fi 

-eq - is used for matching numbers

As per man test:

   INTEGER1 -eq INTEGER2
          INTEGER1 is equal to INTEGER2

Upvotes: 4

Related Questions