-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_time_start.sh
executable file
·58 lines (51 loc) · 2.1 KB
/
log_time_start.sh
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
#!/bin/zsh -
#===============================================================================
#
# FILE: log_time_start.sh
#
# USAGE: log_time_start.sh <name of executable>
#
# DESCRIPTION: logs time spent by games/apps. Records all info in a text-file
# (~/logs/executable.ltime) in natural language. This is a two-step
# breakdown along with log_time_end.sh
# OPTIONS: No option.
# ARGUMENTS: The executable and its parameters, like sleep 30
# REQUIREMENTS: zsh. This script was designed to despise the primitive bash.
# BUGS: Bzz.
# NOTES: ---
# AUTHOR: Cláudio "Patola" Sampaio (Patola), [email protected]
# ORGANIZATION: MakerLinux
# CREATED: 25/02/2018 22:56:47 -03
# VERSION: 0.9b
# REVISION: By a bunch of monkeys (NOT apes).
#===============================================================================
#set -o nounset # Treat unset variables as an error
convertsecs() {
[[ $1 != [0-9]* ]] && exit 3
((h=${1}/3600))
((m=(${1}%3600)/60))
((s=${1}%60))
printf "%02d:%02d:%02d\n" $h $m $s
}
function log_time_start() {
[[ "$@" == "" ]] && { echo "Argumento vazio." ; exit 4 }
SECONDS=0
executavel="$1"
[[ $executavel == ./* ]] && executavel="$(echo "$executavel" | sed 's#^\./#'"$(pwd)"'/#')"
which "$executavel" >&- && executavel=="${executavel}"
datainicio="$(date '+%Y-%m-%d %H:%M:%S')"
datafim="$(date '+%Y-%m-%d %H:%M:%S')"
tempogasto=$SECONDS
arquivolog="$(echo "$executavel" | tr ' ' '_' | tr -cd '_a-zA-Z0-9-').ltime"
[[ ! -d ~/logs ]] && { mkdir ~/logs || exit 2 } # create logdir if it doesn't exist, bail out if can't create
if [[ -f ~/logs/"${arquivolog}" ]]
then
tempoanterior="$(tail -1 ~/logs/${arquivolog} | cut -d' ' -f6)"
let tempototal=tempoanterior+tempogasto
else
let tempototal=tempogasto
fi
echo "$tempogasto seconds this time ($(convertsecs $tempogasto)), $tempototal seconds total time ($(convertsecs $tempototal)). [started $datainicio and ended $datafim]" | tee -a ~/logs/"${arquivolog}"
return 0
}
log_time_start "$@"