forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
philog
executable file
·84 lines (78 loc) · 1.56 KB
/
philog
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
#!/usr/bin/perl -w
use strict;
my $logfile = "./.philog.LOGFILE";
my $date = `date`; chop($date);
my $pwd = `pwd`; chop($pwd);
my $pretty = 0;
my $curdir = 0;
my $clean = 0;
my $history = 0;
foreach (@ARGV) {
$pretty = 1 if /^-pretty$/;
$curdir = 1 if /^-dir$/;
$clean = 1 if /^-clean$/;
$history = 1 if /^-history$/;
}
if ($history) {
my @cmd = <STDIN>;
my $last = 1;
$last = $ARGV[1] if $#ARGV>0 && $ARGV[1] =~ /^\d+$/;
my $cmd = $cmd[$#cmd-$last];
$cmd =~ s/^\s*\d+\s*//;
$cmd =~ s/^[\d\-]+ [\d\:]+\s*//;
chomp($cmd);
&log($cmd);
}
elsif ($#ARGV-$pretty-$clean == -1) {
open(LOG,$logfile);
while(<LOG>) {
/^(... ... .. ..:..:.. ..T 20..) (\S+) (.+)$/;
my ($time,$dir,$cmd) = ($1,$2,$3);
if($pretty) {
print "\e[32m$time \e[34m$dir ".($cmd=~/^[\#\^]/?"\e[30;1m":"\e[m").$cmd."\e[m\n";
}
elsif ($clean) {
print "$cmd\n";
}
else {
print "$time $dir $cmd\n";
}
}
close(LOG);
}
elsif ($curdir) {
open(LOG,$logfile);
while(<LOG>) {
/^(... ... .. ..:..:.. ..T 20..) (\S+) (.+)$/;
my ($time,$dir,$cmd) = ($1,$2,$3);
next unless &same_dir($dir,$pwd);
if ($pretty) {
print "\e[32m$time ".($cmd=~/^[\#\^]/?"\e[30;1m":"\e[m").$cmd."\e[m\n";
}
elsif ($clean) {
print "$cmd\n";
}
else {
print "$1 $3\n";
}
}
close(LOG);
}
else {
my $cmd;
foreach (@ARGV) { $cmd .= "$_ "; }
chop($cmd);
&log($cmd);
}
sub log {
my ($cmd) = @_;
open(LOG,">>$logfile");
print LOG "$date $pwd $cmd\n";
close(LOG);
}
sub same_dir {
my ($a,$b) = @_;
$a =~ s/nfs\/[^\/]+/nfs/;
$b =~ s/nfs\/[^\/]+/nfs/;
return $a eq $b;
}