Shell 脚本语法错误 if [[ $Char == [A-Z] -o $Char == [a-z] ]]

Shell Script Syntax Error if [[ $Char == [A-Z] -o $Char == [a-z] ]]

在下面提到的代码中 如果 [[ $Char == [A-Z] -o $Char == [a-z] ]] 显示以下错误

symbol.sh:第 2 行:条件表达式中的语法错误

symbol.sh:第 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 == [A-Z] -o $Char == [a-z] ]]

您混淆了各种比较运算符的语法。 test(或[)命令确实使用-o语法作为“或”运算符,但the [[ conditional construct有一个不同的语法;在那里,-o 检查是否启用了给定的 shell 选项。要获得 [[ 的“或”,请在测试之间使用 || 语法:

if [[ $Char == [A-Z] ]] || [[ $Char == [a-z] ]] 
then 
# ...

您也可以使用 bash 语法来测试您的语言环境字符 类:

#!/bin/bash
read -p "Enter the Character " Char
if [[ $Char == [[:alpha:]] ]]
then
    echo "The Character entered is an ALPHABET"
elif [[ $Char == [[:digit:]] ]]
then
    echo "The Character entered is a NUMBER"
else
    echo "The Character entered is a Special Symbol"
fi