带有case语句的bash脚本不返回输出

use*_*369 2 linux bash case

我编写了这段代码来根据一天中的什么时间回显问候语,但是当我运行它时,它不会显示任何错误,但也不会向命令行回显任何内容。为了尝试排除故障,我注释掉了所有内容并只回显了时间变量,效果很好。所以,我做错了什么?!

#!/bin/bash


time=$(date +%H)

case $time in
#check if its morning
    [0-11] ) echo "greeting 1";;

#check if its afternoon
    [12-17] ) echo "greeting 2";;

#check if its evening
    [18-23] ) echo "greeting 3"
esac
Run Code Online (Sandbox Code Playgroud)

cho*_*oba 6

[...]引入字符类,而不是整数区间。因此,[18-23]与 相同[138-2],也与 相同[13],因为 8 和 2 之间没有任何值。

您可以使用以下作为修复:

case $time in
#check if its morning
    0?|1[01] ) echo "greeting 1";;

#check if its afternoon
    1[2-7] ) echo "greeting 2";;

#check if its evening
    1[89]|2? ) echo "greeting 3"
esac
Run Code Online (Sandbox Code Playgroud)