一旦过程完成,如何发出声音?
我通过终端开始了一个漫长的过程。 一旦过程完成,是否可以让Ubuntu终端发出声音? 这样,我不需要继续检查,而是通过声音通知。
至少有三种命令行方法可以通过将脚本命令放在脚本末尾来实现这一点,您可以为长时间的过程调用它:
-
发出声音的“经典”方式是使用嘟嘟声 通过PC扬声器。 但是这并不适用于所有情况(例如在我的系统PC扬声器完全禁用),您需要从
/etc/modprobe/blacklist.conf
删除pscpkr并加载pcspkr
内核模块。sudo sed -i 's/blacklist pcspkr/#blacklist pcspkr/g' /etc/modprobe.d/blacklist.conf sudo modprobe pcspkr beep [optional parameters]
-
我们还可以使用aplay(默认安装)以wav格式播放任何声音文件:
aplay /usr/share/sounds/alsa/Side_Right.wav
-
另一种方法是使用pulseaudio命令行界面来启用系统(在
libsndfile
)在默认音频输出上识别的任何声音文件的播放:paplay /usr/share/sounds/freedesktop/stereo/complete.oga
我们可以使用
/usr/share/sounds/
默认声音文件,或者我们可能在不同位置使用的任何其他声音文件。
只是提到它,还有一种很好的方法可以通过滥用espeak实现这一点, espeak默认安装在Ubuntu <= 12.04中。 看,或者更确切地说听下面的例子:
#! /bin/bash c=10; while [ $c -ge 0 ]; do espeak $c; let c--; done; sleep 1 ## here lengthy code espeak "We are done with counting"
在Ubuntu> = 12.10 Orca使用speak-dispatcher。 然后我们可以安装espeak 或者使用spd-say "Text"
。
我用
make; spd-say done
将“make”替换为您使用的长时间运行命令。
根据这个 \a
字符转义ASCII码7,这是计算机的嘟嘟声。
因此echo $'\a'
可以在我的本地机器上发出哔声,即使它是在我通过PuTTY等终端接口连接的计算机上运行的bash shell上执行的。
TL; DR
在命令完成后播放声音:
long-running-command; sn3
(其中sn3
是声音编号3)将它放在.bashrc
:
sound() { # plays sounds in sequence and waits for them to finish for s in $@; do paplay $s done } sn1() { sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg } sn2() { sound /usr/share/sounds/freedesktop/stereo/complete.oga } sn3() { sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga }
或者阅读下面的更多选项:
错误和成功的不同声音
这就是我用于你所要求的东西 – 有一点不同:它不仅在命令完成时播放声音,而且在成功和出错时播放不同的声音。 (但如果你不喜欢它,你可以改变它。)
我有一个名为oks
的Bash函数,我在长时间运行的命令结束时使用它:
make && make test && make install; oks
当前一个命令完成时,它会播放声音并显示OK或ERROR(带有错误代码)。
源代码
这是带有两个助手的函数:
sound() { # plays sounds in sequence and waits for them to finish for s in $@; do paplay $s done } soundbg() { # plays all sounds at the same time in the background for s in $@; do # you may need to change 0 to 1 or something else: pacmd play-file $s 0 >/dev/null done } oks() { # like ok but with sounds s=$? sound_ok=/usr/share/sounds/ubuntu/stereo/dialog-information.ogg sound_error=/usr/share/sounds/ubuntu/stereo/dialog-warning.ogg if [[ $s = 0 ]]; then echo OK soundbg $sound_ok else echo ERROR: $s soundbg $sound_error fi }
安装
您可以直接将它放在〜/ .bashrc中或将其放在其他文件中,然后将此行放在〜/ .bashrc中:
. ~/path/to/file/with/function
组态
将sound_ok
和sound_error
更改为其他声音。
您可以尝试sound
与sound
soundbg
并更改sound_ok
和sound_error
以使用您喜欢的许多声音序列来获得所需的结果。
好听的声音
要在系统上找到一些好的声音,您可以尝试:
for i in /usr/share/sounds/*/stereo/*; do echo $i; paplay $i; sleep 1; done
以下是我经常使用的一些声音,默认情况下可以在Ubuntu上使用,这些声音很适合通知 – sn1声音很大很好,sn2非常响亮且仍然相当不错,sn3非常响亮且不太好听:
sn1() { sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg } sn2() { sound /usr/share/sounds/freedesktop/stereo/complete.oga } sn3() { sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga }
同样,如果您想在后台播放声音而不等待声音完成(例如,当您播放大量声音时不会减慢脚本速度),您可以将sound
更改为soundbg
。
无声版
以防万一 – 这里的function与oks
相同但oks
:
ok() { # prints OK or ERROR and exit status of previous command s=$? if [[ $s = 0 ]]; then echo OK else echo ERROR: $s fi }
用法
以下是您使用它的方式:
成功示例:
ls / && ls /bin && ls /usr; oks
示例有错误:
ls / && ls /bim && ls /usr; oks
当然在实践中命令更像是:
make && make test && make install; oks
但是我使用了ls
所以你可以很快看到它是如何工作的。
对于静默版本,您可以使用ok
而不是oks
。
或者您可以使用例如:
ls / && ls /bim && ls /usr; ok; sn1
打印OK / ERROR但总是播放相同的声音等。
更新
我把这些函数放在GitHub上,参见:
来源可以从以下url下载:
更新2
我在上面的回购中添加了一个soundloop
函数。 它播放声音,可以被Ctrl + C打断(不像一个简单的while true; do paplay file.ogg; done
那个人希望工作,但它没有),正如shadi在评论中所要求的那样 。 它实现为:
soundloop() { set +m a=`date +%s` { paplay $1 & } 2>/dev/null wait b=`date +%s` d=$(($b-$a)) [ $d -eq 0 ] && d=1 while :; do pacmd play-file $1 0 >/dev/null sleep $d done }
如果您认为它很复杂,请将您的投诉转发给PulseAudio开发人员。
扩展Michael Curries的答案,你可以通过PROMPT_COMMAND
使Bash打印成一个BEL
( \a
)字符:
PROMPT_COMMAND='printf \\a'
PROMPT_COMMAND
这种方式设置PROMPT_COMMAND
将使Bash在每个命令结束时执行printf \\a
,这将使终端播放声音(虽然当muru指出时,只需触发重新提示将使终端播放声音,即每次绘制新提示时都会播放声音,例如即使只是按ENTER键也是如此 。
这是终端function,因此可能无法在所有终端上运行; 例如它在控制台中不起作用(但我确信它在gnome-terminal
和xterm
)。
命令
speaker-test
发出吵闹声。 最简单但令人讨厌的解决方案。 🙂
有关配置噪声信号的选项,请参阅speaker-test(1)
手册speaker-test(1)
。
command && (say done ; echo done) || (echo error ; say error)
例1: echo alik && (say done ; echo done) || (echo error ; say error)
echo alik && (say done ; echo done) || (echo error ; say error)
将导致完成单词。
示例2: non_existing_command_error && (say done ; echo done) || (echo error ; say error)
non_existing_command_error && (say done ; echo done) || (echo error ; say error)
将导致错误字。
*需要gnustep-gui-runtime
–
sudo apt-get install gnustep-gui-runtime
干杯。
这不是你问的,但你可以使用通知。
替换其他答案中给出的命令
notify-send "Process terminated" "Come back to the terminal, the task is over"
我创建了一个简单且几乎是本机的脚本来播放声音并显示带有给定消息和Ubuntu时间的通知( Gist ):
#!/bin/sh # https://gist.github.com/John-Almardeny/04fb95eeb969aa46f031457c7815b07d # Create a Notification With Sound with a Given Message and Time # The Downloaded Sound is from Notification Sounds https://notificationsounds.com/ MSSG="$1" TIME="$2" # install wget if not found if ! [ -x "$(command -v wget)" ]; then echo -e "INSTALLING WGET...\n\n" sudo apt-get install wget echo -e "\n\n" fi # install at package if not found if ! [ -x "$(command -v at)" ]; then echo -e "INSTALLING AT...\n\n" sudo apt-get install at echo -e "\n\n" fi # install sox if not found if ! [ -x "$(command -v sox)" ]; then echo -e "INSTALLING SOX...\n\n" sudo apt-get install sox sudo apt-get install sox libsox-fmt-all echo -e "\n\n" fi # download the noti sound if this is first time # add alias to the bashrc file if ! [ -f ~/noti/sound.mp3 ]; then echo -e "DOWNLOADING SOUND...\n\n" touch ~/noti/sound.mp3 | wget -O ~/noti/sound.mp3 "https://notificationsounds.com/wake-up-tones/rise-and-shine-342/download/mp3" sudo echo "alias noti=\"sh ~/noti/noti.sh\"" >> ~/.bashrc source ~/.bashrc echo -e "\n\n" fi # notify with the sound playing and particular given message and time echo "notify-send \""$MSSG\"" && play ~/noti/sound.mp3" | at $TIME
如何使用?
首次运行 – 设置:
-
在家里创建一个新目录并将其称为
noti
mkdir ~/noti
-
下载noti.sh并将其提取到上面的noti目录。
-
打开终端并将目录更改为
noti
cd ~/noti
-
通过发出以下命令使noti.sh可执行:
sudo chmod +x noti.sh
-
像这样运行测试:
sh ~/noti/noti.sh "Test" "now"
例子
noti "Hello From Noti" "now +1 minute" noti "Hello From Noti" "now +5 minutes" noti "Hello From Noti" "now + 1 hour" noti "Hello From Noti" "now + 2 days" noti "Hello From Noti" "4 PM + 2 days" noti "Hello From Noti" "now + 3 weeks" noti "Hello From Noti" "now + 4 months" noti "Hello From Noti" "4:00 PM" noti "Hello From Noti" "2:30 AM tomorrow" noti "Hello From Noti" "2:30 PM Fri" noti "Hello From Noti" "2:30 PM 25.07.18"
用于通知流程结束(示例)
sudo apt-get update; noti "Done" "now"