Computing.Net > Forums > Unix > Shell Scripting Help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Shell Scripting Help

Reply to Message Icon

Name: Slamo
Date: May 13, 2003 at 19:25:33 Pacific
OS: Windows Xp Professional
CPU/Ram: P4 2.3 GHz 512MB DDR333
Comment:

Hi, can anyone help me, I am doing an assignment and I'm having trouble figuring it out.

First of all. I am using the Bourne shell and I need to display the user of the shell script their Surname followed by their first name. I also have to display the path for their home directory which I have tried doing with:

echo Your home directory is `$home`

but that doesn't seem to work.

If anyone can help it would be greatly appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 13, 2003 at 19:45:12 Pacific
Reply:

echo "Your home is $HOME"

echo "Today is `date`"


0

Response Number 2
Name: Slamo
Date: May 13, 2003 at 20:42:36 Pacific
Reply:

For some reason that doesn't seem to be working:

$ echo "Your home is $home"
Your home is


Also, how do I display the users name in the format Surname, FirstName


0

Response Number 3
Name: David Perry
Date: May 13, 2003 at 21:21:03 Pacific
Reply:

The environment variable is case sensitive. $HOME and $home are different variables.

Is the last name stored in the gecos field of /etc/passwd ?


0

Response Number 4
Name: Slamo
Date: May 13, 2003 at 23:53:49 Pacific
Reply:

Yes, the first name is stored in /etc/passwd

But it is first name first

I need to display last name first to the user.


0

Response Number 5
Name: Slamo
Date: May 13, 2003 at 23:54:47 Pacific
Reply:

Sorry, the full name is stored in etc/password

not just the first name. Typo.


0

Related Posts

See More



Response Number 6
Name: David Perry
Date: May 14, 2003 at 05:12:17 Pacific
Reply:

Does

grep "^$user:" /etc/passwd | cut -f 5 -d ':'

give you enough of a clue to finish?


0

Response Number 7
Name: WilliamRobertson
Date: May 14, 2003 at 17:18:46 Pacific
Reply:

You could also look at

finger $LOGNAME

finger returns more info than just full name, so would take a bit more filtering, e.g:

finger $LOGNAME | awk '/Name: / {print $5, $4}'

On the other hand it could capture networked users who are not listed in the local passwd file.


0

Response Number 8
Name: Slamo
Date: May 14, 2003 at 19:38:30 Pacific
Reply:

Thank you very much guys, you have been awesome.

This helped me so much


0

Response Number 9
Name: Slamo
Date: May 14, 2003 at 19:58:02 Pacific
Reply:

when i use $user or $USER in the shell script, it doesn't work, the code only worked when i actually hard code the users login name into the script.

Am i doing something wrong.

Sorry, I'm only a n00b


0

Response Number 10
Name: WilliamRobertson
Date: May 15, 2003 at 07:13:12 Pacific
Reply:

It might depend on the shell you're using or the platform you're on. Try $LOGNAME.



0

Response Number 11
Name: Slamo
Date: May 18, 2003 at 02:50:42 Pacific
Reply:

Is there any way I can display the user name in the format SURNAME, FIRSTNAME

Sorry, I should of asked that originally.


0

Response Number 12
Name: propyl
Date: May 23, 2003 at 16:34:37 Pacific
Reply:

The name of the shell script can be displayed from the script.

SCRIPTNAME="`basename $0`"
echo "Name of script is: ${SCRIPTNAME}"


0

Response Number 13
Name: propyl
Date: May 23, 2003 at 16:50:23 Pacific
Reply:

Set USERNAME to the LOGNAME you want to process, then ...

cat /etc/passwd|grep \^${USERNAME}: | \
awk -F: '{print $5}' | awk '{print $2,$1}'
Inefficient but reliable! The grep starts with "^" and ends with ":" to to stop false matches e.g. smith and smithy. First awk in the pipe is delimited ":". Second awk is delimited space. Enjoy.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Shell Scripting Help

C Shell script help! www.computing.net/answers/unix/c-shell-script-help/3108.html

c shell script help PLEASE www.computing.net/answers/unix/c-shell-script-help-please/4355.html

Write a Bourne Shell Script--help! www.computing.net/answers/unix/write-a-bourne-shell-scripthelp/2055.html