-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.sh
More file actions
executable file
·75 lines (59 loc) · 2.01 KB
/
log.sh
File metadata and controls
executable file
·75 lines (59 loc) · 2.01 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
#!/bin/sh
# 2014-07-17 Created by Julien Moreau aka PixEye
log_file=~/.my-log
now=`date +'%Y-%m-%d %T'` # Example: 2014-07-17 14:46:05
nbwp=1 # Number of wanted parameters (without options)
cmd=`basename $0` # Command name
usage="Usage: $cmd [-h]\n\t" # Help message:
usage=$usage"Display this help message.\n"
usage=$usage"\n"
usage=$usage"Usage: $cmd <action description>\n\t"
usage=$usage"Add this action in a file with date & time.\n"
usage=$usage"\n"
usage=$usage"Usage: $cmd -e\n\t"
usage=$usage"Edit the log file.\n"
usage=$usage"\n"
usage=$usage"Usage: $cmd -t\n\t"
usage=$usage"Show the tail of the log file."
echo "\ntest"|grep ntest >> /dev/null && e="-e" # Does echo need the -e option?
if [ "$#" -ne $nbwp -o "$1" = "-h" ] # Check parameters number
then echo $e $usage 1>&2 ; exit 2 # Display message help and exit
fi
if [ "$#" -ne $nbwp -o "$1" = "-e" ]
then vi + "$log_file" ; exit $? # Edit log file
fi
if [ "$#" -ne $nbwp -o "$1" = "-t" ]
then tail "$log_file" ; exit $? # Tail of the log file
fi
if [ "$#" -ne $nbwp -o "$1" = "-l" ]
then tail -n 2 "$log_file" ; exit $? # Tail of the log file
fi
if test -r "$log_file"
then
last_line=`tail -n1 "$log_file"`
last_datetime=`echo $last_line|cut -c -19`
last_date=`echo $last_datetime|cut -d' ' -f1`
last_time=`echo $last_datetime|cut -d' ' -f2`
last_h=`echo $last_time|cut -d':' -f1`
last_m=`echo $last_time|cut -d':' -f2`
last_s=`echo $last_time|cut -d':' -f3`
current_h=`date +%k`
current_m=`date +%M`
diff_h=`expr $current_h - $last_h`
diff_m=`expr $current_m - $last_m`
if [ "$diff_m" -lt 0 ]
then diff_m=`expr $diff_m + 60` ; diff_h=`expr $diff_h - 1`
fi
if [ "$diff_m" -le 9 ] ; then diff_m="0$diff_m" ; fi
echo "Previous line: $last_line"
time_diff="$diff_h:$diff_m"
echo "Difference with previous time: $time_diff"
set +x # end of debug mode
fi
new_line="$now $*"
echo "$new_line" >> "$log_file"
echo "Added to '$log_file':"
echo "$new_line"
exit 0 # Normal exit
# Prefs for vim editing:
# vim: tabstop=8 shiftwidth=8 textwidth=80 noexpandtab