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





FirstManAsked:
Who is Participating?
 
omarfaridCommented:
Hi,

It seams that you have a wrong idea about shell scripting. Shell scripting in not a full programming language that has its own functions. Shell scripting has  the basic control statements, plus it uses other tools & commands available on the system to manipulate data.

Im my case I used who to till me who is logged in, sort to sort unique, and grep to get info from the password file. awk was used to get the required info.

Bash (or other shells for that matter) do not have functions to those things. The idea on unix systems is "why to redevelop what is already developed"
0
 
omarfaridCommented:
Hi,

Try this script

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

correction

for user in `who | awk ' {print $1} ' | sort -u`
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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


0
 
FirstManAuthor 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??
0
 
omarfaridCommented:
Hi,

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

I could not understand what you meant. What alternative you are looking for?
0
 
FirstManAuthor 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
0
 
FirstManAuthor 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"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.