Skip to content

Instantly share code, notes, and snippets.

@JasonkayZK
Created July 15, 2025 07:04
Show Gist options
  • Select an option

  • Save JasonkayZK/c32cc4af8582b12d785619f6c999999f to your computer and use it in GitHub Desktop.

Select an option

Save JasonkayZK/c32cc4af8582b12d785619f6c999999f to your computer and use it in GitHub Desktop.
一个可以发送系统通知的脚本。
#!/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