Shell 脚本语法错误 if [[ $Char == [AZ] -o $Char == [az] ]]

发布于 2025-01-16 04:13:10 字数 919 浏览 0 评论 0原文

在下面提到的代码中,该行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2025-01-23 04:13:10

您混淆了各种比较运算符的语法。 test(或[)命令确实使用-o语法作为“or”运算符,但是 [[条件构造具有不同的语法;在那里,-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

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:

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

You could alternatively use the bash syntax that tests against your locale's character classes:

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文