sispmctl – light (on|off|toggle|state)

Hello,
this is a followup to the posts sispmctl English and sispmctl – using it as non root English. With this script we can switch the light on and off, toggle it and check the current state.

This shell script shows how to

  • check the parameters
  • use or with if
  • use echo without creating a new line
  • use sispmctl

Here we go:

#!/bin/sh
# light
# 20110112 crissa for http://blog.rotten.li

if [ "$#" == "1" ]
then
  if [ "$1" == "on" ] || [ "$1" == "-o" ]
  then
    if [ `sispmctl -q -n -g 1` == "0" ]
    then
      sispmctl -q -o 1
      echo "light: switched on"
  
      sleep 3
    else
      echo "light: the light is on"
    fi
  elif [ "$1" == "off" ] || [ "$1" == "-f" ]
  then
    if [ `sispmctl -q -n -g 1` == "1" ]
    then
      sispmctl -q -f 1
      echo "light: switched off"
  
      sleep 3
    else
      echo "light: the light is off"
    fi
  elif [ "$1" == "toggle" ] || [ "$1" == "-t" ]
  then
    echo "light: toggle"
    sispmctl -q -t 1
    echo -n "light: the light is "
    sispmctl -q -g 1
  
    sleep 3
  elif [ "$1" == "state" ] || [ "$1" == "-s" ]
  then
    echo -n "light: the light is "
    sispmctl -q -g 1
  else
    echo "light (on|off|toggle|state)"
  fi
else
  echo "light"
  echo "light on     or light -o to switch light on"
  echo "light off    or light -f to switch light off"
  echo "light toggle or light -t to toggle light"
  echo "light state  or light -s to get light state"
fi

# eof

The script checks the current state before it switches the light on or off. Why switch the light on if it is already on? At the moment I don’t know how toggle could ever be useful. But the current state is useful if you aren’t in the same room as the power outlet and the light!
Bye, Tore

This entry was posted in General, Linux and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *