0

UPDATED:

I am trying to do some color changes with my keyboard using apexctl depending on what time it is. But my case statement is giving me trouble. I want to compare the time when the script runs, $zed to various possibilities that it could be, and set the lights accordingly.

But it gives me th default case every time. This outputs "Dude what?"

Why arent any of my cases working?

#!/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
2
  • Define "chokes"
    – psusi
    Jan 9 '15 at 1:47
  • 2
    Note in Hauke Laging's answer that there's a space after the [. That's essential because the [ isn't mere grouping syntax, it's actually a command: it's a synonym of test. Also, you should (generally) quote parameter expansions; this is especially important inside test expressions.
    – PM 2Ring
    Jan 9 '15 at 7:05
3

You completely misunderstand the way case and [ ] work. What you need is 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 ] doesn't make sense anyway because 0 is smaller than 3 i.e. it is the same like [ "$zed" -le 3 ] alone.

3

I see what is happening with your case statements. From the Pattern Matching section of the bash man page:

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

For all of the hours from 10-23, the pattern matching is looking for any one of the enclosed characters.

Option 1:

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

1[3])
apexctl ...
;;

Option 2:

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

13)
apexctl ...
;;

Note unrelated to the case functionality:

Your 0-4 hour apexctl command is prepended with sudo. Is that what you meant?

1
  • I meant to remove them. the script is intended to be run as root, via a cron job.
    – j0h
    Jan 9 '15 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.