//View Tip #573
Similar Tips
There are no more tips with these tags. Perhaps you'd like to submit one, or browse the archives.

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
I noticed a one liner here for working out when you started work that day. The code given was:

date "+%b %d" | xargs -i grep -m1 -i {} /var/log/syslog.0 |awk '{ print "Today I got to work at " $3 }'


This basically just gets the time the machine was started that day.

There are a few problems here, firstly this can be done a lot easier with the 'uptime' command. More importantly though the machine I work on doesn't get rebooted each day so instead of the startup time I'd need the time I first logged in that day. This can be achieved with the following:

last $USER | sed -n '/'"$(date +"%b %d")"'/!q; p' | tail -1 | awk '{ print "Today I got to work at " $7 }'


View Comments »




Comments 

Add your comment

Comments are currently disabled
zhuzhixin
I think there is also a problem with your solution. What will happen if you last login after mid-night, say 00:15, and then go to bed?

So maybe modify "tail -1" to "tail -x" and just print some date ...
Posted 2009-03-19 15:20:53
Or, what happens if you never really log out?  I'm the only one who uses my machine, so I just turn on the screensaver when I leave for the day...
Posted 2009-03-19 17:11:23
SteveG
Hey, I never said it would work for everyone! You guys need to come up with your own solutions! :p
Posted 2009-03-19 17:17:52
aaaa
My system is configured in es_ES.UTF-8 so
$ date "+%b %d"
gives:
abr 14
(Note the 'b' and the lower-case 'a')

syslog is saved as "Apr 14", so is "last", so these scripts won't work in i18n'ed systems like mine.

Solution: just force en_EN :)

$ LANG=en_EN.UTF-8 date "+%b %d" | xargs -i grep -m1 -i {} /var/log/syslog.0 |awk '{ print "Today I got to work at " $3 }'

and (modified $7 to $6 to work on my sys):

$ last $USER | sed -n '/'"$(LANG=en_EN.UTF-8 date +"%b %d")"'/!q; p' | tail -1 | awk '{ print "Today I got to work at " $6 }'
Posted 2009-04-14 13:47:43

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit