/bin/sh starts with check in script

Matthew Seaman m.seaman at infracaninophile.co.uk
Wed Feb 10 13:20:21 UTC 2016


On 2016/02/10 12: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)?

When you say 'in /bin/sh' I take it you mean without calling any
external programs?  Otherwise the natural way would be to use grep(1).

Try:

#!/bin/sh

case ${line} in
   "#*")
	echo Ignoring comment line
	;;
   *)
	... do whatever ...
        ;;
esac

	Cheers,

	Matthew


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 972 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20160210/bcb91194/attachment.sig>


More information about the freebsd-questions mailing list