Skip to content

Instantly share code, notes, and snippets.

@LiuJiewenTT
Created December 4, 2025 01:28
Show Gist options
  • Select an option

  • Save LiuJiewenTT/403fcce75e31bfba3c8e9b82bae378ee to your computer and use it in GitHub Desktop.

Select an option

Save LiuJiewenTT/403fcce75e31bfba3c8e9b82bae378ee to your computer and use it in GitHub Desktop.
grep铃(grepbel, a bel for grep)。发现目标字符串就让终端响铃。不过滤、彩色显示、按行输出、从管道接收待匹配文本。
#!/bin/bash
# 描述: 发现目标字符串就让终端响铃。不过滤、彩色显示、按行输出、从管道接收待匹配文本。
# 用法: grepbel "关键字"
# Developed by LiuJiewenTT (https://github.com/LiuJiewenTT).
keyword="$1"
# 从标准输入逐行读取
# 使用 --line-buffered 保证流式输出
grep --line-buffered --color=always -E "$keyword|$" |
while IFS= read -r line; do
# 直接输出 grep 已经带颜色的行
printf "%s\n" "$line"
# 检查是否匹配关键字,响铃
[[ "$line" =~ $keyword ]] && tput bel
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment