From owner-freebsd-questions@FreeBSD.ORG Fri Jan 26 23:15:13 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 AF19A16A405 for ; Fri, 26 Jan 2007 23:15:13 +0000 (UTC) (envelope-from bill@ayn.mi.celestial.com) Received: from ayn.mi.celestial.com (hayek.celestial.com [192.136.111.12]) by mx1.freebsd.org (Postfix) with ESMTP id 7890213C49D for ; Fri, 26 Jan 2007 23:15:13 +0000 (UTC) (envelope-from bill@ayn.mi.celestial.com) Received: from localhost (localhost [127.0.0.1]) by ayn.mi.celestial.com (Postfix) with ESMTP id AEC946860B908; Fri, 26 Jan 2007 15:19:13 -0800 (PST) X-Virus-Scanned: amavisd-new at mi.celestial.com Received: from ayn.mi.celestial.com ([127.0.0.1]) by localhost (ayn.mi.celestial.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id T8ofmdXaqHgs; Fri, 26 Jan 2007 15:19:13 -0800 (PST) Received: by ayn.mi.celestial.com (Postfix, from userid 203) id 9231E6800A4F4; Fri, 26 Jan 2007 15:19:13 -0800 (PST) Date: Fri, 26 Jan 2007 15:19:13 -0800 From: Bill Campbell To: freebsd-questions@freebsd.org, Bill Campbell Message-ID: <20070126231913.GA12604@ayn.mi.celestial.com> Mail-Followup-To: freebsd-questions@freebsd.org, Bill Campbell References: <20070126175122.64D2616A500@hub.freebsd.org> <20070126230241.GA93074@ns.umpquanet.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070126230241.GA93074@ns.umpquanet.com> User-Agent: Mutt/1.5.11 OpenPKG/2.5 Cc: Subject: Re: [OT] Does "~" always point to $HOME? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd@celestial.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Jan 2007 23:15:13 -0000 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=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 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 ``Fix reason firmly in her seat and call to her tribunal every fact, every opinion. Question with boldness even the existence of a God; because, if there is one, he must more approve of the homage of reason, than that of blindfolded fear.'' --Thomas Jefferson