case 语句的行为不符合预期(fuzzytime() 函数)

case 语句的行为不符合预期(fuzzytime() 函数)
FuzzyTime()
{
local tmp=$( date +%H )

case $((10#$tmp)) in
    [00-05] )
        wtstr="why don't you go to bed"
        ;;
    [06-09] )
        wtstr="I see your very eager to start the day"
        ;;
    [10-12] )
        wtstr="and a very good day too you"
        ;;
    [13-18] )
        wtstr="Good Afternoon"
        ;;
    [19-21] )
        wtstr="Good Evening"
        ;;
    [22-23] )
        wtstr="it is getting late, it's time to party or go to bed"
        ;;
    *)
        wtstr="guess the planet your on has more than a 24 hour rotation"
        echo 'case value is:' $tmp
        ;;
esac
}

case 变量代表 24 小时上下文中的小时,但数字 08 和 17 似乎会引起问题。我通过使用解决了 08 $((10#$tmp)),但现在 17 是一个问题;有什么建议吗?这是我的第一个 bash 脚本,如果这是一个愚蠢的问题,请提前抱歉。

答案1

[]表示字符范围: [10-12] 表示数字 1 2 以及数字 0-1 之间的范围 - 这将匹配 range 中的单个数字0-2

使用简单的比较if-elif-else-fi

if [ "$tmp" -ge 0 ] && [ "$tmp" -le 5 ]; then
  echo "<0,5>"
elif [ "$tmp" -ge 6 ] && [ "$tmp" -le 9 ]; then
  echo "<6,9>"
  #...
else
  #...
fi

(或者,如果您想要每个间隔,您可以迭代一系列范围限制,但在这种情况下您也可以对其进行硬编码 - 正如您正在尝试做的那样)。

编辑:请求的数组版本:

FuzzyTime(){
  local needle=$1 #needle is $1
  : ${needle:=$( date +%H )} #if no needle is empty, set it to "$(date +%H)
  local times=( 0 6 10 13 19 22 24 0 ) 
  local strings=( 
          "why don't you go to bed"
          "I see your very eager to start the day"
          "and a very good day too you"
          "Good Afternoon"
          "Good Evening"
          "it is getting late, it's time to party or go to bed"
          "guess the planet your on has more than a 24 hour rotation"
          )
    local b=0
    # length(times) - 2 ==  index of the penultimate element 
    local B="$((${#times[@]}-2))" 
    for((; b<B; b++)); do
      if ((needle >= times[b] && needle < times[b+1])); then break; fi
    done
  echo "${strings[$b]}"
}

FuzzyTime "$1"

测试:

$ for t in {0..27}; do FuzzyTime "$t"; done
0 -- why don't you go to bed
1 -- why don't you go to bed
2 -- why don't you go to bed
3 -- why don't you go to bed
4 -- why don't you go to bed
5 -- why don't you go to bed
6 -- I see your very eager to start the day
7 -- I see your very eager to start the day
8 -- I see your very eager to start the day
9 -- I see your very eager to start the day
10 -- and a very good day too you
11 -- and a very good day too you
12 -- and a very good day too you
13 -- Good Afternoon
14 -- Good Afternoon
15 -- Good Afternoon
16 -- Good Afternoon
17 -- Good Afternoon
18 -- Good Afternoon
19 -- Good Evening
20 -- Good Evening
21 -- Good Evening
22 -- it is getting late, it's time to party or go to bed
23 -- it is getting late, it's time to party or go to bed
24 -- guess the planet your on has more than a 24 hour rotation
25 -- guess the planet your on has more than a 24 hour rotation
26 -- guess the planet your on has more than a 24 hour rotation
27 -- guess the planet your on has more than a 24 hour rotation

答案2

[root@localhost ~]# FuzzyTime
-bash: ((: 09: value too great for base (error token is "09")
-bash: ((: 09: value too great for base (error token is "09")
-bash: ((: 09: value too great for base (error token is "09")
-bash: ((: 09: value too great for base (error token is "09") 
-bash: ((: 09: value too great for base (error token is "09")
-bash: ((: 09: value too great for base (error token is "09"
guess the planet your on has more than a 24 hour rotation

[root@localhost ~]# FuzzyTime 9
I see your very eager to start the day

临时解决方案似乎是:

user=$( whoami ) 
ltime=$( date +%H%M )
new=$(echo $( date +%H ) | sed 's/^0*//')
outputFT=$(FuzzyTime $new)

echo 'Hello '$user 'its' $ltime 'hours,' $outputFT
# echo 'Hello '$user 'its' $ltime 'hours,' $FuzzyTime

我的问题似乎围绕着如何以 BASH 系统喜欢的格式自动输入时间,似乎任何一位数字都会出现上述错误 0-9。 (仍然非常喜欢数组解决方案)。

答案3

bash/dash/ksh/zsh 等对模式使用与路径名扩展或文件名通配相同的匹配规则case,因此您的 shell 将这些模式解释为范围(并且这些范围甚至无效)。

case还允许您使用 分隔多个模式|

尝试在您的案例中使用0[0-5])0[6-9])1[0-2])等模式匹配。

例如这样:

case $((10#$tmp)) in
    0[0-5]) wtstr="why don't you go to bed" ;;
    0[6-9]) wtstr="I see you're very eager to start the day" ;;
    1[0-2]) wtstr="and a very good day too you" ;;
    1[3-8]) wtstr="Good Afternoon" ;;
    19|2[01]) wtstr="Good Evening" ;;
    2[23]) wtstr="it is getting late, it's time to party or go to bed" ;;
    *) wtstr="guess the planet your on has more than a 24 hour rotation"
       echo 'case value is:' $tmp
       ;;
esac

相关内容