Operating System - HP-UX
1832766 Members
2945 Online
110045 Solutions
New Discussion

how do I write a shell program

 
sanman_2
Occasional Contributor

how do I write a shell program

1. how do I write a shell program to display the number of files in a users current directory.

2. a program to display a sorted list of the logged in users. Just the user name and no other information.

Your help will be greatly appreciated!!
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: how do I write a shell program

Hi:

Try this:

# ls | wc -l

# who | sort | more

..JRF...
James R. Ferguson
Acclaimed Contributor

Re: how do I write a shell program

Hi:

Oh, sorry, you only wanted the username, so:

# users

...or if you prefer more fancy...

# who|sort|awk '{print $1}'

...JRF...
Anthony Goonetilleke
Esteemed Contributor

Re: how do I write a shell program

don't forget the -a if not you wont get the files begining with a dot i.e .profile

ls -a | wc -l

or if you want a recursive method

find . | wc -l
Minimum effort maximum output!
Rick Garland
Honored Contributor

Re: how do I write a shell program

Number files in a users home directory:

cd $HOME (users home directory)
find . -depth | wc -l


Users logged in:

who | awk '{print $1}' | sort | uniq -c
This will list all users who are logged in but if you only want to see the user names once, this is providing the unique feature. If a username appears more than once in the output, the 'uniq -c' will eliminate the duplicates and show the name once.
Stefan Schulz
Honored Contributor

Re: how do I write a shell program

You can use sort -u instead of sort | uniq. So this would read:

who | awk '{print $1}' | sort -u

This saves you one command and will be a little bit faster.
No Mouse found. System halted. Press Mousebutton to continue.
federico_3
Honored Contributor

Re: how do I write a shell program


If you are searching all the files in a user directory you should use the find command because it recuyrsively discends the directory hierarchy for the path you fornish:.
You could use this form:

find user_dir -type f | awk '{print "TOTAL FILES => " NR }'


and about the users logged in :


who | awk '{print $1}'| sort -u


federico