From owner-freebsd-questions@FreeBSD.ORG Sat Jan 27 10:53:00 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 219A116A401 for ; Sat, 27 Jan 2007 10:53:00 +0000 (UTC) (envelope-from m.seaman@infracaninophile.co.uk) Received: from smtp.infracaninophile.co.uk (ns0.infracaninophile.co.uk [81.187.76.162]) by mx1.freebsd.org (Postfix) with ESMTP id 8483713C46C for ; Sat, 27 Jan 2007 10:52:59 +0000 (UTC) (envelope-from m.seaman@infracaninophile.co.uk) Received: from [IPv6:::1] (localhost.infracaninophile.co.uk [IPv6:::1]) by smtp.infracaninophile.co.uk (8.13.8/8.13.8) with ESMTP id l0RAqXPI085516; Sat, 27 Jan 2007 10:52:33 GMT (envelope-from m.seaman@infracaninophile.co.uk) Message-ID: <45BB2EEB.10204@infracaninophile.co.uk> Date: Sat, 27 Jan 2007 10:52:27 +0000 From: Matthew Seaman Organization: Infracaninophile User-Agent: Thunderbird 1.5.0.9 (X11/20070120) MIME-Version: 1.0 To: freebsd-questions@freebsd.org, Bill Campbell References: <20070126175122.64D2616A500@hub.freebsd.org> <20070126230241.GA93074@ns.umpquanet.com> <20070126231913.GA12604@ayn.mi.celestial.com> In-Reply-To: <20070126231913.GA12604@ayn.mi.celestial.com> X-Enigmail-Version: 0.94.0.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------enig92C06122C2731982EB59DC58" X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (smtp.infracaninophile.co.uk [IPv6:::1]); Sat, 27 Jan 2007 10:52:49 +0000 (GMT) X-Virus-Scanned: ClamAV 0.88.7/2493/Fri Jan 26 12:00:46 2007 on happy-idiot-talk.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00, DKIM_POLICY_TESTING,NO_RELAYS autolearn=ham version=3.1.7 X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on happy-idiot-talk.infracaninophile.co.uk Cc: Subject: Re: [OT] Does "~" always point to $HOME? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Jan 2007 10:53:00 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig92C06122C2731982EB59DC58 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: quoted-printable 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 >>> Subject: Re: [OT] Does "~" always point to $HOME? >>> To: freebsd-questions@freebsd.org, Bill Campbell >>> >>> Message-ID: <20070126172244.GB6575@ayn.mi.celestial.com> >>> Content-Type: text/plain; charset=3Dus-ascii >>> >>> My point isn't whether the FreeBSD /bin/sh expands it, but that not a= ll >>> systems are FreeBSD, and that one can have problems on other *NIX sys= tems. >>> >>> 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 directo= ry >> 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=20 >> portably return the home directory for user "username"? >=20 > Probably the most portable way to do this would be to use awk. A > simple script, homedir, might look like this: >=20 > #!/bin/sh > # getting the backwhacks correct is sometimes ``interesting'' > homedir=3D`awk -F: "/^$1:/{print \\$6}" /etc/passwd` >=20 > [ -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.=20 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 --=20 Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW --------------enig92C06122C2731982EB59DC58 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.1 (FreeBSD) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFuy7x8Mjk52CukIwRCN6XAKCIvj4rwMdCusSgH4HnZQsKAe09+QCcC9Z1 zfUPQwdFZh9wj0OF64RuxvQ= =TtQD -----END PGP SIGNATURE----- --------------enig92C06122C2731982EB59DC58--