From owner-freebsd-hackers@FreeBSD.ORG Sat Aug 8 18:42:06 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8A2C7106566B for ; Sat, 8 Aug 2009 18:42:06 +0000 (UTC) (envelope-from dougb@FreeBSD.org) Received: from mail2.fluidhosting.com (mx21.fluidhosting.com [204.14.89.4]) by mx1.freebsd.org (Postfix) with ESMTP id 3FF0C8FC1F for ; Sat, 8 Aug 2009 18:42:06 +0000 (UTC) Received: (qmail 14372 invoked by uid 399); 8 Aug 2009 18:42:04 -0000 Received: from localhost (HELO foreign.dougb.net) (dougb@dougbarton.us@127.0.0.1) by localhost with ESMTPAM; 8 Aug 2009 18:42:04 -0000 X-Originating-IP: 127.0.0.1 X-Sender: dougb@dougbarton.us Message-ID: <4A7DC6F6.4010802@FreeBSD.org> Date: Sat, 08 Aug 2009 11:41:58 -0700 From: Doug Barton Organization: http://www.FreeBSD.org/ User-Agent: Thunderbird 2.0.0.22 (X11/20090729) MIME-Version: 1.0 To: rea-fbsd@codelabs.ru References: <4A7B1DB0.1040602@FreeBSD.org> In-Reply-To: X-Enigmail-Version: 0.95.7 OpenPGP: id=D5B2F0FB Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org Subject: Re: Problem in bin/sh stripping the * character through ${expansion%} X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 Aug 2009 18:42:06 -0000 Eygene Ryabinkin wrote: > Doug, good day. > > Thu, Aug 06, 2009 at 11:15:12AM -0700, Doug Barton wrote: >> I came across this problem during a recent portmaster update. When >> trying to strip off the * character using variable expansion in bin/sh >> it doesn't work. Other "special" characters do work if they are >> properly escaped. >> >> The attached mini-script clearly shows the problem: >> >> $ sh sh-strip-problem >> var before stripping: foo\* >> var after stripping: foo\* >> >> var before stripping: foo\$ >> var after stripping: foo\ > > According to the sh(1), it is not a problem. Namely, > - \* being unquoted at all will produce a lone '*'; Bzzzt! :) Backslash A backslash preserves the literal meaning of the following char- acter, with the exception of the newline character (`\n'). A backslash preceding a newline is treated as a line continuation. > - '*' when treated as the smallest pattern, will result in a stripping > of a zero-length string -- it is the smallest pattern in the case of > '*' that matches anything. I agree with you if I had done ${var%*}, but the backslash should be preventing the * from being interpreted as part of the variable expansion. > In order to strip the trailing star you should use > ----- > var=${var%[*]} > ----- > This gives you the pattern of '[*]' that is properly treated as the > single star -- it's a weird way to escape the star in the patterns. That's creative, and better than the kludge I worked up for portmaster, thanks. > I will try to look at the XCU to understand what is the POSIX > way of doing the things. Great, thanks. Doug -- This .signature sanitized for your protection