-
Notifications
You must be signed in to change notification settings - Fork 0
/
logread.sh
57 lines (49 loc) · 1.2 KB
/
logread.sh
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
#!/bin/sh
# Shell script compatibility wrapper for /sbin/logread
#
# Copyright (C) 2019 Dirk Brenken <[email protected]>
# SPDX-License-Identifier: GPL-2.0-or-later
count=
pattern=
follow=
help=
while getopts "l:e:fh" OPTION
do
case $OPTION in
l) count=$OPTARG;;
e) pattern=$OPTARG;;
f) follow="-f";;
h) help=1;;
*) echo "Unsupported option $OPTION"
usage;;
esac
done
logfile="/var/log/messages"
if [ ! -f "${logfile}" ]
then
echo "Error: logfile not found!"
exit 2
fi
usage()
{
echo "Usage: logread [options]"
echo "Options:"
echo " -l <count> Got only the last 'count' messages"
echo " -e <pattern> Filter messages with a regexp"
echo " -f Follow log messages"
echo " -h Print this help message"
exit 1
}
[ -n "$help" ] && usage
# if no count and follow then print from beginning
[ -z "$count$follow" ] && count="+1"
# if no count but follow then print only new lines
[ -z "$count" ] && count="0"
# shellcheck disable=SC2086
if [ -z "$pattern" ]; then
echo tail -n "$count" $follow "$logfile"
busybox tail -n "$count" $follow "$logfile"
else
echo tail -n "$count" $follow "$logfile" | grep -E "$pattern"
busybox tail -n "$count" $follow "$logfile" | grep -E "$pattern"
fi