Shell 脚本语法错误 if [[ $Char == [AZ] -o $Char == [az] ]]
在下面提到的代码中,该行 if [[ $Char == [AZ] -o $Char == [az] ]] 显示以下错误
symbol.sh: line 2: 条件表达式中的语法错误
symbol.sh: line 2: `-o' 附近的语法错误
read -p "Enter the Character " Char
if [[ $Char == [A-Z] -o $Char == [a-z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [0-9] ]]
then
echo "The Character entered is a NUMBER"
else
echo "The Character entered is a Special Symbol"
fi
我尝试以其他方式执行此操作,效果很好。
read -p "Enter the Character " Char
if [[ $Char == [a-z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [A-Z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [0-9] ]]
then
echo "The Character entered is a NUMBER"
else
echo "The Character entered is a Special Symbol"
fi
我只是想知道这行代码有什么问题 if [[ $Char == [AZ] -o $Char == [az] ]]
In the below mentioned code the line
if [[ $Char == [A-Z] -o $Char == [a-z] ]]
shows the following error
symbol.sh: line 2: syntax error in conditional expression
symbol.sh: line 2: syntax error near `-o'
read -p "Enter the Character " Char
if [[ $Char == [A-Z] -o $Char == [a-z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [0-9] ]]
then
echo "The Character entered is a NUMBER"
else
echo "The Character entered is a Special Symbol"
fi
I tried doing it the other way and it works fine.
read -p "Enter the Character " Char
if [[ $Char == [a-z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [A-Z] ]]
then
echo "The Character entered is an ALPHABET"
elif [[ $Char == [0-9] ]]
then
echo "The Character entered is a NUMBER"
else
echo "The Character entered is a Special Symbol"
fi
I just want to know what is wrong with this line of code
if [[ $Char == [A-Z] -o $Char == [a-z] ]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您混淆了各种比较运算符的语法。
test
(或[
)命令确实使用-o
语法作为“or”运算符,但是[[
条件构造具有不同的语法;在那里,-o
检查是否启用了给定的 shell 选项。要使用[[
获得“或”,请在测试之间使用||
语法:您也可以使用针对您的语言环境的字符类进行测试的 bash 语法:
You've mixed up the syntax for the various comparison operators. The
test
(or[
) command does use the-o
syntax to be an "or" operator, but the[[
conditional construct has a different syntax; there,-o
checks to see if the given shell option is enabled. To get an "or" with[[
, use the||
syntax between tests:You could alternatively use the bash syntax that tests against your locale's character classes: