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

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

大多数应用程序都可以在屏幕右上角显示格式化良好的事件通知。我即将编写一个 bash 脚本,该脚本将在后台执行相当长的处理,我真的想知道它何时完成。我如何才能从 bash 脚本中显示该通知?

答案1

如果你正在使用 Jaunty 中的新通知系统,那么你需要通知发送命令

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.

答案2

找到了另一种方法,通过禅意

echo 'message:hi' | zenity --notification --listen

或者像这样:

zenity --notification --text "System update necessary!" 

(这还具有已安装在 Ubuntu 上的好处。)

答案3

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

  1. [效果良好] 弹出通知会在 4 至 10 秒后自动关闭(是否与您的操作系统设置相关?):

    notify-send "Hello world"
    

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

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

    1. 窗口无法自动对焦: 资料来源:我自己;注意:对于基于 Unity 的旧版 Ubuntu,例如 16.04,-t 除 0 之外的所有值都会被忽略——多么愚蠢。:(。对于基于 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"
      

      在此处输入图片描述

      在 Ubuntu 18.04 或 20.04 或更高版本中,只需添加-u critical到命令即可一直保持打开状态,直到您单击其任意位置!

      notify-send -u critical "Hello world"
      

      在此处输入图片描述
      来源:@lucidbrot 在这个答案下面的评论,加上我自己的测试。

    2. 或者窗口确实可以自动对焦:

      zenity --info --title "Hello" --text "World"
      

      注意:除非您单击该按钮,否则窗口不会关闭OK
      在此处输入图片描述
      来源:https://askubuntu.com/a/804475/327339

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

    zenity --info --title "Hello" --text "World" --timeout=2
    

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

  4. 【超级丑】

    xmessage 'hello world'
    

    注意:除非您单击该按钮,否则窗口不会关闭okay
    在此处输入图片描述
    来源:http://www.linux-commands-examples.com/xmessage

播放声音

  1. 如果您还想播放声音以及窗口弹出来表示命令或某件事的完成,请参阅我的其他答案:
    1. AskUbuntu.com:如何在某个过程完成后发出声音?
    2. 我的“Bash 单行警报脚本”在这里:询问 Ubuntu:Ubuntu 的闹钟。在其中,我使用了zenity弹出窗口。

也可以看看

  1. 我的《Bash 单行报警脚本》从命令行,以及alarm_timer <seconds> <alarm message>那里的我的自定义函数。

答案4

还有短信这将弹出一个窗口,因此它可以在任何 X11 系统上运行。

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

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

相关内容