1

I run various scripts inside BASH. These use various programs, but mostly for compiling. E.g.:

#!/bin/bash
cp file1.txt file2.txt
pdflatex file.txt
bibtex file
pdflatex file.txt

Sometimes, a program runs into an error, which it prints out and stops the script from finishing. Is there any way to have the BASH script trigger a bell, notifying me that an error has been found and the script has stopped?

Village
  • 22,513
  • 46
  • 122
  • 163

3 Answers3

6

You can use the trap command to define an exit function and notify you:

function exit_shell {
    echo -e "Goodbye!\a"
}
trap exit_shell 1 2 3 4 5 6 7 8 10 11 12 13 14 15

# And the rest of your script goes here...

This way, almost any signal (except for 9 or KILL which can't be trapped) will cause your function to execute. The \a will sound the terminal bell in a hardware independent manner (although it's almost always ASCII 0x07).

However, why limit yourself to a mere bell which might not sound. You could send yourself an SMS text using the mail command and your cell phone service provider's email gateway. For example, in Verizon you can send an email to phone@vtext.com where phone is your phone number without dashes.

function exit_shell {
    echo -e "Goodbye!" | mailx -s "Command failed!" $phone@vtext.com
}
trap exit_shell 1 2 3 4 5 6 7 8 10 11 12 13 14 15

Or, you can find a command line Twitter client or Facebook client and update all of your friends with your status.

David W.
  • 105,218
  • 39
  • 216
  • 337
3

echo -e "\007"

007 is ASCII for BEL or bell.

this system beep?

cctan
  • 2,015
  • 3
  • 19
  • 29
1

Other than the terminal bell in cctan's answer (which can be disabled), there is no universal way to do this. In Linux, the "aplay" command can be used to play a sound file from the command line.

jordanm
  • 33,009
  • 7
  • 61
  • 76