PDA

View Full Version : grep -v -f


dustin
11-21-02, 05:02
Hello all !
im from Germany and hope you can understand my bad english :)

now my problem:

first my script:

echo "folgender User hat sich soeben eingeloggt: "
date
#( until user1= who | egrep -v $uservar
until user1=`who | cut -b 1-8 | fgrep -v -f ./user`
do
sleep 3
date
echo $user1
echo $user1 >> user
cat user
done

ok,
some lines in the script are not important (like date etc.)
importent is line 4!

until user1=`who | cut -b 1-8 | fgrep -v -f ./user`

i want that this line search for users how loggt on in unix. (who)
but they dont show all users only some how stand in the file ./user
and the result of the search must in the variable user1.

i think something of the syntax is wrong.

Please can you help me?

thank you

Damian Ibbotson
11-21-02, 07:01
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

WingMan
11-26-02, 11:36
What is you have a User name greater than 8 characters ...

who -u | awk ' {print$1}

will return the list of users currently logged in wih no limit on the number of characters in the user name.

backer
11-27-02, 16:06
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