LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looking for command to help with program (https://www.linuxquestions.org/questions/linux-newbie-8/looking-for-command-to-help-with-program-4175440361/)

LBP74 12-06-2012 09:35 PM

Looking for command to help with program
 
I need to be able to calculate the amount of time a user has been logged onto the system for.

UsrMin=$(who | grep $1 | cut -c30-35)
Time=$(date +%H:%M)
echo $UsrMin
echo $Time
echo LoginTime CurrentTime
echo ' '$UsrMin - $Time
~

conntime f132a13
20:57
21:18
LoginTime CurrentTime
20:57 - 21:18


This is as close as I could get to figuring out my problem. It wouldn't allow me to do the subtraction so I left it like this. Please help!

chrism01 12-06-2012 10:13 PM

Extending that method, you'd convert the hrs to mins, add the extra mins, then subtract 'now' from 'then', optionally converting the result to hrs and mins if diff_result > 60.
Of course, this only works when the login time is in the same day as the current time. For a more general approach, you'd have to calc the date diff as well, probably using the various date format options.

Personally, if this was for a serious eg prod system, I'd likely do it in Perl as it has plenty of date calc routines.


All times are GMT -5. The time now is 10:05 PM.