Created
July 15, 2025 07:04
-
-
Save JasonkayZK/c32cc4af8582b12d785619f6c999999f to your computer and use it in GitHub Desktop.
一个可以发送系统通知的脚本。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # 帮助函数 | |
| show_help() { | |
| echo "Usage: $0 [Option] [message]" | |
| echo "Show system notification" | |
| echo | |
| echo "Option:" | |
| echo " -h, --help Show help info" | |
| echo " -t, --title TITLE Set notification title (Default: Notify)" | |
| exit 0 | |
| } | |
| # 默认值 | |
| title="Notify" | |
| # 解析命令行参数 | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) | |
| show_help | |
| ;; | |
| -t|--title) | |
| if [[ -z "$2" ]]; then | |
| echo "错误: --title 需要一个参数" >&2 | |
| exit 1 | |
| fi | |
| title="$2" | |
| shift 2 | |
| ;; | |
| *) | |
| # 第一个非选项参数视为消息内容 | |
| message="$1" | |
| shift | |
| break | |
| ;; | |
| esac | |
| done | |
| # 如果没有提供消息内容,则从标准输入读取 | |
| if [[ -z "$message" ]]; then | |
| if [[ -t 0 ]]; then | |
| echo "错误: 没有提供消息内容" >&2 | |
| show_help | |
| exit 1 | |
| else | |
| message=$(cat) | |
| fi | |
| fi | |
| # 发送通知 | |
| case "$(uname -s)" in | |
| Darwin) | |
| # macOS 使用 AppleScript | |
| osascript -e "display notification \"$message\" with title \"$title\"" | |
| ;; | |
| Linux) | |
| # Linux 使用 notify-send (libnotify) | |
| if command -v notify-send &>/dev/null; then | |
| notify-send "$title" "$message" | |
| else | |
| echo "错误: 未找到 notify-send 命令。请安装 libnotify 包。" >&2 | |
| exit 1 | |
| fi | |
| ;; | |
| *) | |
| echo "错误: 不支持的操作系统" >&2 | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment