The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Processes and Users barbus Shell Programming and Scripting 22 10-03-2007 07:09 PM
users and there processes iago UNIX for Dummies Questions & Answers 3 09-10-2007 02:51 PM
i need a scipt to email users with idle processes!? sheppy28 Shell Programming and Scripting 0 02-02-2007 04:08 PM
Displaying the processes of users olimiles Shell Programming and Scripting 2 08-18-2006 02:57 PM
Users and processes Chiefos UNIX for Dummies Questions & Answers 17 06-17-2006 04:16 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2008
sam4now sam4now is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
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 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 03:16 PM.. Reason: Slash before bin
  #3 (permalink)  
Old 04-28-2008
sam4now sam4now is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
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 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 03:42 PM.. Reason: Explanation of and alternative to sed script
  #5 (permalink)  
Old 04-28-2008
sam4now sam4now is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
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 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
What does the output from users look like on your system? Which platform is this?
  #7 (permalink)  
Old 04-28-2008
sam4now sam4now is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 23
firstname.surname firstname.surname firstname.surname
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0