printing each users by processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing each users by processes
# 1  
Old 04-28-2008
printing each users by processes

hello All,

i am trying to get the each users by with thier processing. I have try it in several ways but i am not getting thier my code is

#!bin/bash/
users=`cat /etc/passwd | cut -d':' -f5`
onlineusers=`users | tr '.' ' '`
$onlineusers | while read `$users`
do
echo `ps -U $users`
done

but its not working, root: it says command not found

pls any way around it.
# 2  
Old 04-28-2008
The correct syntax is

Code:
while read users

What you have tries to evaluate the value of $users as a command, thus you get "root: command not found" (because the first user is root, so that is the command, and the rest of the users are passed as arguments to this command).

Why do you extract users from /etc/passwd when you end up reading them from users? On my system, users prints the same user multiple times if they are logged in multiple times, which might not be what you want.

Here's the whole script with the problems fixed:

Code:
#!/bin/bash
users | tr '. ' '
' | sort -u | while read user
do
  ps -U "$user"
done

The second argument to tr is a newline inside single quotes; opening quote, end of line, new line, closing quote. Yes, that's a valid string in bash (and sh generally). This breaks up the output of users on multiple lines so we can sort -u to get rid of any duplicates.

The output of users on my Ubuntu box doesn't have any full stops in it, but maybe yours is different.

I also took out the (as far as I could tell) gratuitous echo `backticks` and the temporary assignment of the output of users to a variable which only got used once. Oh, and I fixed the shebang line -- there should be no slash after bash, and one before bin.

Last edited by era; 04-28-2008 at 04:16 PM.. Reason: Slash before bin
# 3  
Old 04-28-2008
re

Thanks era

Have u tried the code, its not working on my mine.

I am trying to get individual online REAL NAME and not the USERNAME with thier correspondence processes. Using the etc/passwd would get the name
but comparing it with users online is the complicated bit.
# 4  
Old 04-28-2008
Yes, I have tested it here (but only after you asked). I only have one user so it's not a very thorough test case.

Do you get an error, or doesn't it do what you expect?

Where do you want the real name to be displayed?

Code:
#!/bin/bash
users | tr '. ' '
' | sort -u | while read user
do
  sed -n "s/:[^:]*:[^:]*$//;s/^$user:[^:]*:[^:]*:[^:]*://p" /etc/passwd
  ps -U "$user"
done

The sed script is the moral equivalent of grep "^$user:" /etc/passwd | cut -d: -f5 which is arguably more readable. Maybe you want to use that instead, actually.

Last edited by era; 04-28-2008 at 04:42 PM.. Reason: Explanation of and alternative to sed script
# 5  
Old 04-28-2008
Thanks for the reply era
But i still get error username not found and some of this against each user


********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
# 6  
Old 04-28-2008
What does the output from users look like on your system? Which platform is this?
# 7  
Old 04-28-2008
firstname.surname firstname.surname firstname.surname
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Display info about users (UID GID processes terminal)

I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made. 1. The problem statement, all variables and given/known data: The task is to create a script which prints information about users whose names are specified in the... (2 Replies)
Discussion started by: kornfan
2 Replies

2. UNIX for Dummies Questions & Answers

help printing processes

hello, i need help with something which should be fairly simple but i am having a little trouble. i need to list all the processes on the system but also need the parent id. so the following does not print parent id. ps -A or ps -e. i searched manual and saw an option --ppid so i... (2 Replies)
Discussion started by: iluvsushi
2 Replies

3. Shell Programming and Scripting

Sorting the users logged in according to the number of processes

Is it possible to get a list of users sorted by the number of processes executed by each. I have a HP - UX server with 2800 processes running currently. And I want to know the number of processes owned by each person logged in to that server.something like below: user1 : 150 Processes user2 :... (2 Replies)
Discussion started by: engineer
2 Replies

4. Shell Programming and Scripting

How to display what processes, users have opened at a given time

Hello, What i have to do is make a top 10 list of users sorted by the number of processes opened at a given time. Can anyone help me with finding out for a given moment, for all users how many processes each had opened? (5 Replies)
Discussion started by: gabibyte
5 Replies

5. Programming

How to stop other processes and kernel from printing output on current virtual term

Hello All, Background ======== I am creating a virtual appliance console for a software stack on VMware ESXi. I am using Centos 5.x as the Linux distro (Guest OS). I have created a ncurses based application that does the user authentication and present him with some basic controls to do basic... (2 Replies)
Discussion started by: ku@ntum
2 Replies

6. Shell Programming and Scripting

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. :O 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... (22 Replies)
Discussion started by: barbus
22 Replies

7. UNIX for Dummies Questions & Answers

users and there processes

I am trying to write a script to list all users and their processes. I cant seem to find out how to link the PID to the user. For example if i run ps -a I get a nice list of all the processes running and their PID, but no information about which user is running them. I need the users real name not... (3 Replies)
Discussion started by: iago
3 Replies

8. Shell Programming and Scripting

i need a scipt to email users with idle processes!?

hello, i am VERY new to this whole script world. I need to come up with a script that will email a user if they have an idle process past 500 minutes...any suggestions??? Thanks so much. (0 Replies)
Discussion started by: sheppy28
0 Replies

9. Shell Programming and Scripting

Displaying the processes of users

Hi guys, I'm writing a script to display all the current logged on users and the processes they're running, output to a file. The output will look similar to this: User1 (Real name not login name) PID TTY TIME CMD 3179 pts/3 00:00:00 vim User2 PID TTY TIME ... (2 Replies)
Discussion started by: olimiles
2 Replies

10. UNIX for Dummies Questions & Answers

Users and processes

Hi All, I am trying to write a script to list all users and their processes. I cant seem to find out how to link the PID to the user. For example if i run ps -a I get a nice list of all the processes running and their PID, but no information about which user is running them. If i use w i... (17 Replies)
Discussion started by: Chiefos
17 Replies
Login or Register to Ask a Question