-
Notifications
You must be signed in to change notification settings - Fork 5
/
ftail
executable file
·31 lines (22 loc) · 1.06 KB
/
ftail
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
#!/bin/bash
# Print the contents of a file since the last time we read it, kinda like
# "tail -f" but across multiple runs, by remembering how big the file was the
# last time we ftail-ed it.
# If the file became smaller since the last time, we start from the beginning.
# Uses "skvdb" -- a short perl program that provides a simple key-value
# database for general use by shell scripts.
[[ -z $1 ]] && exit 1
# realpath and current size
rp=$(realpath "$1")
cs=$(stat -c %s "$rp")
# offset (i.e., size of file last time we read it); default to 0 if key not found
off=$(skvdb get "ftail:$rp")
off=${off:-0}
echo >&2 "off=$off, cs=$cs"
[[ $cs -lt $off ]] && { echo >&2 "current size $cs < last read $off, resetting to 0"; off=0; }
# oh boy if I ever switch to one of the BSDs because I finally tire of
# Lennart's lunacy I'll be in deep shit using all these GNU specific flags!
dd if="$rp" skip=$off iflag=skip_bytes status=none
# record current size in skvdb (unless in debug mode)
[[ -z $D ]] && skvdb set "ftail:$rp" $cs
[[ -n $D ]] && echo >&2 "not setting 'ftail:$rp' to $cs"