如何根據一天中的不同時間取得案例陳述

如何根據一天中的不同時間取得案例陳述

更新:

我正在嘗試使用鍵盤進行一些顏色更改頂點控制取決於現在是什麼時間。但我的案件陳述給我帶來了麻煩。我想將腳本運行的時間 $zed 與它可能出現的各種可能性進行比較,並相應地設置燈光。

但它每次都會給我預設。這輸出“夥計什麼?”

為什麼我的任何一個案例都不起作用?

#!/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 ]單獨的相同。

相關內容