Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Jan 2007 13:34:53 -0800
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: [OT] Does "~" always point to $HOME?
Message-ID:  <45BBC57D.40605@u.washington.edu>
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matthew Seaman wrote:
> Bill Campbell wrote:
>> On Fri, Jan 26, 2007, James Long wrote:
>>>> Message: 24
>>>> Date: Fri, 26 Jan 2007 09:22:44 -0800
>>>> From: Bill Campbell <freebsd@celestial.com>
>>>> Subject: Re: [OT] Does "~" always point to $HOME?
>>>> To: freebsd-questions@freebsd.org, Bill Campbell
>>>> 	<freebsd@celestial.com>
>>>> Message-ID: <20070126172244.GB6575@ayn.mi.celestial.com>
>>>> Content-Type: text/plain; charset=us-ascii
>>>>
>>>> My point isn't whether the FreeBSD /bin/sh expands it, but that not all
>>>> systems are FreeBSD, and that one can have problems on other *NIX systems.
>>>>
>>>> Knowing where there may be differences, and avoiding the assumptions that a
>>>> program behaves the same on all systems, can help writing code that's
>>>> portable without surprises.
>>> This begs the rookie question:
>>>
>>> What is the portable way to determine an aribtrary user's home directory
>>> then, if ~username is not portable across shells?
>>>
>>> Does one just have to grep and awk /etc/passwd?  Is the format of
>>> /etc/passwd portable, such that one standard grep/awk sequence will 
>>> portably return the home directory for user "username"?
>> 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. 
> 
> TIMTOWTDI:
> 
> pw user show -n $username | cut -d: -f 9   (But pw(8) is FreeBSD specific)
> 
> perl -le "print +(getpwnam("$username"))[7];"
> 
> su  $username -c 'echo $HOME'  (But only if the script is running with
>                                 root privileges)
> 
> 
> Of course, none of these methods are guaranteed to work in all
> circumstances.  In which case, you might as well choose to program
> in a language or for an interpreter that is readily available on the
> systems you are writing the code for and that provides the functionality
> you need.  On FreeBSD that probably comes down to using ~username with
> /bin/sh (with liberal comments warning of uportable assumptions, of
> course) -- which should work unmodified with any of the *BSDs or MacOS X.
> If you program the rest of the script carefully it should also work with
> bash under Linux (and others) or even (I think) ksh on Solaris.
> 
> Portability is hard.
> 
> 	Cheers,
> 
> 	Matthew

Yes, but I don't know of any engineering firms right off hand that run
the BSDs. Quite a few engineering groups run solaris or linux, just
because the machines are donated or the number of support folks who are
familiar with the operating platform and can maintain the systems is higher.
- -Garrett
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.1 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFu8V9EnKyINQw/HARAtDZAKCtj1ZXIb3llJF9uyUwoVKtlINwcACfQguh
TyuMDnbUd/Jiw3nHVvyO2bQ=
=v2tL
-----END PGP SIGNATURE-----



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