Operating System - Linux
1748185 Members
4269 Online
108759 Solutions
New Discussion юеВ

Re: Script to calculate the total idle minutes

 
SOLVED
Go to solution
Rita Li
Frequent Advisor

Script to calculate the total idle minutes

I have a small script that reads in an output with format as follows:

ivcheung pts/th .
ivcheung pts/tp .
ktam pts/tx 0:02
kwchow pts/tr 0:54
mmak pts/tk 1:16
ritali pts/tc 0:01
rwong pts/tt 0:08
tchan pts/tw 0:15

Then find out those telnet sessions that have been idled for more than 30 minutes

My script is like :

num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`
lines=1
while (( num > 0 ))
do
info=`head -n $lines $tempdir/whodo.out | tail -1`
userid=`echo $info | cut -f1 -d' '`
tty=`echo $info | cut -f2 -d' '`
idle=`echo $info | cut -f3 -d' '`
if [ $idle = "old" ]
then
idle_min=999999
else
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
fi
if [ $idle_min -gt 30 ]
then
tty="/dev/"$tty
( invoke another script to kill the idled session )
fi
(( lines = lines + 1 ))
(( num = num - 1 ))
done

This script used to work fine until recently I moved from a 11.0 server to another 11.i server, if the idled time = "0:08" or "0:09", error message would occur

08 : The specified number is not valid for this command.

I know I can use

echo $(( 10#08 ))

to force a base 10 echo but don't know how to embed this into the old lines

Rita
15 REPLIES 15
Dennis Handly
Acclaimed Contributor

Re: Script to calculate the total idle minutes

There have been other posts on this issue of leading zeros. You may want to use:
typeset -LZ idle=$(echo $info | cut -f3 -d' ')

>num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`

Why do you need the cut? wc -l will give lines directly. Also, you can use grep -c, so you don't need wc.

Any reason you use num and a while loop and getting the lines one at a time with: info=`head -n $lines $tempdir/whodo.out | tail -1`
Why not use awk to process the whole file until EOF?

By using awk, you can make it lots simpler to read. And no stinkin' problems with garbage "octal" leading zeroes. And you don't need to use cut. You would have to use substr for the ":" extraction of the MM:SS time.

If you aren't familiar with awk, you could at least use:
grep pts $tempdir/whodo.out |
while read userid tty idle; do
...
done
Rita Li
Frequent Advisor

Re: Script to calculate the total idle minutes

My problem is: I am not familar with awk
Peter Godron
Honored Contributor
Solution

Re: Script to calculate the total idle minutes

Rita,
you could try:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=`echo "$idle_hr * 60 + $idle_min"|bc`

instead of your:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
Dennis Handly
Acclaimed Contributor

Re: Script to calculate the total idle minutes

You need to use typeset of the one with leading zeroes:
typeset -LZ idle_min=$(echo $info | cut -f3 -d' ')

>My problem is: I am not familar with awk

Then use my "grep | while read userid tty idle; do" suggestion.
Dennis Handly
Acclaimed Contributor

Re: Script to calculate the total idle minutes

>typeset -LZ idle_min=$(echo $info | cut -f3 -d' ')

Oops that's not your initial value. Just add this before your code:
typeset -LZ idle_min
john korterman
Honored Contributor

Re: Script to calculate the total idle minutes

Hi Rita,


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

try this:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo ${idle#[0-9]*:0}|cut -f2 -d':'

regards,
John K.
it would be nice if you always got a second chance
Jdamian
Respected Contributor

Re: Script to calculate the total idle minutes

According to sh-posix man pages, forcing the base to be 10 is not necessary.

In fact, I tested your script in my 11.11 box and it runs fine.

Which shell is using in 11.11 ?
Have you tried several shells (ksh, sh-posix) ?
Dennis Handly
Acclaimed Contributor

Re: Script to calculate the total idle minutes

>Oscar: According to sh-posix man pages, forcing the base to be 10 is not necessary.

Perhaps UNIX95 was set?

It is hard to argue when Rita had the error message:
08 : The specified number is not valid for this command.
Of course it would be nice to know which command. I have no problems duplicating it on 11.23:
./itrc_leadzero.sh[26]: 08 : The specified number is not valid for this command.
26 (( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':') ))

Which means you need my: typeset -LZ idle_min
With Peter's extra temps:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=(( idle_hr * 60 + idle_min ))

Of course bc(1) probably doesn't have this scummy C octal rule.

Jdamian
Respected Contributor

Re: Script to calculate the total idle minutes

Hi Rita
Hi Dennis

I agree Denis when he wrote that the problem might be UNIX95.

According to sh-posix man pages:

If UNIX95 is defined, shell arithmetic evaluators let command,
$((...)) and ((...)) recognizes interger constants beginning with 0
and 0x (or 0X) as octal and hexadecimal numbers.