1827474 Members
2014 Online
109965 Solutions
New Discussion

Help with shell script

 
sanman_2
Occasional Contributor

Help with shell script

Hi All,

I need to write a program using the DATE and WHO commands that prints the number of hours and minutes that a user has been logged on to the system ( assume that this is less that 24 hours.

Your help will be greatly appreciated!!!
6 REPLIES 6
Victor BERRIDGE
Honored Contributor

Re: Help with shell script

Hi,
What about the command last?

Just thoughts

Best regards
Victor
Rita C Workman
Honored Contributor

Re: Help with shell script

Why don't you just do a grep on the wtmp file. Something like:

last | grep

will show you the user when they logged in and how long they have been on...

rcw
Thomas Schler_1
Trusted Contributor

Re: Help with shell script

I do not want to write a script for you. But, log in as root and try 'last -1 '. Maybe, that's what you need.
no users -- no problems
Rick Garland
Honored Contributor

Re: Help with shell script

The last command will provide the info you are looking for. For the time, awk the field that contains the amount of time logged in
Rainer_1
Honored Contributor

Re: Help with shell script

here my script

who |awk -vh=`date +%H` -vm=`date +%M` '{
split($NF, a, ":")
sh=a[1];
sm=a[2];
dm=h*60+m-a[1]*60-a[2];
hh=int(dm/60);
mm=dm-hh*60;
printf("%s : %d hours %d min\n", $1, hh, mm);
}'
Darrel Louis
Honored Contributor

Re: Help with shell script

Just a quick command.
Hope this will lead you to the better output.

first run
/etc/last -R
Then filter out which information you want.
/etc/last -R|awk '{print $1, $3, $7, $8, $9, $10}'
This will display user-id, hostname or ip-address, time.

Check "man awk" for further details