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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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 04:16 PM.. Reason: Slash before bin