Login loop

Hi!
I'm writing a program that test wich users are on

 
who | cut -d' ' -f1 > users

echo "The current users are: "
sort users 

[/FONT]
then enters a loop that will wait 5 seconds then test which users are on then compare it to who was on before. If no one has logged on or off it will echo "no one has logged on/off" but if somebody has it will echo their username and "has logged on" or their username "has logged off"
So far I have:

 
while true
do
sleep 5
who | cut -d' ' -f1 > 5secs
sort 5secs >/dev/null
#???
cmp users 5secs
if [$? -eq 0]
then
echo "No users have logged in/off in the past 5seconds"
else
diff users 5secs
#???
 
5secs > users
done  

but I'm not sure if theat's how to correctly use the cmp method to test for differences. Also I have no idea how to use diff to compare the two correctly and return if users have been added or removed and what their names are.
Can anyone help me to figure out how diff can be used this way? Or is there another command to use?
any help with these would be appreciated :slight_smile:

Hi,

Here is your script with right commands and proper syntax.

>users
while true
do
  who | cut -d' ' -f1 |sort > 5secs
  diff -q users 5secs > /dev/null
  if [ $? -eq 0 ]
  then
     echo "No users have logged in/off in the past 5seconds"
  else
     comm -32 users 5secs|xargs -i echo {} has logged off
     comm -31 users 5secs|xargs -i echo {} has logged on
  fi
  mv 5secs users
  sleep 5
done

Regards,

Ranjith

Hey!
Thankyou for your help. It works great now
:smiley: