15

当我们运行诸如 source compile 或 brew upgrade 之类的命令时,我们通常会做其他事情,例如网络搜索,而不是一直观察命令运行。

命令完成后有什么可能的方法可以通知我吗?

brew upgrade | afplay /System/Library/Sounds/Submarine.aiff -v 10

我在Mac上尝试了afplay,但问题是a)由于管道而没有显示stdout,b)它在命令开始运行后立即播放声音,而不是等到“brew升级”完成。

请问有什么建议吗?

4

8 回答 8

23

brew upgrade && say brew upgrade done

利用say命令并获得自定义语音通知

一般示例:

YOUR_COMMAND && say YOUR_COMMAND done

于 2016-11-22T18:13:20.143 回答
17

我想你需要

brew upgrade && afplay /System/Library/Sounds/Submarine.aiff -v 10

如果 brew 成功完成,则 afplay 将运行。

作为解释说明,您要求外壳程序确定两个语句(“brew”和“afplay”)是否为真 - 因此有义务在执行之前完全评估“brew”语句(即等待它完成) “afplay”,以确定这两个陈述是否正确。

于 2013-11-09T15:53:58.313 回答
12

Thanks Mark. You gave me a great idea.

I haven't noticed &&, || can mean logical operators on shell also. It appears '&& afplay ..' works only when 'brew upgrade' returns 0 which happens most of the time, and '|| afplay ..' works only when 'brew upgrade' returns 1 (or any non-zero) meaning error. I decided to use semicolon ';' so afplay can play sound alarm whatever 'brew upgrade' reterns after job done.

brew upgrade ; afplay /System/Library/Sounds/Submarine.aiff -v 10

EDIT: Now I use the following code in .profile (or .bashrc), which utilizes the advanced notifier tool, terminal-notifier.

function brew { caffeinate -s brew $@; terminal-notifier -title 'Homebrew' -subtitle 'Finished' -message brew' '$1' '$2' '$3 -sound 'recv_mail' -contentImage '/Download/Any/Icon/beer_icon.png';}

Please note:

  1. There must be a whitespace after the left brace '{' for bash syntax.
  2. caffeinate is used to prevent Mac from falling into sleep mode while brew works for a long time. If you remove 'caffeinate -s', you should rename the function name also.
  3. You can install terminal-notifier with brew. -contentImage is optoinal of course.
于 2013-11-12T01:04:54.303 回答
3

要直接回答这个问题,无需安装任何额外的东西,只需系统警报即可完成。

运行“命令”后播放系统提示音:

command; printf "\a"

Giorgio 的回答也不错,如"

command; say command is done

喜欢连续报警的想法;我会做这样的事情来每 10 秒重复一次警报:

command; while :; sleep 10; do say command is done; done

或者

command; while :; sleep 2; do printf "\a"; done
于 2020-02-19T13:49:34.447 回答
2

这是我添加到我的一个小功能.zshrc

noti() {
  if [[ $? = 0 ]]; then
    say OK
  else
    say error
  fi
}

从 shell执行some command ; noti将根据命令结果播放“OK”或“错误”。

我发现它是一种更好的替代方法,&&因为它会同时通知成功和错误。

于 2019-10-16T12:31:22.270 回答
2
sleep 30m; for i in {1..10}; do afplay /System/Library/Sounds/Purr.aiff -v 10; done

OP要求报警。此代码以最大音量重复声音 Purr.aiff 10 次,以确保您能听到它(如闹钟)。替换sleep30m为您想要的命令。如果您想要更长的时间,请10在 for 循环中替换为10000.

如果您想要无限警报(按CTRL-C退出),请尝试:

sleep 30m; while :; do afplay /System/Library/Sounds/Purr.aiff -v 10; done

如果您想要其他声音,您可以尝试替换Purr.aiff为:

  • 巴索.aiff
  • 项目清单
  • 青蛙.aiff
  • 英雄.aiff
  • 流行音乐
  • 潜艇.aiff
  • 吹嘘
  • Funk.aiff
  • 莫尔斯艾夫
  • Tink.aiff
  • 瓶子.aiff
  • Glass.aiff
  • ping.aiff
  • Sosumi.aiff

Purr.aiff并且Blow.aiff是我个人的最爱。

于 2018-03-07T04:52:50.763 回答
1
  1. ~/.bash_profile在您的(或等效的)中创建一个别名。

    # Play sound
    alias notify='afplay /System/Library/Sounds/Submarine.aiff -v 10'
    
  2. 来源您的~/.bash_profile.

    source ~/.bash_profile
    
  3. 运行需要很长时间的命令。您可以notify在同一行使用别名,也可以notify在命令已经开始运行后键入。

    brew update; notify
    
    # or
    
    brew update
    # oops, I forgot to add "; notify". No worries, just type it now
    notify
    

    一旦brew update运行完毕,notify就会执行,播放声音。

于 2021-08-27T18:49:46.717 回答
0

旧线程,但在 macOS Catalina 上适用于我的简单本机命令:

$ echo -e "\007"

或附加到命令:

$ long_running_program && echo -e "\007"

这实质上与 BEL(贝尔)ASCII 字符相呼应,该字符会发出原生 macOS 终端通常发出的哔声。

于 2020-06-22T22:10:20.717 回答