[OT] Does "~" always point to $HOME?

Bill Campbell freebsd at celestial.com
Sat Jan 27 17:08:53 UTC 2007


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 at 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


More information about the freebsd-questions mailing list