Home Directory Location Code

Hello All,
I am a teacher at a local high school, and we have put together a small computer and loaded a linux distro onto it for the students to experiment with, because 20 children share the computer, it is hard to keep track of there home directorys,

i was wondering if i could find a script for the location of the users home directory, so i could load a terminal, type in the command followed by the users name, and i could find where the users home directory is, i know i can see it in the /ect/password, but a script would be much easier

any help would be greatly appriciated

from a bash shell do the following

echo ~username

so if you have user fred, try

echo ~fred

Also, you should have standard rules for managing the computer, for example, putting all home directories under "/home" or similar, so fred's home directory should be "/home/fred".

thanks, but is there another way of doing it through the bash shell? :confused:
Maybe Using the Grep Command and the Cut Command to Give Me and Entire Line of Information from the etc/Passwd on the user?

thanks

The mechanism I have shown you is the simplest. You are welcome to make things more complex for yourself.

As porter said, this is more difficult and gets the same results as his suggestion, but if you really wanted to grep and cut the /etc/passwd file you could:

grep username /etc/passwd | cut -d ":" -f 6

or awk

grep username /etc/passwd | awk 'BEGIN {FS="[:]"} { print $6 }'

Thanks for your help so far guys, im almost done now, there is just one bit i need help with, i would like it to display a reply for a user who doesnt exist, such as, User Not found or similar

This is my code so far

echo "Which Users home directory to you want to locate?"
read Username
grep "^$Username" /etc/passwd | cut -d ":" -f 6

I was reading somewhere about a case or something
something about using

if [ $? -eq 0  ]

:confused:

Thanks Again

How about

finger $Username >/dev/null 2>&1
if test "$?" != "0"
then
     echo user does not exist
fi

What does the F/dev/null 2>&1 Do?

>/dev/null means write stdout to /dev/null, the bit-bucket

2>&1 means put stderr in the same place as stdout

Who is the adminstrator for this system?

Nominate one of your tech-savvy students.

Another way:

userentry=$(getent passwd $username)
if [ $? -ne 0 ]; then
        echo "$username does not exist"
        exit 1
fi
/usr/bin/echo "${username}'s home directory is \c"
echo "$userentry" | cut -d: -f6