Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Feb 2009 22:46:24 +1100 (EST)
From:      Ian Smith <smithi@nimnet.asn.au>
To:        Giorgos Keramidas <keramida@ceid.upatras.gr>
Cc:        questions@freebsd.org
Subject:   Re: sh parameter substitution problem
Message-ID:  <20090211224120.F38905@sola.nimnet.asn.au>
In-Reply-To: <87ljsdusk3.fsf@kobe.laptop>
References:  <20090211213011.C38905@sola.nimnet.asn.au> <87ljsdusk3.fsf@kobe.laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 11 Feb 2009, Giorgos Keramidas wrote:
 > On Wed, 11 Feb 2009 21:47:17 +1100 (EST), Ian Smith <smithi@nimnet.asn.au> wrote:
 > > I'm getting nowhere trying to parse out IP addresses from strings of 
 > > this form in /bin/sh, which have been awk'd out of 'tail named.run':
 > >
 > >  addr='195.68.176.4#1440:'
 > >  addr='195.68.176.4#16811:'
 > >  addr='195.68.176.4#276:'
 > >
 > > sh(1) in hand, I've tried:
 > >
 > >  ip=${addr:%#*}
 > >  ip=${addr:%%#*}
 > >  ip=${addr:%[#]*}
 > >  ip=${addr:%%[#]*}
 > >
 > > but all of these report './testbit: 7: Syntax error: Bad substitution'
 > >
 > > How can I split these strings to exclude the '#' and all beyond, 
 > > preferably using sh syntax, at least without resorting to perl?
 > 
 > Remove the ':' part and quote the text to avoid parsing '#' as a comment
 > delimiter:
 > 
 >     $ addr='195.68.176.4#1440:'
 >     $ echo "${addr%#*}"
 >     195.68.176.4

Thankyou Giorgos,

just before yours arrived I'd twigged that the ':' was wrong there, and 
tried ip=${addr%#*} which worked fine.  I guess # within ${..} doesn't 
get taken as a comment .. which makes sense or these would always need 
to be double-quoted.

cheers, Ian



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