1828038 Members
1979 Online
109973 Solutions
New Discussion

Modify the script

 
SOLVED
Go to solution
peterchu
Super Advisor

Modify the script

Sorry to ask the innocent question , I have the below script that run very fine at unx can't fail in linux box , could suggest how can I modify it to make it also work at linux box , very thanks if anyone can help .

the script:

#!/bin/sh

PATH=$PATH:/usr/bin:/usr/sbin:/usr/contrib/bin
TIMEOUT=40
EXCEPTIONLIST=~/etc/exception.lst
touch $EXCEPTIONLIST

kill_pid()
{
PID=$1
kill -9 $PID
}

echo "User:Activity:PID:Status"
who -u | while read line
do
time=$(echo $line | awk '{print $6}')
pid=$(echo $line | awk '{print $7}')
user=$(echo $line | awk '{print $1}')

grep -q "^${user}:" $EXCEPTIONLIST
if [ $? != 0 ]
then
if [ "$time" = "old" ]
then
(( time = $TIMEOUT + 1 ))
else
echo $time |grep -q "\."
if [ $? != 0 ]
then
time=$(echo $time|awk '{FS=":";print $1*60+$2}')
else
time=0
fi
fi
fi
#echo $time
if [ $time -gt $TIMEOUT ]
then
echo "$user:$time:$pid:Killed"

# Uncomment to do kill that PID
kill_pid $pid
else
echo "$user:$time:$pid:Untouched"
fi
done

______________________________________________________

the error is
./kill_idle: line 38: [: 00:03: integer expression expected , where line 38 is "if [ $time -gt $TIMEOUT ]"
3 REPLIES 3
Chris Xu
Trusted Contributor

Re: Modify the script

You can replace -gt with > like below:

old: "if [ $time -gt $TIMEOUT ]"

new: "if [ $time > $TIMEOUT ]"

Chris
Alexander Chuzhoy
Honored Contributor
Solution

Re: Modify the script

I copied and pasted it to in to my test machine-works fine.
had to create etc folder under user's home.
I have fedora 3.....
Regards.
Chris Xu
Trusted Contributor

Re: Modify the script

Sorry, ignore my previous reply. It was wrong.

One way to fix it is to declare TIMEOUT as an integer in the beginning like this.
typeset -i TIMEOUT
TIMEOUT=40

Chris