sh parameter substitution problem

Giorgos Keramidas keramida at ceid.upatras.gr
Wed Feb 11 03:36:41 PST 2009


On Wed, 11 Feb 2009 21:47:17 +1100 (EST), Ian Smith <smithi at 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



More information about the freebsd-questions mailing list