Linux zsh shell中case用;;&报parse error near `&'如何解决
问题原因及解决方法
错误原因
- 语法差异:Bash中
case语句实现多条件连续匹配的符号为;;&,但Zsh中对应功能的符号为;|,直接套用Bash语法会触发解析错误。 - 冗余笔误:你提供的代码中每个
echo语句末尾多了多余的]字符,也会导致语法异常。 - 额外说明:你代码中
[[:alpha:]]匹配的是纯字母,对应的描述应为「字母字符」,如果需要匹配字母+数字可以改用[[:alnum:]]字符类。
修正后的代码
#! /bin/zsh # testing the 'case' syntax of zsh read \?"enter a character: " case "$REPLY" in [[:upper:]]) echo "The input is an uppercase letter." ;| [[:lower:]]) echo "The input is a lowercase letter." ;| [[:alpha:]]) echo "The input is an alphabetic character." ;; *) echo "Input is invalid." ;; esac
运行效果
输入小写字母f时,会输出你预期的多条件匹配结果:
enter a character: f The input is a lowercase letter. The input is an alphabetic character.
内容的提问来源于stack exchange,提问作者User 1999
相关产品推荐
相关产品推荐

