From owner-freebsd-standards@FreeBSD.ORG Fri Nov 24 13:21:57 2006 Return-Path: X-Original-To: freebsd-standards@FreeBSD.ORG Delivered-To: freebsd-standards@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 27A9316A4A0 for ; Fri, 24 Nov 2006 13:21:57 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (lurza.secnetix.de [83.120.8.8]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1CF1E43D58 for ; Fri, 24 Nov 2006 13:21:13 +0000 (GMT) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (kdqted@localhost [127.0.0.1]) by lurza.secnetix.de (8.13.4/8.13.4) with ESMTP id kAODLm0L059665; Fri, 24 Nov 2006 14:21:54 +0100 (CET) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.13.4/8.13.1/Submit) id kAODLlu6059664; Fri, 24 Nov 2006 14:21:47 +0100 (CET) (envelope-from olli) Date: Fri, 24 Nov 2006 14:21:47 +0100 (CET) Message-Id: <200611241321.kAODLlu6059664@lurza.secnetix.de> From: Oliver Fromme To: freebsd-standards@FreeBSD.ORG, sa2c@sa2c.net In-Reply-To: <7a2b18c80611240418w43776195p340d1ff49ac69786@mail.gmail.com> X-Newsgroups: list.freebsd-standards User-Agent: tin/1.8.2-20060425 ("Shillay") (UNIX) (FreeBSD/4.11-STABLE (i386)) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.1.2 (lurza.secnetix.de [127.0.0.1]); Fri, 24 Nov 2006 14:21:54 +0100 (CET) Cc: Subject: Re: type command bahaviour of FreeBSD's /bin/sh (Fwd: [issue426] ?hgmerge assumes FileMerge to be exist unconditionally on FreeBSD) X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-standards@FreeBSD.ORG, sa2c@sa2c.net List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Nov 2006 13:21:57 -0000 NIIMI Satoshi wrote: > FreeBSD's /bin/sh produces: > | % sh -c 'type /nonexistent; echo $?' > | /nonexistent: No such file or directory > | 0 Note that the "type" utility is normally not called with a full path, but with a command name. In that case it behaves correctly: $ type asdfg asdfg: not found $ echo $? 127 If you have a full path name and need to know whether it exists, you should rather use the "test" utility instead of "type". The purpose of "type" is to find out how a _name_ would be interpreted when given as a command (i.e. keyword, builtin, alias, shell function or $PATH expansion). > Does the current behaviour of /bin/sh's type command conform to the standards? SUSv3/POSIX2001 is not very clear, unfortunately. It doesn't exactly explain how the "type" utility should behave when given a non-existing absolute path. I tend to believe that it's not a bug in FreeBSD, but rather a valid interpretation of the standard. > > Because of the above feature (bug?), testing > > "/Developer/.../FileMerge" always succeeds and used unconditionally. > > I made a patch to work around this. As I said above, it is _wrong_ to use "type" for testing the existence of a file whose full path is known. Use "test" (a.k.a. "[") for that purpose: if [ -f "$FULLPATH" ]; then # ... found else # ... not found fi Even if you need to find out whether a name expands to an external command, it is _not_ sufficient to only check the return code of "type". You also need to parse the output in order to check whether it returned a full path (as opposite to "shell builtin", "alias" or whatever). In fact it is non-trivial, because the output format of the "type" builtin is not standardized and varies between shells. Therefore, if you need to find a command in the $PATH list, then it's better to do that yourself using a shell loop, like this: FULLPATH="" OLD_IFS="$IFS" IFS=":" for DIR in $PATH; do if [ -x "$DIR/$COMMAND" ]; then FULLPATH="$DIR/$COMMAND" break fi done IFS="$OLD_IFS" if [ -n "$FULLPATH" ]; then echo "Found: $FULLPATH" else echo "$COMMAND not found" fi My conclusion is that you should _never_ use "type" in portable shell scripts. Best regards Oliver -- Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing Dienstleistungen mit Schwerpunkt FreeBSD: http://www.secnetix.de/bsd Any opinions expressed in this message may be personal to the author and may not necessarily reflect the opinions of secnetix in any way.