在 Linux 中從 Shell 指令碼顯示 GUI 通知


在 Linux 中,有多種方法可以從 shell 指令碼顯示 GUI 通知,但一種常見的方法是使用 notify-send 命令。此命令是 libnotify 庫的一部分,通常預安裝在大多數 Linux 發行版上。

以下是如何使用 notify-send 顯示標題為“Hello”、訊息為“World”的通知的示例:

notify-send "Hello" "World"

您只需將此命令新增到 shell 指令碼中即可在指令碼中使用它,或者將通知文字賦值給一個變數並使用它。

message="This is your reminder"
notify-send "Reminder" "${message}"

此外,您可以使用各種選項自定義通知的外觀和行為,例如指定圖示、設定緊急級別或控制通知持續時間。

使用 notify-send 命令

notify-send 命令用於從命令列或指令碼顯示 GUI 通知。該命令的基本語法如下:

notify-send [options] <title> <message>

其中 <title> 是通知的標題,<message> 是通知的正文文字。

以下是一些您可以與 notify-send 命令一起使用的常見選項:

-u, --urgency=LEVEL - 指定通知的緊急級別。LEVEL 可以是“low”(低)、“normal”(普通)、“critical”(嚴重)之一。

-t, --expire-time=TIME - 指定通知的持續時間(以毫秒為單位)。

-i, --icon=ICON - 指定要與通知一起使用的圖示。

-c, --category=TYPE - 指定通知型別(例如,“email”(郵件)、“im”(即時訊息))。

-h, --hint=TYPE:NAME:VALUE - 指定要傳遞給通知伺服器的提示。

您可以使用以下示例:

notify-send -u critical -t 5000 -i /path/to/icon.png "Title" "The message"

此命令將顯示一個嚴重通知,該通知將在 5 秒後過期,並使用位於 /path/to/icon.png 的圖示。

從 Shell 指令碼傳送通知

您可以像從命令列一樣,使用 notify-send 命令從 shell 指令碼傳送通知。以下是一個簡單的 shell 指令碼示例,它傳送一個標題為“Script Complete”(指令碼完成)且訊息為“The script has finished running”(指令碼已完成執行)的通知:

#!/bin/bash

# your script commands go here

# send the notification
notify-send "Script Complete" "The script has finished running"

您還可以使用變數來儲存通知標題和訊息,然後將它們傳遞給 notify-send 命令。

#!/bin/bash

# your script commands go here

# define the title and message
title="Script Complete"
message="The script has finished running"

# send the notification
notify-send "$title" "$message"

您還可以傳送通知以告知特定任務的完成或失敗,例如當任務具有特定狀態時:

#!/bin/bash

# your script commands go here

# Define the title and message
title="Task Failed"
message="The task has failed to run"

if task_status -eq 0; then
  title="Task Complete"
  message="The task has been completed successfully"
  notify-send -u normal "$title" "$message"
else
  notify-send -u critical "$title" "$message"
fi

此指令碼將檢查任務的狀態。如果任務成功完成,它將傳送一個普通通知,標題為“Task Complete”(任務完成),訊息為“The task has been completed successfully”(任務已成功完成),否則它將傳送一個嚴重通知,標題為“Task Failed”(任務失敗),訊息為“The task has failed to run”(任務執行失敗)。

使用 zenity 實用程式

在 Linux 中,從 shell 指令碼顯示 GUI 通知另一種方法是使用 zenity 實用程式。zenity 是一個命令列工具,允許您在命令列和 shell 指令碼中顯示 GTK+ 對話方塊。它可用於顯示各種對話方塊,包括警報、錯誤訊息和詢問提示。

以下是如何使用 zenity 顯示標題為“Hello”、訊息為“World”的通知的示例:

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

您還可以使用 --warning、--error、--question 選項代替 --info 來顯示不同型別的通知。--title 選項設定對話方塊的標題,--text 選項設定對話方塊的文字。

您還可以使用 zenity 顯示帶有進度條的通知,例如:

(for i in {1..100}; do echo $i; sleep 0.1; done) | zenity --progress --title="Title" --text="Your process is running" --auto-close --auto-kill

此命令將顯示一個進度條,該進度條在達到 100% 後關閉並自行終止。

您可以在 shell 指令碼中以與從命令列使用相同的方式使用 zenity 命令,只需將其新增到指令碼中並傳遞任何必要的選項和引數即可。

從 Shell 指令碼顯示對話方塊和通知

zenity 是一個命令列工具,可用於從 shell 指令碼在圖形使用者介面 (GUI) 中顯示各種型別的對話方塊和通知。

以下是一些您可以使用 zenity 顯示的不同型別的對話方塊和通知的示例:

資訊對話方塊:

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

警告對話方塊:

zenity --warning --title="Warning" --text="This is a warning message"

錯誤對話方塊:

zenity --error --title="Error" --text="An error has occurred"

詢問對話方塊:

zenity --question --title="Question" --text="Do you want to continue?"

文字輸入對話方塊:

zenity --entry --title="Enter your name" --text="Please enter your name"

檔案選擇對話方塊:

zenity --file-selection --title="Select a file" --file-filter='*.txt'

列表對話方塊

zenity --list --title="Select an option" --column "Option" --column "Description"  --print-column=2 "Option 1" "This is the first option" "Option 2" "This is the second option" "Option 3" "This is the third option"

您只需將 zenity 命令新增到指令碼中並傳遞任何必要的選項和引數即可在 shell 指令碼中使用它。

您還可以透過將其儲存在變數中來在指令碼中使用 zenity 命令的輸出,例如:

response=$(zenity --question --title="Question" --text="Do you want to continue?")
if [ "$response" = "0" ]; then
  # user clicked OK
else
  # user clicked Cancel
fi

請記住,zenity 需要在系統中安裝 GTK+ 庫,某些精簡發行版中可能不存在,在這種情況下,您可能需要手動安裝它。

結論

有多種方法可以從 Linux 中的 shell 指令碼顯示 GUI 通知和對話方塊,包括使用 notify-send 命令和 zenity 實用程式。notify-send 是一個可用於傳送桌面通知的命令列工具,它通常預安裝在大多數 Linux 發行版上。另一方面,zenity 是一個可用於在命令列和 shell 指令碼中顯示 GTK+ 對話方塊的命令列工具,它提供了比 notify-send 更多的選項,並且對於顯示各種型別的對話方塊和通知很有用。

更新於:2023 年 1 月 24 日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告