Processes and Users

Thx for all the help so far, I really appreciate it, this is the last task I have to do then I am no longer a trainee ^^, which means I can use whatever utility I want. :open_mouth:

I need to write a script that does more or less what is shown below.
The Users Name
PID TTY TIME CMD
12345 pts/3 00:00:00 vim

The Users2 Name2
PID TTY TIME CMD
12345 pts/2 00:00:00 vim

Print out all users currently logged on to the system and display the processes they are running.
The problem I am facing is I don�t know how to acquire the users name (the actual name not the Username), as for the format it has to be like shown above more or less.

And again I cant use SED or Awk (I will be free to use them after am done with this :)).

So its gona be one of those while read line statements.
So the 1 million dollar question is how I format it by field

Will the cut command help me at all here?

I am starting to think this guy uses Google to find the tasks that he wants me to do, This is unbelievably similar to what i want to do :confused:, maybe it is just an exercise that is given to most beginners or something.

Thanks in advanced for any help.

Check '/etc/passwd' file for the mapping...

that did the trick thx, now i guess i must link the the user IDs to the name some how :eek:

#!/bin/ksh

userID='uucp'

while IFS=: read userIDpasswd junk junk junk userName junk
do
   if [ "${userID}" = "${userIDpasswd}" ]; then
      echo "userID->[${userID}] userName->[${userName}]"
   fi;
done < /etc/passwd

cool, thx man :b:

I have a question though, this programme will be ran by a few people, if one of those people where to change a process name or amend a users name, will this outcome be effected?

you'll need write a 'generic' script and pass the user ID to the script as a parameter. The process name(s) will be listed by whatever tool you use all the processes.
The snippet of the code provided above deals only with the userID->userName mapping - not with the listing of processes.

am bit confused on what junk does here :confused:

am getting this

userID->[] username->[Michael Jackson]

and its printed for all users, and there is no useID

is junk used to ignore or hide unwanted fields?

'junk' is just a placeholder for fields in the '/etc/passwd' file we/you want to skip reading.
Make sure you copy/paste the code 'as-is' - you seem to be refering to 'useID' and not 'userID' as it's used in the code snippet.

thx for the quick reply

now i get

userID->[] username->[uucp]

copy/paste the line from the /etc/passwd file with 'uucp' on it, pls.

You understand that this was just a 'hint' how to do the mapping, doncha ya? You can assign any existing userID from your system to the 'userID' variable. Probably copy/pasting a couple of lines here help.....

yay it works, thank you vgersh99 :smiley:

here are a few line from the /etc/passwd

jasmeet.manik:x:1422:100:Jasmeet Manik:/home/jasmeet.manik:/bin/bash
pascal.morano:x:1423:100:Pascal Morano:/home/pascal.morano:/bin/bash
shan.luo:x:1424:100:Shan Luo:/home/shan.luo:/bin/bash
raeika.memari:x:1425:100:Raeika Memari:/home/raeika.memari:/bin/bash
shakirat.raji:x:1426:100:Shakirat Raji:/home/shakirat.raji:/bin/bash

its back to doing what it was doing before

am still a bit confused on how the link between the current processes and the /etc/passwd is achieved

given a snippet from /etc/passwd file
and the script:

#!/bin/ksh

userID='raeika.memari'

while IFS=: read userIDpasswd junk junk junk userName junk
do
   if [ "${userID}" = "${userIDpasswd}" ]; then
      echo "userID->[${userID}] userName->[${userName}]"
   fi;
done < /etc/passwd

I get the following output:

userID->[raeika.memari] userName->[Raeika Memari]

thx again.

how would i feed the usernames of the currently logged in users to the userID variable?

#!/bin/ksh

w | nawk 'NR>2{print $1}' | sort -u | while read userID
do
   while IFS=: read userIDpasswd junk junk junk userName junk
   do
      if [ "${userID}" = "${userIDpasswd}" ]; then
         echo "userID->[${userID}] userName->[${userName}]"
      fi;
   done < /etc/passwd
done

thx again, its work, but i am not allowed to use awk :o

there must being a simpler way to view logged in users and what processes they are running :confused:

Sorry if I caused confusion on what I am trying to achieve
1) I need to find out users currently logged on to the system.
2) I need to find out what processes they are running.
3) I need to print those processes on the screen, for each user along with their actual name.
I am assuming the only way to get the name at this point if from the �/etc/passwd� file
Can this be done without using SED or AWK?

Sounds like a strange game you are playing.

w | cut -d ' ' -f1 | sort -u | while read userID
do
   while IFS=: read userIDpasswd junk junk junk userName junk
   do
      if [ "${userID}" = "${userIDpasswd}" ]; then
         echo "userID->[${userID}] userName->[${userName}]"
      fi;
   done < /etc/passwd
done

Look into what's been provided so far and also 'man who' and 'man w' - good luck!