trigger an audible notification when a command in unix completes, using the bash exit code

first a few methods which might or might not work:

your_command_here; beep

which doesn’t work on all systems

your_command_here; tput bel

which doesn’t work from inside an attached screen

your_command_here; spd-say "command completed"

which simply sounds awful

there is a possibility to make a notification with speaker-test.
a simple shell script could look like this:

#!/bin/sh
( speaker-test -t sine -f 440 )& pid=$!
sleep 0.4s
kill -9 $pid

but it’s somewhat unreliable.

i decided to buy a voice pack and write my own function.
this sci-fi computer voice pack from the envato marketplace sounds great

i stored all the sound files in /usr/local/scifi-computer-voice-pack-2/

content of the actual functions file /usr/local/functions.sh:

#created by ned 01.2017
#its purpose is to audibly notify the user about the exit code of the last executed command.

#it is sourced in /etc/bash.bashrc


#aplay condition and soundfile link script
#sound files in
#/usr/local/scifi-computer-voice-pack-2/


talk(){

retval=$?


                if [ $retval -eq 0 ]; then
                        #echo "affirmative"
                        tell=0
                else
                        #echo "negative"
                        tell=1
                fi

        if [ $# -eq 1 ]; then
                if [ "$1" = "0" ]; then
                        #echo "affirmative"
                        tell=0
                fi
                if [ "$1" = "1" ]; then
                        #echo "negative"
                        tell=1
                fi
        fi
        if [ $# -eq 2 ]; then
                if [ "$2" = "0" ]; then
                        #echo ok
                        tell=0
                fi
                if [ "$2" = "1" ]; then
                        #echo nok
                        tell=1
                fi
        fi

        #echo "$retval"



case "$1" in
"-h"|"--help")
        printf "talk uses sound files stored in \n\t/usr/local/scifi-computer-voice-pack-2/\n\n"
        printf "usage: talk <argument1> <argument2>\n"
        printf "valid <argument1>:\n"
        printf -- "-h|--help\t\tprints this help message\n"
        printf -- "--boot\t\t\t(quiet) boot message\n-p|--processing\t\tprocessing..\n-y|--yellow\t\tyellow alert\n"
        printf "<no argument1>\t\tuses <exit code>\n"
        printf -- "-t|--transfer\t\tuses <exit code> and transfer*.wav\n"
        printf -- "-a|--access\t\tuses <exit code> and access*.wav\n"
        printf "\na second <argument2> can be passed to\n<no argument1>\n-t|--transfer\n-a|--access\n"
        printf "valid <argument2>:\n0 or 1\t\t\tforce response\n"
        printf "\nexamples:\ntalk\t\t\tresponds using <exit code>\ntalk -a 1\t\tresponds negatively\nrsync *; talk -t\tuses rsync exit code\n"

        ;;
"--fail")
        return 1
        ;;
"--boot")
        #boot
        aplay -q /usr/local/scifi-computer-voice-pack-2/self\ destruct\ initiated.wav
        ;;
"-a"|"--access")
        if [ $tell -eq 0 ]; then
                echo "access granted"
                aplay -q /usr/local/scifi-computer-voice-pack-2/access\ granted.wav
        else
                echo "access denied"
                aplay -q /usr/local/scifi-computer-voice-pack-2/access\ denied.wav
                return 1
        fi
        ;;
"-p"|"--processing")
        echo "processing.."
        aplay -q /usr/local/scifi-computer-voice-pack-2/processing.wav
        ;;
"-t"|"--transfer")
        if [ $tell -eq 0 ]; then
                echo "transfer complete"
                aplay -q /usr/local/scifi-computer-voice-pack-2/transfer\ complete.wav
        else
                echo "transfer incomplete"
                aplay -q /usr/local/scifi-computer-voice-pack-2/transfer\ incomplete.wav
                return 1
        fi
        ;;
"-y"|"--yellow")
        #echo "yellow alert!"
        aplay -q /usr/local/scifi-computer-voice-pack-2/yellow\ alert.wav
        ;;
""|"0"|"1")
        #general purpose
        if [ $tell -eq 0 ]; then
                echo "affirmative"
                aplay -q /usr/local/scifi-computer-voice-pack-2/affirmative.wav
        else
                echo "negative"
                aplay -q /usr/local/scifi-computer-voice-pack-2/negative.wav
                return 1
        fi
        ;;
*)
        echo "invalid arguments"
        echo "see 'talk --help'"
        aplay -q /usr/local/scifi-computer-voice-pack-2/rephrase\ your\ query.wav
        return 1
        ;;
esac
return 0
}


sourcing /usr/local/functions.sh in /etc/bash.bashrc:

#manually added functions added by ned. check for functions.sh file and source it
if [ -f /usr/local/functions.sh ]; then
        . /usr/local/functions.sh
fi

a logout & login are required for the changes to take effect when changing a bashrc file or a shell function.

commands in bash terminate with an exit code, stored in variable $?
my function notifies depending on the exit code of the last run command.

examples:

my_command_here; talk

this will either say “affirmative” or “negative” depending on the exit code of “my_command_here”.

scp *; talk -t

if the scp command fails, talk -t will say “transfer incomplete”, or “transfer complete” if it succeeds.

my_command_here; talk -a

will say “access granted” or “access denied”

some other arguments, which do not consider the bash exit code are also available:

talk -p; my_command_here

talk will say “processing..” and then execute “my_command_here”.

more information can be found with:

talk --help


the bash exit code can’t be accessed with shell scripts or bash commands, because they run in an isolated environment. that’s why a shell function is necessary.

more information on command notifications:
askubuntu
superuser

Posted in IT

One Reply to “trigger an audible notification when a command in unix completes, using the bash exit code”

Comments are closed.