mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
commit
4475afe37b
@ -7,39 +7,39 @@
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Send desktop notifications and reminders from Linux terminal
|
||||
从 Linux 终端发送桌面通知与提醒
|
||||
======
|
||||
This Linux tutorial demonstrates how to use script commands to send
|
||||
yourself desktop notifications and reminders.
|
||||
本 Linux 教程演示如何使用脚本命令来发送
|
||||
自己的桌面通知与提醒。
|
||||
![Person using a laptop][1]
|
||||
|
||||
Sometimes it's useful to get visual feedback from a script. For example, when a script or cron job completes, a long-running build fails, or there is an urgent problem during script execution. Desktop applications can do this with popup notifications, but it can be done from a script too! You can use script commands to send yourself desktop notifications and reminders.
|
||||
有时候,来自脚本的视觉回馈是很有用的。例如,当一个脚本或计划任务完成时,一个长期运行的构建任务失败时,或者当脚本执行中出现了紧急问题时。桌面应用程序可以通过弹出通知来做到这一点,但脚本也可以做到这一点!你可以使用脚本命令来给自己发送桌面通知与提醒。
|
||||
|
||||
![Example notification][2]
|
||||
|
||||
(Tomasz Waraksa, CC BY-SA 4.0)
|
||||
|
||||
The below code has been written and tested on Linux. It can also be done on macOS with a bit of effort. See the last section for some [hints and tips][3].
|
||||
下面的代码是在 Linux 上编写和测试的。它也可以在 macOS 上运行,只需花点功夫。请参见最后一节 [提示与技巧][3]。
|
||||
|
||||
### Sending notifications from the Linux terminal
|
||||
### 从 Linux 终端发送通知
|
||||
|
||||
To send notifications from the Linux terminal, use the [`notify-send`][4] command. Run `which ``notify-send` to see if it's present on your system. If not, install it with your package manager of choice.
|
||||
要从 Linux 终端发送通知,请使用 [`notify-send`][4] 命令。运行 `which ``notify-send` 命令来查看它是否在于你的系统中。如果没有,请使用包管理器来安装它。
|
||||
|
||||
On Fedora, type:
|
||||
在 Fedora 上,输入:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo dnf install notify-send`
|
||||
```
|
||||
|
||||
On Debian-based distributions, type:
|
||||
在基于 Debian 的发行版上,输入:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo apt install notify-send`
|
||||
```
|
||||
|
||||
A few examples of simple notifications:
|
||||
几个简单的通知示例:
|
||||
|
||||
|
||||
```
|
||||
@ -50,7 +50,7 @@ $ notify-send "Tip of the Day" "How about a nap?"
|
||||
|
||||
```
|
||||
|
||||
You can customize the notification with options such as urgency level, custom icon, etc. Find out more with `man notify-send`. You can use a small set of HTML tags in the notification body to give your messages a nice touch. On top of that, URLs are rendered as clickable. For example:
|
||||
你可以用紧急程度、自定义图标等选项来自定义通知。过 `man notify-send` 了解更多。你也可以在通知正文中使用一小组 HTML 标记,以使消息有一个棒的视觉感受。最重要的是,URL 被呈现为可点击的。例如:
|
||||
|
||||
|
||||
```
|
||||
@ -66,18 +66,18 @@ $ notify-send -u critical \
|
||||
|
||||
(Tomasz Waraksa, CC BY-SA 4.0)
|
||||
|
||||
Sent notifications are picked up by the desktop environment and displayed just like any other notification. They will have the same consistent look, feel, and behavior.
|
||||
发送的通知会被桌面环境接收,并像其他通知一样显示。它们将具有相同的外观、交互和行为。
|
||||
|
||||
### Combine notify-send with at
|
||||
### 将 notify-send 与 at 结合使用
|
||||
|
||||
Cron is commonly used to schedule commands at regular intervals. The `at` command schedules the single execution of a command at a specified time. If you run it like this, it starts in interactive mode, where you can enter commands to execute at a given time:
|
||||
计划任务通常被用来定期安排命令。`at` 命令安排在一个指定的时间执行一条命令。 如果你像这样运行它,它会以交互模式启动,你可以在其中输入要在指定时间执行的命令:
|
||||
|
||||
|
||||
```
|
||||
`$ at 12:00`
|
||||
```
|
||||
|
||||
This isn't useful for scripts. Luckily, `at` accepts parameters from standard input so that we can use it this way:
|
||||
这对脚本来说并不有用。幸运的是, `at` 接受来自标准输入的参数,所以我们可以这样使用它:
|
||||
|
||||
|
||||
```
|
||||
@ -88,7 +88,7 @@ $ echo "backup-db" | at 13:00
|
||||
|
||||
```
|
||||
|
||||
There are many ways of specifying time. From absolute time, such as `10:00` through relative time, such as `now + 2 hours`, to special times such as `noon` or `midnight`. We can combine it with `notify-send` to show ourselves reminders at some time in the future. For example:
|
||||
有许多指定时间的方法。 从绝对时间,如 `10:00`,到相对时间,如 `now + 2 hours` ,再特殊时间,如`noon` 或 `midnight`。我们可以把它和 `notify-send` 结合起来,在未来的某个时间向自己发送提醒。例如:
|
||||
|
||||
|
||||
```
|
||||
@ -99,9 +99,9 @@ There are many ways of specifying time. From absolute time, such as `10:00` thro
|
||||
|
||||
(Tomasz Waraksa, CC BY-SA 4.0)
|
||||
|
||||
### The remind command
|
||||
### 提醒的命令
|
||||
|
||||
Now, build a custom Bash command for sending yourself reminders. How about something as simple and human-friendly as:
|
||||
现在,建立一个自定义的 Bash 命令来给自己发送提醒信息。像这样简单且人性化的命令:
|
||||
|
||||
|
||||
```
|
||||
@ -115,18 +115,18 @@ $ remind "It's Friday pints time!" at 17:00
|
||||
|
||||
```
|
||||
|
||||
This is better than Alexa! How to get this goodness?
|
||||
这比 Alexa 更好!该怎样做?
|
||||
|
||||
See the code below. It defines a shell function called **remind**, which supports the above syntax. The actual work is done in the last two lines. The rest is responsible for help, parameter validation, etc., which roughly matches the proportion of useful code vs. necessary white-noise in any large application.
|
||||
请看下面的代码。它定义了一个名为 **remind** 的函数,它支持上述语法。实际工作是在最后两行完成的。其余的部分负责显示帮助信息、参数校验等,这与任何大型应用程序中有用的代码与必要的白噪声的比例大致相同。
|
||||
|
||||
Save the code somewhere, for example, in the `~/bin/remind` file, and source the function in your `.bashrc` profile so that it's loaded when you log in:
|
||||
把代码保存在某个地方,例如,在 `~/bin/remind` 文件中,并在你的 `.bashrc` 配置文件写入该函数,以便在你登录时加载它:
|
||||
|
||||
|
||||
```
|
||||
`$ source ~/bin/remind`
|
||||
```
|
||||
|
||||
Reload the terminal, then type remind to see the syntax. Enjoy!
|
||||
重新打开终端,然后输入 remind 来查看语法。尽情享受吧!
|
||||
|
||||
|
||||
```
|
||||
@ -195,13 +195,13 @@ function remind () {
|
||||
|
||||
```
|
||||
|
||||
### Easy notifications
|
||||
### 简单的提醒
|
||||
|
||||
With these few simple open source commands, you can integrate your own scripts, applications, and tasks with your desktop. Try it out!
|
||||
通过这几个简单的开源命令,你可以将你自己的脚本、应用程序和任务与你的桌面结合起来。试一试吧!
|
||||
|
||||
* * *
|
||||
|
||||
_This article has been adapted with the author's permission from the original article, found [here][7]._
|
||||
_本文经作者许可改编自原文,详情见[此处][7]。_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -209,7 +209,7 @@ via: https://opensource.com/article/22/1/linux-desktop-notifications
|
||||
|
||||
作者:[Tomasz Waraksa][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[mcfd](https://github.com/mcfd)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user