From owner-freebsd-questions@freebsd.org Fri Feb 12 17:53:34 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 02012AA6C59 for ; Fri, 12 Feb 2016 17:53:34 +0000 (UTC) (envelope-from smithi@nimnet.asn.au) Received: from sola.nimnet.asn.au (paqi.nimnet.asn.au [115.70.110.159]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7B4861E5F for ; Fri, 12 Feb 2016 17:53:32 +0000 (UTC) (envelope-from smithi@nimnet.asn.au) Received: from localhost (localhost [127.0.0.1]) by sola.nimnet.asn.au (8.14.2/8.14.2) with ESMTP id u1CHrTZw015506; Sat, 13 Feb 2016 04:53:29 +1100 (EST) (envelope-from smithi@nimnet.asn.au) Date: Sat, 13 Feb 2016 04:53:28 +1100 (EST) From: Ian Smith To: Jan Henrik Sylvester cc: Sergei G , freebsd-questions@freebsd.org Subject: Re: /bin/sh starts with check in script In-Reply-To: Message-ID: <20160213034617.D51785@sola.nimnet.asn.au> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Feb 2016 17:53:34 -0000 In freebsd-questions Digest, Vol 610, Issue 5, Message: 1 On Thu, 11 Feb 2016 15:29:22 +0100 Jan Henrik Sylvester wrote: > On 02/10/2016 13:58, Sergei G wrote: > > I came up with this solution to check if variable $line starts with a > > hash. Basically I am checking if line is a comment in the configuration > > file. > > > > #!/bin/sh > > if expr "${line}" : '#.*' > /dev/null; then > > echo Ignoring comment line > > fi expr(1) suggests preferring sh(1) for maths expressions and parsing :) > > I had to redirect to /dev/null, because expr prints a number to STDOUT. > > Is there a better way to do this kind of string matching check in > > /bin/sh (not bash)? > > [ "${line#\#}" != "$line" ] && echo comment > > See the Parameter Expansion section of sh(1). That looks like it ought to work, but does not here on stable/9; I don't think it honours the escaping when expecting a second '#' or other char? After some playing, this also finds comment lines that have optional whitespace before the first '#', not pinning comments only to column 1, while ignoring comments after other text. Not what everybody needs .. [ ! "`echo ${line%%#*} | tr -d [:blank:]`" ] && echo comment BUG|FEATURE: if $line is the null string it is taken to be a comment. cheers, Ian