-
Notifications
You must be signed in to change notification settings - Fork 25
/
echo-now
executable file
·99 lines (95 loc) · 2.57 KB
/
echo-now
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# #!/bin/bash
set -euf -o pipefail
out () { printf %s\\n "$*" ; }
##
# now.sh
# ======
#
# By Alex Pankratov
# https://github.com/apankrat/now.sh/blob/master/README.md
#
# A simple shell script that prints current date/time while waiting
# for an input and echoing it to the stdout.
#
# It is particularly useful with [`tail`](http://en.wikipedia.org/wiki/Tail_%28Unix%29)
# when following log files. Instead of -
#
# tail -F access.log | grep "GET / "
#
# yielding -
#
# 173.199.116.xx - - [03/Mar/2013 01:05:18 -0800] "GET / HTTP/1.1" 200 2249 ...
# 59.167.170.xx - - [03/Mar/2013 01:10:19 -0800] "GET / HTTP/1.1" 200 2307 ...
# 54.251.76.xx - - [03/Mar/2013 01:16:52 -0800] "GET / HTTP/1.1" 200 2223 ...
#
# use -
#
# tail -F access.log | grep "GET / " | now
#
# and get -
#
# 173.199.116.xx - - [03/Mar/2013 01:05:18 -0800] "GET / HTTP/1.1" 200 2249 ...
# 59.167.170.xx - - [03/Mar/2013 01:10:19 -0800] "GET / HTTP/1.1" 200 2307 ...
# 54.251.76.xx - - [03/Mar/2013 01:16:52 -0800] "GET / HTTP/1.1" 200 2223 ...
# Mar 3 01:20:59 PST 2013
#
# where **the last line is reprinted every second** with current time.
#
# This gives
# an idea of when the last log activity was without needing to remember what
# the server time zone is or what the "-0800" translates to in your local time.
#
# Timestamp format
# ================
#
# The timestamp format can be adjusted by passing the
# [`date`](http://en.wikipedia.org/wiki/Date_%28Unix%29)
# format string to `now`. For example, this tweak -
#
# tail -F access.log | grep "GET / " now "[%d/%b/%Y %H:%M:%S %z]"
#
# yields -
#
# 173.199.116.xx - - [03/Mar/2013 01:05:18 -0800] "GET / HTTP/1.1" 200 2249 ...
# 59.167.170.xx - - [03/Mar/2013 01:10:19 -0800] "GET / HTTP/1.1" 200 2307 ...
# 54.251.76.xx - - [03/Mar/2013 01:16:52 -0800] "GET / HTTP/1.1" 200 2223 ...
# [03/Mar/2013 01:20:59 -0800]
#
# Maintainer: Joel Parker Henderson ([email protected])
# License: GPL
# Updated: 2015-01-25
##
##
# See what 'read -t' returns on timeout.
#
# For that - read from stdout. Alternatively, it could read
# from /dev/zero, but it's not available under Cygwin and
# in other non-*nix environments
#
read -t 1 <&1
timeout=$?
#
# loop forever
#
while true
do
#
# relay stdin to stdout
#
while true
do
read -t 1 line; rc=$?
if [ $rc != 0 ]; then break; fi
out $line
done
if [ $rc != $timeout ]; then break; fi
#
# print the timestamp
#
if [ $# -eq 1 ]; then
now=`date +"$1"`
else
now=`date`
fi
out -ne "$now\r" >&2
# done