Link to home
Start Free TrialLog in
Avatar of FirstMan
FirstMan

asked on

userprocesses

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??





Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Hi,

Try this script

for user in `who | awk ' {print $1} | sort -u`
do
echo processes run by $user:
echo "----------"
ps -u $user
done
Hi,

correction

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

ASKER

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??
"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 ??
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
ok r u sure that i can use this in /bin/bash?? but i dont want to use AWK??
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


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??
Hi,

The above is a shell script that uses different tools and commands on the system including awk, who sort, ps, etc.
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.
Hi,

I could not understand what you meant. What alternative you are looking for?
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
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"
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial