時間帯に基づいてケースステートメントを区別する方法

時間帯に基づいてケースステートメントを区別する方法

更新しました:

私はキーボードを使って色を変えようとしていますアペックス時間によって異なります。しかし、私のケース ステートメントは問題を引き起こしています。スクリプトが実行される時間を比較し、さまざまな可能性を考慮して、それに応じてライトを設定したいと考えています。

しかし、毎回デフォルトのケースが表示されます。これは「おい、何?」と出力します。

なぜどのケースも機能しないのでしょうか?

#!/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:

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 ]

関連情報