Simple CEC run command on event script

To cut a long story short I need to implement a CEC server in order to allow my Samsung TV to interact with other peripherals. I plan on using a RPi for this purpose.

I have installed CEC-untils and can run the tvservice monitor to look for HDMI events. I now need a simple script that will run on boot to issue a command on one of these events.

The output of the tvservice monitor is:

/opt/vc/bin/tvservice -M
Starting to monitor for HDMI events
 HDMI cable is unplugged
 HDMI is attached

My script meeds to look something like:

#!/bin/sh
/opt/vc/bin/tvservice -M

If  " HDMI is attached"
then do "some command"

END

Thanks

I am assuming this might work?

/opt/vc/bin/tvservice -M | grep -q "HDMI is attached" && RunCommand

---------- Post updated at 02:04 PM ---------- Previous update was at 01:35 PM ----------

So my bash script would look like this:

#!/bin/bash 
while : 
do     
/opt/vc/bin/tvservice -M | grep -q "HDMI is attached" && RunCommand 
done

Not really, that waits for the command to finish and quit before running RunCommand or not. You want to deal with lines, I think:

/opt/bin/tvservice -M 2>&1 | while read LINE
do
        case "$LINE" in
        *Attached*) RunCommand ;;
        *) ;;
        esac
done

The 2>&1 may not be necessary, I'm just hedging my bets in case it prints to stderr instead of stdout.

2 Likes

Thankyou Corona688, works a treat. Now made the script run at startup.

So my code is

#!/bin/bash
echo "on 5" | cec-client -s #switch audio on
echo "is" | cec-client -s #make RPi inactice CEC source

/opt/vc/bin/tvservice -M 2>&1 | while read LINE #run tvservice monitor
do
        case "$LINE" in
        *attached*) echo "on 5" | cec-client -s ;; #switch on audio when TV is plugged in
        *) ;;
        esac
done
1 Like