From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 6 19:46:17 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 A6582106564A for ; Thu, 6 Aug 2009 19:46:17 +0000 (UTC) (envelope-from rea-fbsd@codelabs.ru) Received: from 0.mx.codelabs.ru (0.mx.codelabs.ru [144.206.177.45]) by mx1.freebsd.org (Postfix) with ESMTP id 5A1328FC18 for ; Thu, 6 Aug 2009 19:46:17 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; q=dns; c=simple; s=one; d=codelabs.ru; h=Received:Date:From:To:Cc:Subject:Message-ID:Reply-To:References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To:Sender; b=Y8Ba8EL4nwU8J6fnH8qfZJFRP4kn1wc0o22zEr4GD22ZLqHRIKGso7MV+KVHHAWcg8CSV2GHI83IyvWhdXDc9MW2ARFigVFGyq9W1/e7Vdschd/SQxdEypn3ImmyZqCQqRuNa1UZYcD3LMES3w/7gQL9tiVUQCKuf/e1yHuYXp4=; Received: from phoenix.codelabs.ru (ppp91-77-10-19.pppoe.mtu-net.ru [91.77.10.19]) by 0.mx.codelabs.ru with esmtpsa (TLSv1:AES256-SHA:256) id 1MZ8bx-00010C-IQ; Thu, 06 Aug 2009 23:26:49 +0400 Date: Fri, 7 Aug 2009 03:26:50 +0400 From: Eygene Ryabinkin To: Doug Barton Message-ID: References: <4A7B1DB0.1040602@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4A7B1DB0.1040602@FreeBSD.org> Sender: rea-fbsd@codelabs.ru 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 Reply-To: rea-fbsd@codelabs.ru List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Aug 2009 19:46:17 -0000 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 '*'; - '*' 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. 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. Other characters work, but only because they produce no patterns. In principle, you can try \? that will remove any trailing character no matter what, since it is the second character that will produce the pattern. > In contrast, bash does the right thing: > > bash sh-strip-problem > var before stripping: foo\* > var after stripping: foo\ > > var before stripping: foo\$ > var after stripping: foo\ I will try to look at the XCU to understand what is the POSIX way of doing the things. -- Eygene _ ___ _.--. # \`.|\..----...-'` `-._.-'_.-'` # Remember that it is hard / ' ` , __.--' # to read the on-line manual )/' _/ \ `-_, / # while single-stepping the kernel. `-'" `"\_ ,_.-;_.-\_ ', fsc/as # _.-'_./ {_.' ; / # -- FreeBSD Developers handbook {_.-``-' {_/ #