5 ms·
Show HN: now.sh
- niggler 14y agoTwo comments: 1) Really? This is on the front page of HN? 2) why aren't you printing to stderr?
- octo_t 14y agoIts Sunday, frontpage tends to be quite dead. (No idea why not stderr however)
- latitude 14y ago1) Slow news day ... and this being an example of read's -t option put to a good use. 2) Ah, pedantic nitpicking, love it. Because the script is meant for interactive use with no streams redirected off the terminal, and because it doesn't output diagnostics or errors, but rather augments the stdout. Or put differently - show me an example where writing into stderr would make more sense. (edit) I guess someone can tee the output afterwards. Fine, I'll make it use stderr.
- nitrogen 14y agoOne argument against using stderr: IIRC stdout defaults to line buffering (or more if piped), while stderr is flushed immediately, so using stderr vs. stdout can yield different results.
- liw 14y agoSee also ts from moreutils (http://joeyh.name/code/moreutils/ http://joeyh.name/code/moreutils/)
- huhsamovar 14y agoHey, I can envisage this being helpful. Thanks. One criticism, which I think has already been voiced, is that you you print to standard error instead of standard out. That way anyone piping to a file won't be surprised when things break if the file is read by something else.
- idank 14y agoI often use tscat[1], which pretends a timestamp to every line and has a few more options. [1] http://gerg.ca/software/tscat/ http://gerg.ca/software/tscat/
- latitude 14y agohuhsamovar, you are shadowbanned.
- huhsamovar 14y agoHm, curious. Well, it seems at least you saw my feedback.
- dfc 14y agoAnother alternative is 'ts' which is part of joey hess's awesome collection of unix tools, moreutils.[1] I did not find out about ts until too late so I am still in the habit of using tai64n from djb's daemontools, which adds a tai64 timestamp to every file[2] and then tai64nlocal[3] in order to convert the timestamps to something useful to humans. If you don't know about moreutils check out the collection. I don't know what I'd do without vipe or vidir: Chronic: runs a command quietly unless it fails combine: combine the lines in two files using boolean operations ifdata: get network interface info without parsing ifconfi ifne: run a program if the standard input is not empty isutf8: check if a file or standard input is utf-8 lckdo: execute a program with a lock held mispipe: pipe two commands, returning the exit status of parallel: run multiple jobs at once pee: tee standard input to pipes sponge: soak up standard input and write to a file ts: timestamp standard input vidir: edit a directory in your text editor vipe: insert a text editor into a pipe zrun: automatically uncompress arguments to command [1] http://joeyh.name/code/moreutils/ http://joeyh.name/code/moreutils/ [2] http://cr.yp.to/daemontools/tai64n.html http://cr.yp.to/daemontools/tai64n.html [3] http://cr.yp.to/daemontools/tai64nlocal.html http://cr.yp.to/daemontools/tai64nlocal.html
- latitude 14y agoIf I read ts source right, it merely prepends the current time to every line it echoes from stdin to stdout. now is different. It re-prints current time once a second while waiting for the input. You basically get the running clock at the last line of output.
- dfc 14y agoOops. You are correct. Thanks for pointing that out. I have to say that now that I understand what it does I have no idea about the use case.
- nailer 14y agoOne slightly off-topic note: > ifdata: get network interface info without parsing ifconfi If it's Linux, ifconfig isn't maintained anymore and might actually be lying (eg, virtual IPs on vlans on bonded NICs - sounds exotic but almost every trading house uses this config). Use the 'ip' command for most things you would have done with 'ifconfig'. If it's BSD, ignore that and ifconfig away.
- mutantmonkey 14y agoI'm not sure I really see a point in this. How are you going to forget the server's time zone when it's listed in the timestamps? It doesn't really make sense.
- latitude 14y agoIt's for the cases when you are in a different timezone than the server. When the log says "03/Mar/2013 09:24:13 -0800" and it's 13:50 where I am, it takes me a while to understand how old that line is.
- mutantmonkey 14y agoRight, the server says its timezone is -0800. You don't know what timezone you are currently in?
- latitude 14y agoI know it's different, meaning that I have to do some basic arithmetics to get the log line age. It also requires me to roll my precious eyes to look at the local time in a system tray. It's just all too tedious.
- jamespo 14y agoSome services, like squid have logs with the time in UTC seconds...
- coderholic 14y agoI have the current time printed in my prompt, which means I can easily see how long commands took to run, eg: [start time] $ ./run_long_script [ end time ] $ My actual prompt looks like: [09:36] bmd-mba|bmd ~ $ whoami bmd [09:36] bmd-mba|bmd ~ $ Details in https://github.com/coderholic/config/blob/master/.bash_profile https://github.com/coderholic/config/blob/master/.bash_profi...
- dopamean 14y agoIs there a way to do this so the timestamp includes seconds? Thanks.
- p4bl0 14y agoBy default "\t" in your PS1 will display the time the same way `date +%H:%M:%S` does (which includes seconds).
- p4bl0 14y agoActually with that setup (which is a bit like mine) you can see how long it took from the finishing time of the last command you executed (when your PS1 was redisplayed) to the finishing of the command you just executed.
- wes-exp 14y agoSpeaking of date/time stuff, is there anything that would convert to the current time zone? e.g. echo "03/Mar/2013 01:16:52 +0000" | mytz => 02/Mar/2013 17:16:52 -0800
- lutusp 14y ago$ date -d "Sun Mar 3 18:05:17 UTC 2013" Sun Mar 3 10:05:17 PST 2013 The above converts to the system TZ. Or: $ echo $( export TZ=CST; date -d "Sun Mar 3 18:05:17 UTC 2013" ) Sun Mar 3 18:05:17 CST 2013 The above converts from any TZ to any other TZ, without resetting your shell's TZ variable.
- dantheta 14y agoYou can also use: $ TZ=CST date -d "Sun Mar 3 18:05:17 UTC 2013" Sun Mar 3 18:05:17 CST 2013 which doesn't invoke a subshell. env vars can always be prepended to the executable name for use with just that program (not sure if that's bash only, though).
- datr 14y agoFor an alternative way of accomplishing the example given take a look at multitail [1]. Hitting return will insert a line with the current date and time to the file you're tailing. It also colour codes the output and allows you to tail multiple files simultaneously. [1] http://www.vanheusden.com/multitail/ http://www.vanheusden.com/multitail/
- deleted 14y ago[deleted]
- kvprashant 14y agoGeneral note: please check script before using unless you want another valentine's day ;)
- goldfeld 14y agoThat was funny seeing this here, for a minute I thought someone had posted the now.sh from my dotfiles. But I think it's no longer there. In any case mine wasn't as thoughtful, it was just a shell script to pretty print the current time, so that I could index it with Gnome-do/Kupfer/Synapse and easily call it from there. But I have since moved that responsibility to a key binding in vim, since I'm always inside the editor. All this, of course, so that I could hide the Linux Mint bar on the bottom and still see the time easily through another mean.
- bhpnpmg 14y agoHow about a script: a wrapper to run any other script or process, that logs both STDOUT and STDERR in one file, but prepends a short string of your choice for lines that are from STDERR.