Operating System - Linux
1752483 Members
5795 Online
108788 Solutions
New Discussion юеВ

Re: Script to calculate the total idle minutes

 
SOLVED
Go to solution
Jdamian
Respected Contributor

Re: Script to calculate the total idle minutes

Try

(( idle_min = 10#$(echo $idle|cut -f1 -d':') * 60 + 10#$(echo $idle|cut -f2 -d':') ))
Hein van den Heuvel
Honored Contributor

Re: Script to calculate the total idle minutes

Dear Rita,

Please consider the following free advice, worth every penny.

1) This particular wheel has been invented many times over. Please look around for existing commercial or freeware solutions.
It looks simple enough now, but it is probably too simple. You'll soon need exclusion lists and time ranges and stuff like that.

2) Every Unix sysadm MUST try to aquire some awk and perl skills. Not saying you should start doing everything with those (allthough you could :-), but for your own career you MUST get going on those powerful tools.

3) Idle killers suck... IMHO. They suck computer resources, they suck positive energy out of users, they suck at providing security, they suck admin time. They do exactly waht their name implies, but not what you want: They kill idleness and replace it with busyness. Sounds like a bad virus to me!

4) Consider replacing the "ge" in you script with a simple ">". The leading zero minute field compares nicely as a string instead of numbers.

5) Just to trigger your cusiosity consider this perl script reading the smaple input your gave:

#perl -lne 'if (/(\d+):(\d\d)$/) { ($u,$t,$i)=split; print qq($u on $t idle for $i) if ($1>0 or $2>30)}' tmp.txt
kwchow on pts/tr idle for 0:54
mmak on pts/tk idle for 1:16


#perl -lne -e = cmmand follows, -l=print newlines, -n=loop throug input Not printing.

if (/(\d+):(\d\d)$/){ = Look for decimals, a colong and two decimals at the end of the line.
If found remember in $1 and $2 and execute block.

($u,$t,$i)=split; # AAyup that's what it does.
print qq($u on $t idle for $i) if # conditional statement
($1>0 or $2>30)} # The hour not 0, or minutes greate than 30


Cheers,
Hein.


Sandman!
Honored Contributor

Re: Script to calculate the total idle minutes

As bc(1) doesn't have this scummy octal rule; replace...

(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))

with...

(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':' | bc
) ))
Peter Godron
Honored Contributor

Re: Script to calculate the total idle minutes

Sandman,
please see my bc solution posted at 08:58:06
;-)
Dennis Handly
Acclaimed Contributor

Re: Script to calculate the total idle minutes

>Oscar: According to sh-posix man pages: If UNIX95 is defined,

On 11.23, this UNIX95 has been removed. In all cases it:
... will be processed according to ISOC standard
Rita Li
Frequent Advisor

Re: Script to calculate the total idle minutes

Thank you all for the great support

I found more than 1 solution, but I choose the most understanable one, the "|bc", also rewrote this script which is written few years ago in a more structured format

Correct, I need to acquire to learn some awk knowledge & someone else in my team is learning Perl, so may rewrite this script in Perl later

Thanks,
Rita