Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-12-2011, 04:24 PM   #1
LQ Newbie
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
While reading lines of a file do this..

Ok, I am not quite sure how to phrase this correctly but does anyone know of a way to continuously read lines of a file and if one of those lines are found produce a message?

For example, if i have a list full of usernames and i store that int /home/username.txt`. then if one of the names that is found within user name is also found as a logged in user using the w command, produce a a message saying only that user which was found is logged on.

exec 3<&0
exec 0</home/.friends
while read line
    user=`echo $line`
exec 0<&3
this is what i have tried using, however, it only picks up the last name in the list.

any help would be greatly appreciated!
Old 11-12-2011, 06:24 PM   #2
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,309

Rep: Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271
Welcome to LQ!

If I understand correctly, this command should do what you want
w | grep -f /home/.friends
To have this run continuously, you could set up a cron job.
Old 11-12-2011, 06:46 PM   #3
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,179

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Untested code:
while 1
#Get the list of logged in users (The gawk is to list the users only once.)
  users=$(users /var/log/wtmp | gawk '{for (i=1;i<=NF;++i) ++name[$i]} END {for (i in name) print i}')
# Print any user who's in the friends file
  for name in "${users}"
    found=$(grep -w "${name}" ~/username.txt)
    echo ${found}
# Wait a while and do it again
  sleep 5m
Oops. I didn't notice that you wanted to use the w command.
If that's what you want to use, try
users=$(w -hu | gawk '{for (++name[$1]} END {for (i in name) print i}')

Last edited by PTrenholme; 11-12-2011 at 06:57 PM.
Old 11-12-2011, 07:10 PM   #4
Senior Member
Registered: May 2008
Posts: 3,974
Blog Entries: 1

Rep: Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380Reputation: 1380
Here's another way of producing your list:

comm -1 -2 <( who -u | cut -f1 -d ' ' | sort -u ) <( sort -u /home/.friends )
... then add a bit of message text to pretty it up:
comm -1 -2 <( who -u | cut -f1 -d ' ' | sort -u ) <( sort -u /home/.friends ) | sed -e 's/$/ is logged in/'
...then to make it loop:
while sleep 30
  comm -1 -2 <( who -u | cut -f1 -d ' ' | sort -u ) <( sort -u /home/.friends ) | sed -e 's/$/ is logged in/'
BTW, did you really mean '/home/.friends' or do you want ~/.friends (a.k.a. $HOME/.friends)?
Only mention it as it looked a little odd.

Last edited by GazL; 11-12-2011 at 07:42 PM. Reason: added some.
Old 11-13-2011, 12:07 PM   #5
LQ Newbie
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks for the fast responses! for the record the /home/.friends was just a typo haha. I'll give these a shot when i get a free moment!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading lines within a file (Perl) jack.barnes Programming 12 03-16-2011 11:09 AM
Reading specific lines from a file Mork2k4 Programming 2 09-10-2009 11:51 PM
PHP - Help With Reading Lines In A File windisch Programming 6 05-03-2006 12:48 PM
Reading the number of Lines in a File Programming 31 11-24-2005 12:36 AM
Reading random lines in c++? twirl Programming 7 10-30-2005 10:11 AM

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration