하루 중 시간에 따라 사례 설명을 구별하는 방법

하루 중 시간에 따라 사례 설명을 구별하는 방법

업데이트됨:

다음을 사용하여 키보드로 색상을 변경하려고 합니다.정점몇시인지에 따라. 하지만 내 사례 진술이 문제를 일으키고 있습니다. 스크립트가 실행되는 시간, $zed를 다양한 가능성과 비교하고 이에 따라 조명을 설정하고 싶습니다.

하지만 매번 기본 사례가 제공됩니다. 그러면 "Dude what?"이 출력됩니다.

내 케이스가 작동하지 않는 이유는 무엇입니까?

#!/bin/bash
#Use my keyboard as a clock
#https://github.com/tuxmark5/ApexCtl/issues
#set -vx
zed=`date +"%H"`  
echo $zed

off="000000"
white="FFFFFF"
orange="FF8000"
yellow="FFFF00"
lime="80FF00"
green="00FF00"
teal="00FF80"
turquoise="00FFFF"
sky="0080FF"
blue="0000FF"
purple="7F00FF"
fuschia="FF00FF"
lavender="FF007F"
red="FF0000"


  case $zed in
  0[0-3])
  #purple bluw logo
  apexctl colors -n 551A8B:8 -s 551A8B:8 -e 551A8B:8  -w 551A8B:8 -l 0000FF:8
  ;;
  0[4-9])
  #too early for this
  sudo apexctl colors -n $off:8 -s $off:8 -e $off:8  -w $off:8 -l $off:8
  ;;
  [10-12])
  #still too early for this
  apexctl colors -n $off:8 -s $off:8 -e $off:8  -w $off:8 -l $red:8
  ;;
  [13])
  apexctl colors -n $white:8 -s $white:8 -e $white:8  -w $white:8 -l $white:8
  ;;
  [14])
  apexctl colors -n $orange:8 -s $orange:8 -e $orange:8  -w $orange:8 -l $orange:8
  ;;
  [15])
  apexctl colors -n $yellow:8 -s $yellow:8 -e $yellow:8  -w $yellow:8 -l $yellow:8
  ;;
  [16])
  apexctl colors -n $lime:8 -s $lime:8 -e $lime:8  -w $lime:8 -l $lime:8
  ;;
  [17])
  apexctl colors -n $green:8 -s $green:8 -e $green:8  -w $green:8 -l $green:8
  ;; 
  [18])
  apexctl colors -n $teal:8 -s $teal:8 -e $teal:8  -w $teal:8 -l $teal:8
  ;;
  [19])
  apexctl colors -n $purple:8 -s $purple:8 -e $purple:8  -w $purple:8 -l $purple:8
  ;; 
  [20])
  apexctl colors -n $fuschia:8 -s $fuschia:8 -e $fuschia:8  -w $fuschia:8 -l $fuschia:8
  ;;
  [21-23])  
  apexctl colors -n $red:8 -s $red:8 -e $red:8  -w $red:8 -l $blue:8
  ;;
   *) 
   echo "Dude What?"
  ;;
 esac

답변1

나는 당신의 진술에서 무슨 일이 일어나고 있는지 봅니다 case. 로부터패턴 매칭매뉴얼 페이지 섹션 bash:

[...]  Matches any one of the enclosed characters.

10시부터 23시까지의 모든 시간 동안 패턴 일치는 모든 시간을 찾습니다.하나동봉된 문자 중

옵션 1:

1[0-2])
apexctl ...
;;

1[3])
apexctl ...
;;

옵션 2:

10|11|12)
apexctl ...
;;

13)
apexctl ...
;;

케이스 기능과 관련이 없는 참고사항:

0-4시간 apexctl명령 앞에는 sudo. 그게 무슨 뜻인가요?

답변2

당신은 길을 완전히 오해하고 case[ ]합니다. 필요한 것은 다음과 같습니다 if ... elif ....

if [ "$zed" -eq 0 ] && [ "$zed" -le 3 ]; then
    : ...
elif [ "$zed" -gt 3 ] && [ "$zed" -lt 12 ]; then
    : ...
elif [ "$zed" -eq 27 ]; then
    : ...
else
    : ...
fi

[ "$zed" -eq 0] && [ "$zed" -le 3 ]0은 3보다 작기 때문에 어쨌든 의미가 없습니다. 즉, 혼자인 것과 같습니다 [ "$zed" -le 3 ].

관련 정보