自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用終端工具給你的電腦發(fā)送彈窗提醒!

開發(fā) 開發(fā)工具
現(xiàn)在人手一部智能手機,這些智能手機都有個非常實用的功能,那就是彈窗提醒。當(dāng)我們收到短信,或者微信信息時,手機就會彈窗顯示信息的大致內(nèi)容。有了這個功能你就不會錯過重要信息了。

大家好,我是良許。

現(xiàn)在人手一部智能手機,這些智能手機都有個非常實用的功能,那就是彈窗提醒。當(dāng)我們收到短信,或者微信信息時,手機就會彈窗顯示信息的大致內(nèi)容。有了這個功能你就不會錯過重要信息了。

電腦上也有類似的功能,也很實用。但這個功能都是系統(tǒng)級別,我們能不能通過腳本方式去調(diào)用這個彈窗功能呢?

答案是肯定的!

例如,當(dāng)腳本或 cron 任務(wù)完成時,長時間運行的編譯任務(wù)失敗,或者腳本執(zhí)行過程中出現(xiàn)緊急問題,這些情況下如果能在電腦上彈出一條提醒,肯定會讓隔壁的美女同事刮目相看!

以下代碼已在 Linux 系統(tǒng)上編寫并測試通過,也可以移植到 Mac 電腦上。

從 Linux 終端發(fā)送彈窗通知

要從 Linux 終端發(fā)送通知,需要使用 notify-send 命令。這個命令大部分發(fā)行版都沒有默認安裝,需要我們自行動手。

在 Fedora 上,輸入:

  1. $ sudo dnf install notify-send 

在基于 Debian 的發(fā)行版上,鍵入:

  1. $ sudo apt install notify-send 

幾個簡單彈窗通知的例子:

  1. $ notify-send "liangxu is great!!" 
  2. $ notify-send "welcome to liangxu's website" "www.lxlinux.net" 

這個命令不僅支持彈窗,還可以修改緊急程度、自定義圖標(biāo)等。更多信息可以通過 man notify-send 來查詢。

你還可以在通知正文中使用一小段 HTML 標(biāo)記來為你的信息增加一些格式,比如:加粗、斜體,等等。最重要的是,URL 還支持點擊,非常方便。例如:

  1. $ notify-send -u critical \ 
  2.   "Build failed!" \ 
  3.   "There were <b>123</b> errors. Click here to see the results: http://buildserver/latest" 

發(fā)送的通知跟系統(tǒng)的其它通知樣式一樣,外觀、行為并無二致。

結(jié)合 at 命令使用 notify-send

cron 命令通常用于定期調(diào)度任務(wù),at 命令則是在指定時間單次執(zhí)行指定命令。如果你像下面這樣運行 at 命令,它會以交互模式啟動,然后你可以在其中輸入你要執(zhí)行的命令:

  1. at 12:00 

但我們一般不這么使用它。

at 命令可以接受來自標(biāo)準(zhǔn)輸入的參數(shù),例如:

  1. $ echo "npm run build" | at now + 1 minute 
  2. $ echo "backup-db" | at 13:00 

熟練使用 Linux 的小伙伴都知道,我們有多種指定時間的方法。

  • 絕對時間,例如 10:00
  • 相對時間,例如 now + 2 hours
  • 特殊時間,例如 noon 或 midnight

利用 at 命令的這些特性,我們可以將它與 notify-send 命令結(jié)合使用,達到在未來的某個時間彈窗提醒的效果。例如:

  1. $ echo "notify-send 'Stop it and go home now?' 'Enough work for today.' -u critical" | at now 

編寫腳本實現(xiàn)彈窗通知功能

現(xiàn)在我們知道 nofity-send 怎么玩了,但每次都要敲這么長的一串命令還是很不方便。

作為程序員,我們能偷懶就偷懶,自己動手寫腳本把這個功能封裝起來!

比如我們把它封裝成一個 Bash 命令 remind ,然后通過下面方式來調(diào)用它:

  1. $ remind "I'm still here" now 
  2. $ remind "Time to wake up!" in 5 minutes 
  3. $ remind "Dinner" in 1 hour 
  4. $ remind "Take a break" at noon 
  5. $ remind "It's Friday pints time!" at 17:00 

