Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

pgm to find who logs in and logs out over a period of time

3 posters

Go down

pgm to find who logs in and logs out over a period of time Empty pgm to find who logs in and logs out over a period of time

Post by Mekala Fri Mar 12, 2010 6:48 pm

#!/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.???

Mekala

Posts : 7
Points : 12
Join date : 2010-03-04

Back to top Go down

pgm to find who logs in and logs out over a period of time Empty Re: pgm to find who logs in and logs out over a period of time

Post by Christopher Sat Mar 13, 2010 1:15 am

its really tough job to debug a shell pgm Sleep
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

pgm to find who logs in and logs out over a period of time Empty Re: pgm to find who logs in and logs out over a period of time

Post by Maithreyi Mon Mar 15, 2010 1:48 pm

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

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

pgm to find who logs in and logs out over a period of time Empty Re: pgm to find who logs in and logs out over a period of time

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum