Computing.Net > Forums > Unix > Why won't this work?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Why won't this work?

Reply to Message Icon

Name: olimiles
Date: August 19, 2006 at 02:04:48 Pacific
OS: Win XP
CPU/Ram: AMD Sempron +3200/ 1Gig r
Product: Homemade
Comment:

Hi, i've got a script here which lists the processes of each logged on user, beginning with the user's real name.

The problem i'm having is that when it gets to the ps command, i get an error
"ps: error: User name does not exist"
though it then outputs each user's processes like it should.

finger | while read user name junk
do
[[ user != Login* ]] && ps -fu $user
done

Can anyone tell me why it's not working properly?

Many thanks in advance
Oliver



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 19, 2006 at 11:31:37 Pacific
Reply:

You forgot the dollar sign, $, on $user:

finger | while read user name junk
do
[[ $user != Login* ]] && ps -fu $user
done


0

Response Number 2
Name: olimiles
Date: August 19, 2006 at 12:29:12 Pacific
Reply:

Thanks Nails!

But although the error has now been corrected. i still don't get the User's name above their list of processes! What have i missed?
I'm trying to get an output similar to this:

Mark Thomas
PID TTY TIME CMD
31799 pts/3 00:00:00 vim

Tom Jones
PID TTY TIME CMD
31854 pts/5 00:00:00 ps

Many thanks in advance
Oliver


0

Response Number 3
Name: nails
Date: August 19, 2006 at 15:18:30 Pacific
Reply:

You are only printing the contents of the ps -fu command for the user. Of course, the ps command isn't the most portable of commands, but I think you have to explictly code for the name. You can do something like this:

finger | while read user fn ln junk
do
[[ $user != Login* ]] && echo " $fn $ln"; ps -fu $user
done

In the above script, I'm assuming that the second and third fields of finger's output will always be the first and last name of the user. This can readily be a bad assumption.

At least on my Solaris system, the 'Name' delivered by the finger command is the comment field of the etc/passwd file - which can be anything.

Have you considered printing out /etc/passwd's comment field? Then, you don't have to worry about parsing finger's output:

This is my take:

finger | while read user fn ln junk
do
if [[ $user != Login* ]]
then
awk ' BEGIN { FS=":" }
{
if($1 == v1)
{
# print the user name for the correct user
printf(" %s\n", $5)
exit
}
} ' v1="$user" /etc/passwd
ps -fu $user
fi
done



0

Response Number 4
Name: olimiles
Date: August 19, 2006 at 16:17:10 Pacific
Reply:

thanks for the help Nails

Many thanks in advance
Oliver


0

Response Number 5
Name: pepavalle
Date: September 20, 2006 at 23:42:44 Pacific
Reply:

Hi!!

You have proven with the commando "w"?
The exit is right what you look for.

Bye


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Why won't this work?

Why won't this loop??? www.computing.net/answers/unix/why-wont-this-loop/5557.html

ksh scripting - numeric vs. alpha www.computing.net/answers/unix/ksh-scripting-numeric-vs-alpha/4549.html

sqlplus won't work www.computing.net/answers/unix/sqlplus-wont-work/6300.html