-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshow
executable file
·140 lines (121 loc) · 3.03 KB
/
show
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
##
## Shows colorized log
## Copyright (c) 2006 by Marcin Hłybin
## Michal Nazareicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
set -e
##
## Default variables
##
SHOWLINES="${SHOWLINES:-50}"
SHOWPAGER="${SHOWPAGER:-less -r}"
if [ -z "$SHOWCOLOR" ]; then
if which ccze >/dev/null 2>&1
then SHOWCOLOR="ccze -A"
else SHOWCOLOR=cat
fi
fi
SHOWLOGDIRS=":${SHOWLOGDIRS-/var/log}"
SHOWLOGEXTS=":${SHOWLOGEXTS-.log}"
ARG0="${0##*/}"
##
## Searches for log file
##
log_file () {
__OIFS="$IFS"
IFS=:
for __D in $SHOWLOGDIRS; do
for __E in $SHOWLOGEXTS; do
[ -r "$__D/$1$__E" ] || continue
FILE="$__D/$1$__E"
IFS="$__OIFS"
return 0
done
done
echo "$ARG0: $1: no such file" >&2
exit 1
}
##
## Cheks number of arguments
##
check_args () {
__NUM="$1"
while [ $# -ne 1 ]; do
shift
if [ $__NUM -eq "$1" ]; then return 0; fi
done
echo "$ARG0: wrong number of arguments; type 'show --help' for help" >&2
exit 1
}
##
## Usage
##
if [ $# -eq 0 ] || [ X"$1" = X--help ] || [ "X$1" = X-h ]; then
cat << EOF
usage: $ARG0 [ s | show ] [ <lines> ] <file> displayes <file>
$ARG0 ( f | find ) <pattern> <file> greps <pattern> from <file>
$ARG0 ( l | list ) lists log files in /var/log
sexamples: $ARG0 f ahes auth.log
$ARG0 l
$ARG0 /var/log/httpd/access.log
$ARG0 500 /var/log/messages
EOF
##
## Find
##
elif [ X"$1" = Xf ] || [ X"$1" = Xfind ]; then
check_args $# 3
log_file "$3"
grep "$2" -- "$FILE" | $SHOWCOLOR | $SHOWPAGER
##
## List
##
elif [ X"$1" = Xl ] || [ X"$1" = Xlist ]; then
check_args $# 1
OIFS="$IFS"
IFS=:
for DIR in $SHOWLOGDIRS; do
[ -n "$DIR" ] && [ X"$DIR" != X. ] && [ X"$DIR" != X.. ] || continue
cd "$DIR" || continue
echo "-------------------- $DIR --------------------"
find . -type f \! -name '*.gz' \! -name '*.[0-9]' | \
cut -d/ -f2- | sort | ${PAGER:-less}
cd -
done
IFS="$OIFS"
##
## Show
##
else
if [ X"$1" = Xs ] || [ X"$1" = Xshow ]; then shift; fi
check_args $# 1 2
if [ $# -eq 2 ]; then
SHOWLINES="$1"
shift
fi
log_file "$1"
if [ X"$SHOWCOLOR" = Xcat ] || [ X"$SHOWCOLOR" = X'cat -s' ]; then
NUM=$(( $(wc -l <"$FILE") - $SHOWLINES ))
[ $NUM -gt 1 ] || NUM=1
$SHOWCOLOR <"$FILE" | $SHOWPAGER +$NUM
else
tail -n "$SHOWLINES" <"$FILE" | $SHOWCOLOR | $SHOWPAGER
fi
fi