Link to home
Start Free TrialLog in
Avatar of dileeppattat
dileeppattat

asked on

script to print all users looged in the system and their processes, should display real name on the top then their processes, for each user.

Hi
Wanted to write a shell script which should display all users logged in and the current processess they are running, it should display thier real name first then their processess...then the next users name and processess...so on..
thanks
dileep Pattattu
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Hi,

Try

for user in `who | awk '{print $1}'`
do
echo ----------------
finger omar | head -1 | awk -F":" '{print $3}'
echo "login name: $user"
ps -u $user
echo ----------------
done
Avatar of dileeppattat
dileeppattat

ASKER

hi
sorry that didnt help, I want to print the real name of the user not the login name, eg. dileep.pattattu is my login name bu the real name is Dileep Pattattu. Could you tell me how to obtain the a list of all users logged in and their processes please. the actual output of the shell should be as follows
Dileep Pattattu
PID TTY TIME CMD
31799 pts/3 00:00:00 bash
31866 pts/3 00:00:00 bash
2495 pts/7 00:00:00 vim
8368 pts/0 00:00:00 vim
9544 pts/2 00:00:00 ps

Sonia Arnold
PID TTY TIME CMD
8368 pts/0 00:00:00 vim
9544 pts/2 00:00:00 ps

thanks
Dileep
Hi,

This is the out put of

finger omar | head -1 | awk -F":" '{print $3}'

If you do not want login name then remove the line

echo "login name: $user"

So the script will be


for user in `who | awk '{print $1}'`
do
echo ----------------
finger omar | head -1 | awk -F":" '{print $3}'
ps -u $user
echo ----------------
done
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using finger is not a reliable way of extracting the username.  For example, if you have two users called

freds - Fred Smith
johnf - John Freds

Then

finger freds

will display both users.

Better to use

grep "^$user:" /etc/passwd |cut -f5 -d:
Obviously, my above comment isn't relevant if the users are stored in NIS or LDAP.
If you do want to use finger, it's best to use:

finger -fsm $user | awk '{print $2}'

if your version of finger has the -f and -s flags, otherwise


finger -m $user | head -1 | awk -F: '{print $NF}'

Thank you Tintin for your valuable input :)