timestamped notes on the command line
Bit of a strange request. I'd like have a window where I can record a list of开发者_StackOverflow中文版 ad-hoc notes, with a timestamp for each note. I've written the following which works:
$ while read line
> do
> echo $(date +%H:%M:%S) $line
> done | tee log
Hello
21:35:30 Hello
World
21:35:32 World
Is there a more elegant way of doing this?
If you care about preserving whitespace, quote your variable $line. The date format %T is equivalent to %H:%M:%S. You may want to append to the log file.
Otherwise, the only thing I can think of to make it more eleganter is to put in in a function.
notes() {
echo Type some notes. Hit Ctrl-D to quit.
while read line; do
echo $(date +%T) "$line"
done | tee -a log.txt
}
You can make the date
call a bit shorter:
date +%H:%M:%S
is same as:
date +%T
L() { echo `date` $* >> PATH/LOGFILE; }
in your .bashrc
, and you can do L something important or not
in every terminal.
HTH
精彩评论