In a bash script/command how can I make a PC beep

2020-01-28 03:29发布

问题:

I have some long running scripts with breaks requiring input/interaction to continue but when I switch to another window I'd like to be notified (by sound) that a task is complete and now awaiting input.

I would prefer to be able to play an audio clip (*.mp3, *.ogg, etc.) but wouldn't care if the only solution is to make the PC Speaker beep noise.

Any ideas? I'm open to any CLI utilities I can install that play sounds that in turn I can execute when needed.

FYI: My System is running WinXP Pro.

UPDATE: Doh! My Windows > Control Panel > Sounds > Default Beep: was set to (none). Grrr...

Problem solved.

回答1:

Try this:

echo ^G

(^G is obtained by ctrl+G).

Note: you can't copy and paste this code in a batch file, it won't work. To obtain a ^G character in a file, type in a cmd window:

echo ^G > beep.txt

(again, ^G is obtained by ctrl+G).

Then you'll have a file named beep.txt, open it with notepad, there will be a square character. This is our ^G once it is saved in a file.

You can then copy and paste it in a batch file to make a sound (don't forget to put "echo" in front of it).



回答2:

This will make a beep from within bash

echo -en "\007"


回答3:

spd-say

sleep 2; spd-say 'get back to work'

Infinite loop with -w if you need extra motivation:

sleep 2; while true; do spd-say -w 'get back to work'; done

Pre-installed on Ubuntu 14.04 via the package speech-dispatcher: http://releases.ubuntu.com/trusty/ubuntu-14.04.4-desktop-amd64.manifest for blind people I suppose?

See also: https://askubuntu.com/questions/277215/how-to-make-a-sound-once-a-process-is-complete

Also add a popup

This combo is a life saver, b stands for beep:

b() ( spd-say 'done'; zenity --info --text "$(date);$(pwd)" & )

and then:

super-slow-command;b

If I'm somewhere in the room, I'll hear it and know that the long job is done.

Otherwise, I'll see the popup when I get back to my computer.

Related: How to show a GUI message box from a bash script in linux?

Listen to your cooler

I'm joking of course, but for compilation I noticed that I use often use this queue subconsciously. When the cooler stops humming for a while, it means that the compilation is over!



回答4:

copy con beep.bat [Enter]

@echo off [Enter]

echo [Ctrl+G] [Enter]

[Ctrl+Z] [Enter]

beep.bat [Enter]



回答5:

By setting this variable as follows

PROMPT_COMMAND="echo -en '\a'"

then bash will beep every time it shows the prompt. When you do not need it anymore,

unset PROMPT_COMMAND



回答6:

To play the system sound from Windows command line you can run:

rundll32 user32.dll,MessageBeep

It should work on all version of Windows.



标签: shell audio