Hello all, I'm new to shell scripting and want to make a script that I can write to log the users logging on and off the a unix system.
I have had a good look over the past few days to crack it, I think I am getting close.
I want a script that runs an infinite loop to check every 5 seconds to report on who logs into and who logs out of the unix system.
This is a example of the output I would like:
PHP Code:
The current users are:
abc1
rv0
sxu
tgray
No user has logged in/out in the last 5 seconds.
No user has logged in/out in the last 5 seconds.
User rsmith has logged in.
No user has logged in/out in the last 5 seconds.
User abc1 has logged out.
No user has logged in/out in the last 5 seconds.
I am using
who | awk '{print $1}' | sort > temp1
to get the users logged on and saving that into a temp file.
and have a while loop set up like this which will run each 5 seconds.
while true
do
.
.
.
.
.
sleep 5
done
The problem I'm having is, I can't work out how to compare the current users logged in to the users that have previously logged in.
I thought of making a second temp file that stores the current users then using:
cmp temp1 temp2
to look at the difference. problem is it gives me an output saying "file differs at character 42, line 6" and that isn't helping me much as I would like to then display that users name but not sure how to print that position in the file (or just a line for that matter).
The test to see if the user has logged on or off is easy, as it is only a couple of if statements.
like if user is in temp1 but not in temp2 then the user has logged out and vice versa.
I have been looking at this for a while now and am going around in circles. any code fragments, help or written shell scripts
would be greatly appreciated.
Thanks in advance
Noodle