Beeper script

On my home server sometimes I need to know what is happening. As the server does not have any monitor the only way how to get some kind of a response is using the embedded speaker.

Therefore I implemented a script which is accepting one parameter: the message. Message can be a label (uppercase) or a lower case text without punctuation. Predefined labels are:

  • BOOTED
  • SHUTDOWN
  • RESTART
  • CONNECT
  • DISCONNECT
  • FINISHED
  • ERROR
  • and my favorite STARWARS

And lower case text will be translated to Morse code.

Now if my server boots up STARWARS theme is played 😀

#!/bin/sh
modprobe pcspkr

function play() {
	message=$1
	if [[ $message == "STARWARS" ]]; then
		beep -l 350 -f 392 -D 100 --new -l 350 -f 392 -D 100 --new -l 350 -f 392 -D 100 --new -l 250 -f 311.1-D 100 --new -l 25 -f 466.2 -D 100 --new -l 350 -f 392 -D 100 --new -l 250 -f 311.1 -D 100 --new -l 25 -f 466.2 -D 100 --new -l 700 -f 392 -D 100 --new -l 350 -f 587.32 -D 100 --new -l 350 -f 587.32 -D 100 --new -l 350 -f 587.32 -D 100 --new -l 250 -f 622.26 -D 100 --new -l 25 -f 466.2 -D 100 --new -l 350 -f 369.99 -D 100 --new -l 250 -f 311.1 -D 100 --new -l 25 -f 466.2 -D 100 --new -l 700 -f 392 -D 100 --new -l 350 -f 784 -D 100 --new -l 250 -f 392 -D 100 --new -l 25 -f 392 -D 100 --new -l 350 -f 784 -D 100 --new -l 250 -f 739.98 -D 100 --new -l 25 -f 698.46 -D 100 --new -l 25 -f 659.26 -D 100 --new -l 25 -f 622.26 -D 100 --new -l 50 -f 659.26 -D 400 --new -l 25 -f 415.3 -D 200 --new -l 350 -f 554.36 -D 100 --new -l 250 -f 523.25 -D 100 --new -l 25 -f 493.88 -D 100 --new -l 25 -f 466.16 -D 100 --new -l 25 -f 440 -D 100 --new -l 50 -f 466.16 -D 400 --new -l 25 -f 311.13 -D 200 --new -l 350 -f 369.99 -D 100 --new -l 250 -f 311.13 -D 100 --new -l 25 -f 392 -D 100 --new -l 350 -f 466.16 -D 100 --new -l 250 -f 392 -D 100 --new -l 25 -f 466.16 -D 100 --new -l 700 -f 587.32 -D 100 --new -l 350 -f 784 -D 100 --new -l 250 -f 392 -D 100 --new -l 25 -f 392 -D 100 --new -l 350 -f 784 -D 100 --new -l 250 -f 739.98 -D 100 --new -l 25 -f 698.46 -D 100 --new -l 25 -f 659.26 -D 100 --new -l 25 -f 622.26 -D 100 --new -l 50 -f 659.26 -D 400 --new -l 25 -f 415.3 -D 200 --new -l 350 -f 554.36 -D 100 --new -l 250 -f 523.25 -D 100 --new -l 25 -f 493.88 -D 100 --new -l 25 -f 466.16 -D 100 --new -l 25 -f 440 -D 100 --new -l 50 -f 466.16 -D 400 --new -l 25 -f 311.13 -D 200 --new -l 350 -f 392 -D 100 --new -l 250 -f 311.13 -D 100 --new -l 25 -f 466.16 -D 100 --new -l 300 -f 392.00 -D 150 --new -l 250 -f 311.13 -D 100 --new -l 25 -f 466.16 -D 100 --new -l 700 -f 392 &
	elif [[ $message == "BOOTED" ]]; then
		beep -D 50 -l 1000 -n -D 50 -l 1000 -n -D 50 -l 1000 -n -D 50 -l 1000 -n -D 50 -l 1000
	elif [[ $message == "SHUTDOWN" || $message == "HALT" ]]; then
		beep -D 50 -l 1000 -n -D 50 -l 1000 -n -D 50 -l 1000
	elif [[ $message == "RESTART" || $message == "REBOOT" ]]; then
		beep -D 50 -l 1000
	elif [[ $message == "CONNECT" ]]; then
		beep -f 300 -l 100 -D 50 -n -f 400 -l 100 -D 50 -n -f 500 -l 100 -D 50 -n -f 700 -l 400
	elif [[ $message == "DISCONNECT" ]]; then
		beep -f 700 -l 100 -D 50 -n -f 500 -l 100 -D 50 -n -f 400 -l 100 -D 50 -n -f 300 -l 400
	elif [[ $message == "FINISHED" ]]; then
		beep -f 500 -l 200 -D 500 -r 3
	elif [[ $message == "ERROR" ]]; then
		beep -f 500 -l 400 -D 10 -n -f 800 -l 400 -D 10 -n -f 500 -l 400 -D 10 -n -f 800 -l 400 -D 10 -n -f 500 -l 400 -D 10 -n -f 800 -l 400 -D 100
	else
		new_message=""
		for ((i=0; i < ${#message}; i++)); do
			letter=${message:$i:1}
			if [[ $letter == "." ]]; then
				echo -n "."
				beep -D20 -l 80
			elif [[ $letter == "-" ]]; then
				echo -n "-"
				beep -D20 -l 200
			elif [[ $letter == "/" ]]; then
				echo -n " "
				beep -D20 -l 100 -f 1
			elif [[ $letter == " " ]]; then
				new_message="${new_message}//"
			elif [[ $letter == "a" ]]; then
				new_message="${new_message}.-/";
			elif [[ $letter == "b" ]]; then
				new_message="${new_message}-.../";
			elif [[ $letter == "c" ]]; then
				new_message="${new_message}-.-./";
			elif [[ $letter == "d" ]]; then
				new_message="${new_message}-../";
			elif [[ $letter == "e" ]]; then
				new_message="${new_message}./";
			elif [[ $letter == "f" ]]; then
				new_message="${new_message}..-./";
			elif [[ $letter == "g" ]]; then
				new_message="${new_message}--./";
			elif [[ $letter == "h" ]]; then
				new_message="${new_message}..../";
			elif [[ $letter == "i" ]]; then
				new_message="${new_message}../";
			elif [[ $letter == "j" ]]; then
				new_message="${new_message}.--/";
			elif [[ $letter == "k" ]]; then
				new_message="${new_message}-.-/";
			elif [[ $letter == "l" ]]; then
				new_message="${new_message}.-../";
			elif [[ $letter == "m" ]]; then
				new_message="${new_message}--/";
			elif [[ $letter == "n" ]]; then
				new_message="${new_message}-./";
			elif [[ $letter == "o" ]]; then
				new_message="${new_message}---/";
			elif [[ $letter == "p" ]]; then
				new_message="${new_message}.--./";
			elif [[ $letter == "q" ]]; then
				new_message="${new_message}--.-/";
			elif [[ $letter == "r" ]]; then
				new_message="${new_message}.-./";
			elif [[ $letter == "s" ]]; then
				new_message="${new_message}.../";
			elif [[ $letter == "t" ]]; then
				new_message="${new_message}-/";
			elif [[ $letter == "u" ]]; then
				new_message="${new_message}..-/";
			elif [[ $letter == "v" ]]; then
				new_message="${new_message}...-/";
			elif [[ $letter == "w" ]]; then
				new_message="${new_message}.--/";
			elif [[ $letter == "x" ]]; then
				new_message="${new_message}-..-/";
			elif [[ $letter == "y" ]]; then
				new_message="${new_message}-.--/";
			elif [[ $letter == "z" ]]; then
				new_message="${new_message}--../";
			elif [[ $letter == "0" ]]; then
				new_message="${new_message}-----/";
			elif [[ $letter == "1" ]]; then
				new_message="${new_message}.----/";
			elif [[ $letter == "2" ]]; then
				new_message="${new_message}..---/";
			elif [[ $letter == "3" ]]; then
				new_message="${new_message}...--/";
			elif [[ $letter == "4" ]]; then
				new_message="${new_message}....-/";
			elif [[ $letter == "5" ]]; then
				new_message="${new_message}...../";
			elif [[ $letter == "6" ]]; then
				new_message="${new_message}-..../";
			elif [[ $letter == "7" ]]; then
				new_message="${new_message}--.../";
			elif [[ $letter == "8" ]]; then
				new_message="${new_message}---../";
			elif [[ $letter == "9" ]]; then
				new_message="${new_message}----./";
			fi
		done
		if [[ -n $new_message ]]; then
			play "$new_message"
			echo ""
		fi
	fi
}

if [[ -z $1 ]]; then
	echo "USAGE $0 [message]"
else
	play "$1"
fi

Leave a Reply

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