-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogview
More file actions
executable file
·171 lines (150 loc) · 4.12 KB
/
logview
File metadata and controls
executable file
·171 lines (150 loc) · 4.12 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
# ------------------------------------------------------------------
# [Paul Borowicz] System Admin Project 3 for MOBI
# logfile viewer script
# ------------------------------------------------------------------
### UNCOMMENT NEXT 2 LINES FOR DEBUGGING
#set -x
#trap read debug
VERSION=0.1.0
SUBJECT=some-unique-id
#USAGE='This script will display log files
# it accepts these flags:
# -h # number of lines to display from top
# -t # number of lines to display from bottom
# -k <string> display lines with matching string
# -v version
#
# logview <options> <logfile1> <logfile2> ...'
### ---- FUNCTIONS ###
#This checknum function verifies that the variable is a number
function checknum {
test $1 -ge 0 &> /dev/null
if [ $? -gt 1 ]; then
echo "This flag requires an integer, exiting."
exit
fi
}
#### -------- ###### ----------
USAGE=$(cat <<-END
This script will display log files
it accepts these flags:
-h # number of lines to display from top
-t # number of lines to display from bottom
-k <srting> display lines with matching string
-v version
logview <options> <logfile1> <logfile2> ...
END
)
# --- Options processing -------------------------------------------
# display help if --help is appended to the command
if [ "$1" = "--help" ]
then
echo "$USAGE"
exit 1;
fi
#display usage is command has nothing appended
if [ $# == 0 ] ; then
echo "$USAGE"
exit 1;
fi
##process flags v, t, k, & h as specified below
## require flags to have options and exclude any flags not specified
while getopts ":v:t:k:h:" optname
do
case "$optname" in
#display version
"v")
echo "Version $VERSION"
exit 0;
;;
# get -t, and put it in frombot
"t")
#set t flag arguement to frombot variable
frombot=$OPTARG
checknum $frombot
;;
# get -k and put it in matchme
"k")
#set k flag argument to matchme string
matchme=$OPTARG
;;
# get -h and put it in fromtop
"h")
#set h flag argument to fromtop variable
fromtop=$OPTARG
checknum $fromtop
;;
# exclude any unspecified flags
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
# error if no arguments given with flags
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
# exclude any longer uspecified flags
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift "$(($OPTIND - 1))"
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SUBJECT.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Body --------------------------------------------------------
# SCRIPT LOGIC GOES HERE
#make working directory /var/log, exit if it cannot
cd /var/log ||exit
#Tell user what's going on
echo "Processing the following log files: $@"
echo "$#" ' file(s)'
while [ "$1" ]
do
echo "---------"
if [ -f $1 ]
then
#check if fromtop (h) was provided
if [ -n "$fromtop" ]; then
#display lines from top of file as specified
echo "Top $fromtop lines for $1:"
head -$fromtop $1
fi
#check if matchme (k) was provided
if [ -n "$matchme" ]; then
#grep file for anything matching matchme string
echo "displaying lines with $matchme:"
grep $matchme $1
fi
#check if frombot (t) was provided, output specified number of lines form bottom
if [ -n "$frombot" ]; then
# display the bottom lines of file as specifed in frombot variable
echo "Bottom $frombot lines of $1:"
tail -$frombot $1
else
# check if other flags are present, skip default action if they are
# if no other flags are present and frombottom is unset
# perform these default actions
if [ -z "$matchme" ] && [ -z "$fromtop" ]
then
#set frombot to 3, the default
frombot=3
echo "Bottom $frombot lines of $1:"
tail -$frombot $1
fi
fi
else
echo "$1 not found."
fi
shift
done
# -----------------------------------------------------------------