Shell Program that prompts for user Id

Hi

I have a question that after trying tirelessly I cant solve. I'm not great wth UNIX and wonder if anyone could help. I have to create a shell program using functions that prompts for a user ID. I must then verify that the user Id corresponds to an account on the system. If a legal user Id is provided then the pathname of the user's home directory must be displayed. If a user ID that is not recognised is entered an error message must be displayed. Any possible solution would gbe greatly appreciated.

Thanks in advance!

This sounds like homework to me. Why must you use functions for example?

It is not homework but a sample I was given. I dont necessarily have to do it but I would like to see how it is done so I can look at it and try and understand how it works. If anyone has any kind of help at all it would be greatly appreciated. As for the use of functions I'm not sure why they are used. Anyone with help please let me know. Thanks..

To get user input use the built-in ksh command read. See a ksh manual about how read works.

After having got the input search through /etc/passwd, there are all the users of this system along with their UIDs. Have a look at this file (it is readable for everybody) and you will surely find out how it is organized. Use the command grep (man grep) to search this file with the given input.

bakunin

Hey does anyone have any sample code on how best to do this. im sure there is an if else statement in it.

You have just forfeited the last ounce of good will here. If you do not want to do your work yourself, why should we do your work besides our own? The statement i quoted shows, that you are just making wild guesses instead of looking into manpages, educating and informing yourself or otherwise are unwilling to put any effort in finding a solution apart from writing one-liners here supposed to make us providing you with a script you could then pass on as your own.

The bottom line is: this is despicable.

bakunin

Despicable?? I thought these forums were meant to help. All I want is a possoble solution. I'm not here to offend anyone.

The second nature of this forum is that guidelines, pointers, links, sample codes etc are provided, but don't expect complete, working solutions for your questions/problems.

Hey guys,

Take it easy :slight_smile:

I dont expect a complete working solution. i didn't ask for that but I would be grateful if someone could reply some sample code.

No functions, but I'm sure you could expand on this with a little effort.

#!/bin/bash
clear
echo -n "What username would you like to check? "; read username

grep -q $username /etc/passwd

if [ $? -eq 0 ];then
echo $HOME
else
echo "User does not exist on system."
fi

Thanks a million. I think i know how to expand on this. This is a big help though. Thanks

Thanks i got it workin with functions but I 've got one problem. If the user Id is on the system it always prints the home directory of the person that is currently logged on. I want to print the home directory of the user that has been input.

eg. my problem is "Please enter the username: michael
/home/michael "

"Please enter the username: john
/home/michael "

What I want to do in this case is print /home/john regardless if I'm logged in as michael. I know its something simple. Can anyone provide the line of code that would do this?

Thanks!!

hey mmg,

sorry no bad feelings........you should answer by yourself why the $HOME is always printing same, irrespective of different username you entered ....... and what is $HOME ?????

To add further, check the second last column of /etc/passwd file.

To know more, learn how information in /etc/passswd file is organized.

I know why $HOME is always printing the same because this is the home directory I am in. In this case would I need to cd(change directory) into the directory of the username that was entered? Thanks!

Yeah, I wasnt thinking. Instead of the echoing $HOME try

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