有没有办法在 Ubuntu 中显示来自 bash 脚本的通知?

vav*_*ava 127 linux notifications ubuntu

大多数应用程序可以对出现在屏幕右上角的事件显示格式良好的通知。我即将编写一个 bash 脚本,它将在后台进行长时间的处理,我真的很想知道它什么时候完成。如何显示来自 bash 脚本的漂亮通知?

小智 148

如果您在 Jaunty 中使用新的通知系统,您需要notify-send命令

notify-send - a program to send desktop notifications

SYNOPSIS

With notify-send you can sends desktop notifications to the user via
a notification daemon from the command line.  These notifications can be
used to inform the user about an event or display some form of information
without getting in the user's way.

OPTIONS

-u, --urgency=LEVEL
Specifies the urgency level (low, normal, critical).

-t, --expire-time=TIME
    Specifies the timeout in milliseconds at which to expire the notification.
-i, --icon=ICON[,ICON...]
    Specifies an icon filename or stock icon to display.
-c, --category=TYPE[,TYPE...]
    Specifies the notification category.
Run Code Online (Sandbox Code Playgroud)

  • 例如:通知发送测试“Hello World” (6认同)
  • 谢谢,我自己才找到它:) apt-get install libnotify-bin 必须先运行才能得到它。 (4认同)
  • 由于此“功能”(错误), --expire-time 参数在 Ubuntu 上不起作用。https://bugs.launchpad.net/ubuntu/+source/notify-osd/+bug/390508 (3认同)
  • 我发现“通知发送”在视频/音频播放期间被禁止。虽然这是一个有效的用例,但如果您仍然想看到它们,则必须添加 **--urgency=ritic**。 (2认同)
  • 要使其弹出一个带有“确定”和“取消”按钮的窗口,请使用 `notify-send --expire-time=0 "Hello World"` 或 `notify-send -t 0 "Hello world"` (2认同)

vav*_*ava 40

找到了另一种方式,通过Zenity

echo 'message:hi' | zenity --notification --listen
Run Code Online (Sandbox Code Playgroud)

或者像这样:

zenity --notification --text "System update necessary!" 
Run Code Online (Sandbox Code Playgroud)

(这也有已经安装在 Ubuntu 上的好处。)


Gab*_*les 15

在 Ubuntu 14.04、16.04、18.04、20.04 上测试。来自 Ubuntu 20.04 的屏幕截图。

  1. [运作良好] 4~10 秒后自动关闭的弹出通知(以某种方式与您的操作系统设置相关联?):

    notify-send "Hello world"
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明
    来源:https : //superuser.com/a/31919/425838

  2. 带有单击按钮的弹出窗口:

    1. 窗口没有自动对焦: 来源:我自己;注意:对于基于 Unity 的较旧版本的 Ubuntu,例如 16.04,-t 对于除 0--how 愚蠢之外的所有值都将被忽略。:(。对于较新的基于 Gnome 的 Ubuntu 版本,例如 18.04 或 20.04,-t完全被忽略。因此,在较旧的基于 Unity 的 Ubuntu 版本(例如 16.04)上,使用-t 0原因按钮显示,但在较新的基于 Gnome 的版本,它没有。这意味着对于下面显示的 Ubuntu 20.04 屏幕截图,行为和外观notify-send -t 0 "Hello world"notify-send "Hello world"上面完全相同。

      notify-send -t 0 "Hello world"
      
      Run Code Online (Sandbox Code Playgroud)

      在此处输入图片说明

      在 Ubuntu 18.04 或 20.04 或更高版本上,只需添加-u critical到命令中即可让它无限期地保持打开状态,直到您单击它的任意位置!

      notify-send -u critical "Hello world"
      
      Run Code Online (Sandbox Code Playgroud)

      在此处输入图片说明
      来源:@lucidbrot 在此答案下方的评论,以及我自己的测试。

    2. 窗口确实获得自动对焦:

      zenity --info --title "Hello" --text "World"
      
      Run Code Online (Sandbox Code Playgroud)

      注意:在您单击OK按钮之前,窗口不会关闭。
      在此处输入图片说明
      来源:https : //askubuntu.com/a/804475/327339

  3. [我的最爱] 窗口在指定--timeout的秒数后自动关闭,或者在您单击“确定”按钮后!

    zenity --info --title "Hello" --text "World" --timeout=2
    
    Run Code Online (Sandbox Code Playgroud)

    注意:窗口将在上面指定的超时时间后自动关闭,以秒为单位!
    在此处输入图片说明
    来源:我自己阅读手册页: man zenity

  4. 【超级难看】

    xmessage 'hello world'
    
    Run Code Online (Sandbox Code Playgroud)

    注意:在您单击okay按钮之前,窗口不会关闭。
    在此处输入图片说明
    来源:http : //www.linux-commands-examples.com/xmessage

也播放声音

  1. 如果您也想播放声音以及弹出窗口,以表示命令或其他内容的完成,请在此处查看我的其他答案:AskUbuntu.com:如何在进程完成后发出声音?


kol*_*pto 13

对于 KDE 用户:

$ kdialog --title "Long process completed!" --passivepopup "This popup will disappear in 5 seconds" 5 &
Run Code Online (Sandbox Code Playgroud)


NVR*_*RAM 12

还有xmessage会弹出一个窗口,所以它应该适用于任何 X11 系统。

优点:它还允许使用按钮以交互方式提示用户。

缺点:像任何弹出式警报一样,它通常会获得焦点,因此如果您正在输入中,它可能会在您阅读消息之前消失。

  • 缺点:它看起来非常丑陋,而且是一个超小窗口,对用户来说并不总是很明显。无论如何,它是通用的。:) (5认同)