Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Feb 2016 15:29:22 +0100
From:      Jan Henrik Sylvester <me@janh.de>
To:        Sergei G <sergeig.public@gmail.com>
Cc:        questions-list freebsd <freebsd-questions@freebsd.org>
Subject:   Re: /bin/sh starts with check in script
Message-ID:  <56BC9AC2.3070805@janh.de>
In-Reply-To: <56BB33E8.4080900@gmail.com>
References:  <56BB33E8.4080900@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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
> 
> 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).

Cheers,
Jan Henrik



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?56BC9AC2.3070805>