Date: Sat, 13 Feb 2016 04:53:28 +1100 (EST) From: Ian Smith <smithi@nimnet.asn.au> To: Jan Henrik Sylvester <me@janh.de> Cc: Sergei G <sergeig.public@gmail.com>, freebsd-questions@freebsd.org Subject: Re: /bin/sh starts with check in script Message-ID: <20160213034617.D51785@sola.nimnet.asn.au> In-Reply-To: <mailman.83.1455278401.97224.freebsd-questions@freebsd.org> References: <mailman.83.1455278401.97224.freebsd-questions@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
In freebsd-questions Digest, Vol 610, Issue 5, Message: 1
On Thu, 11 Feb 2016 15:29:22 +0100 Jan Henrik Sylvester <me@janh.de> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20160213034617.D51785>
