The correct syntax is
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.