簡直太特么方便了!

實現(xiàn)起來也很簡單,我們可以將腳本保存在某個位置,例如,在 ~/bin/ 目錄中,并在 .bashrc 配置文件中讓它生效,以便在登錄時加載它:

  1. $ source ~/bin/remind 

腳本內(nèi)容如下:

  1. #!/usr/bin/env bash 
  2. function remind () { 
  3.   local COUNT="$#" 
  4.   local COMMAND="$1" 
  5.   local MESSAGE="$1" 
  6.   local OP="$2" 
  7.   shift 2 
  8.   local WHEN="$@" 
  9.   # Display help if no parameters or help command 
  10.   if [[ $COUNT -eq 0 || "$COMMAND" == "help" || "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then 
  11.     echo "COMMAND" 
  12.     echo "    remind <message> <time>" 
  13.     echo "    remind <command>" 
  14.     echo 
  15.     echo "DESCRIPTION" 
  16.     echo "    Displays notification at specified time" 
  17.     echo 
  18.     echo "EXAMPLES" 
  19.     echo '    remind "Hi there" now' 
  20.     echo '    remind "Time to wake up" in 5 minutes' 
  21.     echo '    remind "Dinner" in 1 hour' 
  22.     echo '    remind "Take a break" at noon' 
  23.     echo '    remind "Are you ready?" at 13:00' 
  24.     echo '    remind list' 
  25.     echo '    remind clear' 
  26.     echo '    remind help' 
  27.     echo 
  28.     return 
  29.   fi 
  30.   # Check presence of AT command 
  31.   if ! which at >/dev/nullthen 
  32.     echo "remind: AT utility is required but not installed on your system. Install it with your package manager of choice, for example 'sudo apt install at'." 
  33.     return 
  34.   fi 
  35.   # Run commands: list, clear 
  36.   if [[ $COUNT -eq 1 ]]; then 
  37.     if [[ "$COMMAND" == "list" ]]; then 
  38.       at -l 
  39.     elif [[ "$COMMAND" == "clear" ]]; then 
  40.       at -r $(atq | cut -f1) 
  41.     else 
  42.       echo "remind: unknown command $COMMAND. Type 'remind' without any parameters to see syntax." 
  43.     fi 
  44.     return 
  45.   fi 
  46.   # Determine time of notification 
  47.   if [[ "$OP" == "in" ]]; then 
  48.     local TIME="now + $WHEN" 
  49.   elif [[ "$OP" == "at" ]]; then 
  50.     local TIME="$WHEN" 
  51.   elif [[ "$OP" == "now" ]]; then 
  52.     local TIME="now" 
  53.   else 
  54.     echo "remind: invalid time operator $OP" 
  55.     return 
  56.   fi 
  57.   # Schedule the notification 
  58.   echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at $TIME 2>/dev/null 
  59.   echo "Notification scheduled at $TIME" 

 

好好玩玩吧!

 

責(zé)任編輯:武曉燕 來源: 良許Linux
相關(guān)推薦

2022-07-14 15:00:53

Linux 終端通知腳本命令

2016-11-09 10:01:04

軟考備考IT行業(yè)

2023-01-30 16:21:24

Linux外觀

2021-08-11 14:34:10

Linux文件管理器

2021-03-01 13:11:20

duf終端工具Linux

2021-03-13 21:00:30

電腦PC電腦彈窗廣告

2021-03-02 08:49:53

tmuxLinux命令

2011-08-21 09:07:10

2024-01-05 12:03:37

終端工具?tmux

2021-08-05 13:20:46

Python工具工具

2021-04-01 10:22:42

工具Linux文件

2022-02-08 15:37:22

微軟Windows 11

2021-08-01 22:59:16

Python工具開發(fā)

2024-06-03 07:01:41

微軟Windows 10Windows 11

2011-02-14 16:47:48

華為平板電腦

2024-02-06 10:06:32

Windows 11Windows 10微軟

2014-06-18 10:47:05

dstat監(jiān)控工具

2021-05-18 12:02:03

GoTTY終端工具Web

2021-01-27 13:16:39

ScreenLinux命令

2021-02-16 10:58:50

ScreenLinux命令
點贊
收藏

51CTO技術(shù)棧公眾號