-
Notifications
You must be signed in to change notification settings - Fork 0
/
logwhich
executable file
·59 lines (46 loc) · 1.6 KB
/
logwhich
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
#!/bin/bash
function usage {
cat << EOT
Usage: $0 Dir [ String ]
Given a Dir directory argument, logwhich prints the names of the "most
interesting log files" to watch. File names to print are determined
by the given string, String ('_log' by default), as follows:
1. create a list of all files of the form Dir/*String*
2. from that list, create a list of distinct Dir/*String prefixes
3. using each prefix to represent its own file group, print the
name of the file that is last (by lexical sort) in each group
The "last" test is a cheap way to find the most recently created logfile in
a common httpd use case. For example, if Dir is ../../logs and contained
access_log.2015.04.01 error_log.2015.04.01 rewrite_log
access_log.2015.04.02 error_log.2015.04.02
access_log.2015.04.03 error_log.2015.04.03
this effect would be produced:
\$ $0 ../../logs
access_log.2014.04.03
error_log.2015.04.03
rewrite_log
EOT
}
Dir=$1
[[ $Dir ]] || { # if no args, print usage info
usage
exit
}
String=${2:-_log}
files_b=( $Dir/*$String* ) # lexically sorted files array (bash)
# if verbose
# echo files_b: ${files_b[@]}
perl -w -e " # double quote first block to interpolate shell values
use strict;
my @files = (qw(
${files_b[*]}
));
my (\$d, \$s) = (qw( $Dir $String ));
"' # single quote second block to shield from shell values
my (%h, $f, $b); # hash, filename, basename before $String
foreach $f (@files) {
($b = $f) =~ s|(\Q$d/\E.*\Q$s\E).*|$1|;
$h{$b} = $f; # final (most recent) value clobbers on purpose
}
map { print "$h{$_}\n" } sort keys %h;
'