Simple CEC run command on event script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple CEC run command on event script
# 1  
Old 01-20-2017
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:
Code:
/opt/vc/bin/tvservice -M
Starting to monitor for HDMI events
[I] HDMI cable is unplugged
[I] HDMI is attached

My script meeds to look something like:
Code:
#!/bin/sh
/opt/vc/bin/tvservice -M

If  "[I] HDMI is attached"
then do "some command"

END

Thanks
# 2  
Old 01-23-2017
I am assuming this might work?
Code:
/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:
Code:
#!/bin/bash 
while : 
do     
/opt/vc/bin/tvservice -M | grep -q "HDMI is attached" && RunCommand 
done


Last edited by barrydocks; 01-23-2017 at 10:05 AM.. Reason: typo
# 3  
Old 01-23-2017
Not really, that waits for the command to finish and quit before running RunCommand or not. You want to deal with lines, I think:

Code:
/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.
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 01-24-2017
Thankyou Corona688, works a treat. Now made the script run at startup.

So my code is
Code:
#!/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

This User Gave Thanks to barrydocks For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run simple single command on multiple Linux servers?

Hi All, How can i run a single command on multiple servers with or without giving credentials. I have a file(servers.txt) which has got list of servers and i want to run a command lsb_release -dr on all these servers and get output of those servers against each server. I tried below code... (9 Replies)
Discussion started by: darling
9 Replies

2. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

3. Shell Programming and Scripting

Run a script on login event

Hi, I need to run a script whenever a user logs in to HP-UX unix server. Please let me know how can i implement this. I need to run this script on login for all users starting with 'X' ( x110,x112,x13545, etc). Thanks a lot. (2 Replies)
Discussion started by: pradebban
2 Replies

4. UNIX for Dummies Questions & Answers

any one used SEC - Simple Event Correlator

Hi, I am trying to configure SEC - Simple Event Correlator to analyze the logs. Just wondering if any of you have used this before? If yes, can you help by showing few of the rules you created? Thanks. (1 Reply)
Discussion started by: samnyc
1 Replies

5. Shell Programming and Scripting

How run simple command in xterm, and leave open?

I have written a simple script to show battery life remaining. I would like to be able to quickly view it with a predefined keybinding or launcher. xterm -e scriptname should do the trick but the xterm closes when the script finishes, not giving me chance to read the output. How can I keep... (3 Replies)
Discussion started by: spoovy
3 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. UNIX for Dummies Questions & Answers

Not able to run the simple perl script

Hi Experts, I have written simple perl script, which assign the value to variable and print it. Following is the script: $ cat 3.pl #!/usr/bin/env ksh #!/usr/bin/perl print "Hello World"; $iputlne = 34; print $iputlne; The error output is: $ /usr/bin/env perl 3.pl Hello World... (9 Replies)
Discussion started by: Amey Joshi
9 Replies

8. UNIX for Dummies Questions & Answers

tip: Simple script won't run in cygwin - vim editor involved

I ran into this issue and thanks to various postings in various forums, was able to figure out the solution but didn't see one posting that laid the whole issue out cleanly. So thought the following might help others ... ------------------------------------------------------------------------... (2 Replies)
Discussion started by: oxysep
2 Replies
Login or Register to Ask a Question