LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 11-12-2011, 04:24 PM   #1
plax65
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.

Code:
exec 3<&0
exec 0</home/.friends
while read line
do  
    user=`echo $line`
done
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
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,439

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Welcome to LQ!

If I understand correctly, this command should do what you want
Code:
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
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Untested code:
Code:
#!/bin/bash
while 1
do
#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}"
  do
    found=$(grep -w "${name}" ~/username.txt)
    echo ${found}
  done
# Wait a while and do it again
  sleep 5m
done
<edit>
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}')
</edit>

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

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

Code:
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:
Code:
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:
Code:
while sleep 30
do
  comm -1 -2 <( who -u | cut -f1 -d ' ' | sort -u ) <( sort -u /home/.friends ) | sed -e 's/$/ is logged in/'
done
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
plax65
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!
 
  


Reply


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 Mistro116@yahoo.com 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 07:43 AM.

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