Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

userprocesses

Posted on 2007-10-06
14
Medium Priority
?
216 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??





0
Comment
Question by:FirstMan
  • 7
  • 7
14 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027465
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027467
Hi,

correction

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

Author Comment

by:FirstMan
ID: 20027480
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FirstMan
ID: 20027493
"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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027510
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
 

Author Comment

by:FirstMan
ID: 20027523
ok r u sure that i can use this in /bin/bash?? but i dont want to use AWK??
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027525
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
 

Author Comment

by:FirstMan
ID: 20027555
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027565
Hi,

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

Author Comment

by:FirstMan
ID: 20027576
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20027602
Hi,

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

Author Comment

by:FirstMan
ID: 20027633
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
 

Author Comment

by:FirstMan
ID: 20027641
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
 
LVL 40

Accepted Solution

by:
omarfarid earned 2000 total points
ID: 20027781
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question