Originally posted by Damian Ibbotson
I think your problem is with using UNTIL. This will execute the contents of the loop until "user1=`who | cut -b 1-8 | fgrep -v -f ./user`" returns an exit status of zero. So, when you find a new user, the code breaks out of the loop.
Try:
until true
do
<some commands>
done
...and you'll see that nothing happens.
Unless I haven't seen what exactly you are trying to achieve here, the code seems to have several flaws. What happens if a user logs off and then logs back on again? What if the same user logs in more than once?
You could have a far more comprehensive script that could trap logins and logouts with an exact time by using all of the information output from the who command.
e.g.
who > file1; sleep 60; who > file2; diff file1 file2
You could write a script around this and format the output as you like.
HINT:
^> = login
^< = logout
============================================
I also am trying to figure out what you are trying to do... If you are trying to trap login information, I suggest you investigate modifying your login rules.
If all you are after is creating a file that has login information in it, perhaps take a look at the command 'last' which already has information in it?
I'm not sure I completely understand what you are trying to accomplish, but let me toss a few ideas your way..
(kinda messy, but I think this might be sorta what you are looking for)
#!/bin/sh
#variables and declarations
user=`echo $LOGIN`
userlist=`who | awk '{print $1}'
exist=`echo $userlist | grep $user`
file="/home/filename.txt"
#this just says, if the file exists and
#user is already in the list, then exit, if not in the list
# put him/her in the list
if [ -e $file ] && [ $user = $exist ]; then
exit 0
else
echo $user >> $file
fi