Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Jan 2007 09:12:55 -0800
From:      Bill Campbell <freebsd@celestial.com>
To:        freebsd-questions@freebsd.org, Bill Campbell <freebsd@celestial.com>
Subject:   Re: [OT] Does "~" always point to $HOME?
Message-ID:  <20070127171255.GA13120@ayn.mi.celestial.com>
In-Reply-To: <45BB2EEB.10204@infracaninophile.co.uk>
References:  <20070126175122.64D2616A500@hub.freebsd.org> <20070126230241.GA93074@ns.umpquanet.com> <20070126231913.GA12604@ayn.mi.celestial.com> <45BB2EEB.10204@infracaninophile.co.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Jan 27, 2007, Matthew Seaman wrote:
>Bill Campbell wrote:
...
>> Probably the most portable way to do this would be to use awk.  A
>> simple script, homedir, might look like this:
>> 
>> #!/bin/sh
>> # getting the backwhacks correct is sometimes ``interesting''
>> homedir=`awk -F: "/^$1:/{print \\$6}" /etc/passwd`
>> 
>> [ -z "$homedir" ] && {
>> 	echo 'empty home for ' $1 2>&1
>> 	exit 1
>> }
>> echo $homedir
>> exit 0
>
>That does assume that all the user information is stored within the
>local /etc/passwd -- if you're using NIS or LDAP or anything
>like that, then you need a method that calls getpwnam(3) for you. 

A one-liner that should take care of these is:

python -c "import os.path; print os.path.expanduser('~$username')"

(This doesn't work with python-1.5.1 on an ancient Linux system
as os.path didn't appear until later).


Bill
--
INTERNET:   bill@Celestial.COM  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``A human being should be able to change a diaper, plan an invasion,
butcher a hog, conn a ship, design a building, write a sonnet, balance
accounts, build a wall, set a bone, comfort the dying, take orders, give
orders, cooperate, act alone, solve equations, analyze a new problem, pitch
manure, program a computer, cook a tasty meal, fight efficiently, die
gallantly.  Specialization is for insects.'' Robert Heinlein



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070127171255.GA13120>