We help IT Professionals succeed at work.

userprocesses

FirstMan
FirstMan asked
on
233 Views
Last Modified: 2010-05-18
I have been tackling this question for sometime and i am getting no success whatsoever. the question is to create a script called 'userprocesses' that allows me to find current users logged on and also display what processes they are currently using.

So far i have used this  following code:

$ who -a (which display the usenames currenlt online with options)
$ who -s (to list the names, line and time fields)

but i feel that i am going wrong because i am entering commands and not creating a script.

can u help??





Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Try this script

for user in `who | awk ' {print $1} | sort -u`
do
echo processes run by $user:
echo "----------"
ps -u $user
done
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

correction

for user in `who | awk ' {print $1} ' | sort -u`

Author

Commented:
thank you for the script, it works pefectly. one more question, if i wanted to do the script as a bash, how would i go about it??

Author

Commented:
"PLEASE DISCARD THE PREVIOUS MESSAGE"

thank you for the script, it works pefectly. one more question, if i wanted to do the script as a bash, how would i go about it?? and instead of having the login name e.g. adam.adebisi, it has the user's real name e.g. Adam Adebisi ??
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

for user in `who | awk ' {print $1}'  | sort -u`
do
user_actual=`grep -w $user /etc/passwd | awk -F":" ' { print $5 } '`
echo processes run by $user_actual:
echo "----------"
ps -u $user
done

Author

Commented:
ok r u sure that i can use this in /bin/bash?? but i dont want to use AWK??
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

To make bash script:

- Put the following line at the top of the file (1st line) e.g. call it myscript

#!/usr/bin/bash

- Add the provided script in the file myscript

- Make the script file executable:

chmod +x myscript

to run

./myscript


Author

Commented:
ok i entered the following script:

#!/usr/bin/bash

for user in `who | awk ' {print $1}'  | sort -u`
do
user_actual=`grep -w $user /etc/passwd | awk -F":" ' { print $5 } '`
echo processes run by $user_actual:
echo "----------"
ps -u $user
done

is this correct for BASH and not AWK??
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

The above is a shell script that uses different tools and commands on the system including awk, who sort, ps, etc.

Author

Commented:
ok then. is there alternative to this bit of script:

for user in `who | awk ' {print $1}'  | sort -u`
do
user_actual=`grep -w $user /etc/passwd | awk -F":" ' { print $5 } '`

becuase for my knowledge, this is not suitable for BASH. As BASH carry script with 'echo' in it.
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

I could not understand what you meant. What alternative you are looking for?

Author

Commented:
Ok
i understand that they are different ways to write a script. This script

for user in `who | awk ' {print $1}'  | sort -u`
do
user_actual=`grep -w $user /etc/passwd | awk -F":" ' { print $5 } '`

is an awk command which is used for manipulating data and generating reports. I DON'T want to write the script in AWK formant, i want to use BASH command or shell scripting

Author

Commented:
This is an example of what i mean

#!/bin/bash
echo "what is your age"
read myVar
echo "Hello $USER you are $myVar years old"
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.