LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH query to find unique Usernames (https://www.linuxquestions.org/questions/linux-newbie-8/bash-query-to-find-unique-usernames-667569/)

basildon 09-04-2008 11:47 AM

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?

TB0ne 09-04-2008 12:03 PM

Quote:

Originally Posted by basildon (Post 3269527)
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....

CRC123 09-04-2008 01:04 PM

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


arckane 09-04-2008 01:09 PM

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

Tinkster 09-04-2008 01:29 PM

Code:

ps -u $( w -h -s| cut -d" " -f 1 |uniq)

CRC123 09-04-2008 02:02 PM

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

Tinkster 09-04-2008 02:14 PM

Aye .. .that's why the `uniq' is in there.

CRC123 09-04-2008 02:26 PM

Quote:

Originally Posted by CRC123 (Post 3269702)
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.

Tinkster 09-04-2008 04:05 PM

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 {}

basildon 09-05-2008 03:49 AM

Great response. Sorted it out. Thanks to all.


All times are GMT -5. The time now is 02:47 PM.