Listen For Input BASH

Here’s a script I worked out at a Hackathon last weekend to listen for input in BASH without maxing your CPU.

#Listens for input on ttyACM0 (an arduino button) nd 
#output the string to a file. Note this file must 
#already exist and I highly recommend making a ram
#disk for it. It also backgrounds the listening
cat /dev/ttyACM0 >> /tmp/input &

#I had to sleep my script for 5 seconds to get 
#rid of a false trigger. You can probably delete this
sleep 5

#Clears out the file
echo -n "" > /tmp/input

while true; do

	if [ -s /tmp/input ]; then #If temp > 0b
		#do stuff!
		echo -n "" > /tmp/input #Clears the input
	fi
	sleep 1 #This sleep is important! it controls
                #The while loop.



done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s