bash 检查输入是否是元音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26739485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if the input is a vowel
提问by Little Child
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:
我从 bash 脚本开始,并将我在其他编程语言中研究过的大部分内容实现到 bash。我必须检查用户是否输入了元音字符。我想出了这个:
#!/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:
但是当我在 IdeOne 上运行它时,出现错误:
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.
我知道另一种不太干净的方法是将多个条件||
放在一起。我正在努力避免这种情况。
回答by anubhava
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
-eq
- 用于匹配数字
As per man test
:
根据man test
:
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2