LinuxQuestions.org
Register a domain and help support LQ
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 09-04-2008, 12:47 PM   #1
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 0
BASH query to find unique Usernames


Hi

I'm trying to find all individual users who are logged on and then display the processes that they are running

#!/bin/bash
who | while read fn; do
ps -u ${fn%% *}
done

Will show users and processes but when I go for Unique users with ...



#!/bin/bash
who | while read fn; do
${fn%% *} | sort -u | ps -u ${fn%% *}
done

I get errors

I'm new to this. Is there anything obviously wrong in the second script?
 
Old 09-04-2008, 01:03 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,915

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by basildon View Post
Hi

I'm trying to find all individual users who are logged on and then display the processes that they are running

#!/bin/bash
who | while read fn; do
ps -u ${fn%% *}
done

Will show users and processes but when I go for Unique users with ...



#!/bin/bash
who | while read fn; do
${fn%% *} | sort -u | ps -u ${fn%% *}
done

I get errors

I'm new to this. Is there anything obviously wrong in the second script?
Not obviously, but it might help if you actually posted the error(s) you're getting....
 
Old 09-04-2008, 02:04 PM   #3
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
I couldn't quite figure out what your script was doing (it's unreadable to me) but here's how I did it:

Code:
#!/bin/bash
fn=`who | cut -d " " -f1 | sort -u`
for looper in $fn; do
ps -u $looper
done
 
Old 09-04-2008, 02:09 PM   #4
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 307

Rep: Reputation: 38
This is gonna sound so simple: throw it to uniq

Code:
#!/bin/bash
who | while read fn; do
${fn%% *} | sort -u | ps -u ${fn%% *} | uniq
done
Enjoy
 
Old 09-04-2008, 02:29 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
ps -u $( w -h -s| cut -d" " -f 1 |uniq)
 
Old 09-04-2008, 03:02 PM   #6
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Tinkster & arckane

Did you test your scripts on a system where more than 1 non-unique user was on at the same time?

you can't pass 2 usernames to ps -u the way you are, it has to be listed de-limited by comma's:

ps -u root username: won't work
ps -u root,username: will work
 
Old 09-04-2008, 03:14 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Aye .. .that's why the `uniq' is in there.
 
Old 09-04-2008, 03:26 PM   #8
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by CRC123 View Post
Tinkster & arckane

Did you test your scripts on a system where more than 1 non-unique user was on at the same time?

you can't pass 2 usernames to ps -u the way you are, it has to be listed de-limited by comma's:

ps -u root username: won't work
ps -u root,username: will work
I meant more than one unique user (my bad). So if user1, user2, and user3 where all on, the command would effectively run this:

ps -u user1 user2 user3

which won't work, it has to look like this:

ps -u user1,user2,user3

Sorry for the confusion.
 
Old 09-04-2008, 05:05 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Oh do'h... you're absolutely right; i'm in dire need for my holiday; and
what I didn't notice was that my su'ed into root account didn't show in w,
hence my version was working ...

Work-around for that problem is to use xargs
Code:
w -h -s| cut -d" " -f 1 |uniq|xargs -i ps -u {}
 
Old 09-05-2008, 04:49 AM   #10
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Great response. Sorted it out. Thanks to all.
 
  


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
SUSE10.1 : bash query : saving input to file linuxFNG Linux - Newbie 3 03-07-2008 01:23 PM
Query Windows RPC to find dynamic ports from linux? humbletech99 Linux - Networking 0 11-08-2007 06:16 AM
bash varible define query noir911 Programming 4 10-18-2006 02:39 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM
Bash Script- Finding/Generating unique UserIDs pheasand Linux - General 2 12-11-2004 10:44 AM


All times are GMT -5. The time now is 03:46 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration