pgm to find who logs in and logs out over a period of time
3 posters
pgm to find who logs in and logs out over a period of time
#!/bin/sh
echo "the current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s temp1 temp2
case "$?" in
0)
echo "no user logged in/out in last 5secs";;
1)
user=`comm -23 temp1 temp2`
user1=`comm -13 temp1 temp2`
file=`grep $user temp1 temp2 | cut -c 1-5`
file1=`grep $user1 temp1 temp2 | cut -c 1-5`
echo "the contents of the file:$file"
echo "the contents of the file2:$file1"
if [ $file = "temp1" ]
then
echo "user $user has logged out"
fi
if [ $file1 = "temp2" ]
then
echo "user $user1 has logged in"
fi
;;
esac
rm temp1
mv temp2 temp1
sleep 5
done
when i execute the pgm iam able to find who logs in and logs out every 5secs along with some error..the o/p i get is
339551
339551
339551
339553
subhadip
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
the contents of the file:temp1
the contents of the file2:
user 335872 has logged out
users.sh: line 26: [: =: unary operator expected
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
the contents of the file:
the contents of the file2:temp2
users.sh: line 22: [: =: unary operator expected
user 335872 has logged in
no user logged in/out in last 5secs
don't know why =: unary expected error is displaying.???
echo "the current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s temp1 temp2
case "$?" in
0)
echo "no user logged in/out in last 5secs";;
1)
user=`comm -23 temp1 temp2`
user1=`comm -13 temp1 temp2`
file=`grep $user temp1 temp2 | cut -c 1-5`
file1=`grep $user1 temp1 temp2 | cut -c 1-5`
echo "the contents of the file:$file"
echo "the contents of the file2:$file1"
if [ $file = "temp1" ]
then
echo "user $user has logged out"
fi
if [ $file1 = "temp2" ]
then
echo "user $user1 has logged in"
fi
;;
esac
rm temp1
mv temp2 temp1
sleep 5
done
when i execute the pgm iam able to find who logs in and logs out every 5secs along with some error..the o/p i get is
339551
339551
339551
339553
subhadip
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
the contents of the file:temp1
the contents of the file2:
user 335872 has logged out
users.sh: line 26: [: =: unary operator expected
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
no user logged in/out in last 5secs
the contents of the file:
the contents of the file2:temp2
users.sh: line 22: [: =: unary operator expected
user 335872 has logged in
no user logged in/out in last 5secs
don't know why =: unary expected error is displaying.???
Mekala- Posts : 7
Points : 12
Join date : 2010-03-04
Re: pgm to find who logs in and logs out over a period of time
its really tough job to debug a shell pgm
Re: pgm to find who logs in and logs out over a period of time
The problem was with if syntax.I have replaced it with case.There were a couple of problems with the logic as well.I have made the changes to work.Try with if.I am not able to fix the if, but fixed the program with case.Check out!
echo "the current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s temp1 temp2
case "$?" in
0) echo "no user logged in/out in last 5secs"
;;
1) user=`comm -3 temp1 temp2`
file=`grep $user temp1 temp2 | cut -c 1-5`
case $file in
temp1)
echo "user $user has logged out"
;;
temp2)
echo "user $user has logged in"
;;
esac
;;
esac
rm temp1
mv temp2 temp1
sleep 5
done
echo "the current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s temp1 temp2
case "$?" in
0) echo "no user logged in/out in last 5secs"
;;
1) user=`comm -3 temp1 temp2`
file=`grep $user temp1 temp2 | cut -c 1-5`
case $file in
temp1)
echo "user $user has logged out"
;;
temp2)
echo "user $user has logged in"
;;
esac
;;
esac
rm temp1
mv temp2 temp1
sleep 5
done
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum
|
|