-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellme
executable file
·101 lines (86 loc) · 2.2 KB
/
bellme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#
# Author: gitmp01
# Version: 1.0.0
#
# Description: ring a bell when a match
# is found
#
function check_paplay_exists() {
if [[ -z $(which paplay) ]]; then
>&2 echo 'paplay command not found: is pulseaudio installed?'
>&2 echo 'Install it through: '
>&2 echo 'sudo apt get install -y pulseaudio'
exit 1
fi
}
function check_bell_variable() {
if [[ -z "$BELL" ]]; then
>&2 echo 'BELL env variable not set, set it to a sound you want to play'
>&2 echo 'Like'
>&2 echo 'export BELL=/usr/share/sounds/freedesktop/stereo/complete.oga'
exit 1
fi
}
function find_match() {
local text=$1
local regexp=$2
local output=$(echo "$text" | grep -E "$regexp")
if [[ -n "$output" ]]; then
paplay "$BELL"
echo "$output"
fi
}
function usage() {
local b=$(tput bold)
local n=$(tput sgr0)
echo "${b}Usage:${n} $(basename "$0") <regexp> [text]"
echo ""
echo "${b}Description:${n}"
echo " Use this script to warn you when something is printed"
echo " in a log file."
echo ""
echo "${b}Arguments:${n}"
echo ""
echo " <regexp> 'grep -E' compatible regexp "
echo " text optional text for testing the script"
echo ""
echo "${b}Note:${n}"
echo " You should set the BELL env variable to a sound like:"
echo " export BELL=/path/to/sound.oga"
echo ""
echo " You may consider to add the line above to your .bashrc file."
echo ""
echo "${b}Examples:${n}"
echo ""
echo " tail -f afile.log | $(basename "$0") 'Error: ¯\_(ツ)_/¯' "
echo " $(basename "$0") 'Hello' 'Hello World!'"
}
function show_usage_if_null() {
local value=$1
if [[ "$value" == "null" ]]; then
usage
exit 1
fi
}
function main() {
check_paplay_exists
check_bell_variable
local regex=${1:-null}
local value=${2:-null}
show_usage_if_null "$regex"
# Check if file descriptor 0 is opened
if test ! -t 0; then
while read value; do
find_match "$value" "$regex"
done
elif [[ -n "$1" ]]; then
show_usage_if_null "$value"
find_match "$value" "$regex"
exit 0
else
usage
exit 1
fi
}
main "$@"