Re: Adding prefixes

Re: Adding prefixes

am 06.10.2007 00:41:23 von cfajohnson

On 2007-10-05, Heruan wrote:
> I have a shell variable in a bash function like this:
>
> DOMAINS="abc.tld def.tld ghi.tld ..."
>
> I wish to add a prefix to all those domains and get:
>
> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>
> How can I achieve this?

With a recent version (3.?) of bash:

printf -v DOMAINS "host.%s " $DOMAINS

With older versions:

DOMAINS=$( printf "host.%s " $DOMAINS )

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Adding prefixes

am 06.10.2007 17:25:15 von Michael Tosch

Chris F.A. Johnson wrote:
> On 2007-10-05, Heruan wrote:
>> I have a shell variable in a bash function like this:
>>
>> DOMAINS="abc.tld def.tld ghi.tld ..."
>>
>> I wish to add a prefix to all those domains and get:
>>
>> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>>
>> How can I achieve this?
>
> With a recent version (3.?) of bash:
>
> printf -v DOMAINS "host.%s " $DOMAINS
>
> With older versions:
>
> DOMAINS=$( printf "host.%s " $DOMAINS )
>

Applause, applause!
Perfect and elegant and fast.

I often forget about the "repetitiveness" in printf.

--
Michael Tosch @ hp : com

Re: Adding prefixes

am 06.10.2007 17:56:17 von Stephane CHAZELAS

2007-10-06, 17:25(+02), Michael Tosch:
> Chris F.A. Johnson wrote:
>> On 2007-10-05, Heruan wrote:
>>> I have a shell variable in a bash function like this:
>>>
>>> DOMAINS="abc.tld def.tld ghi.tld ..."
>>>
>>> I wish to add a prefix to all those domains and get:
>>>
>>> DOMAINS="host.abc.tld host.def.tld host.ghi.tld ..."
>>>
>>> How can I achieve this?
>>
>> With a recent version (3.?) of bash:
>>
>> printf -v DOMAINS "host.%s " $DOMAINS
>>
>> With older versions:
>>
>> DOMAINS=$( printf "host.%s " $DOMAINS )
>>
>
> Applause, applause!
> Perfect and elegant and fast.

It should be noted that it assumes $IFS has not been modified
and that none of the domain names contains any wildcard
character and it also adds a trailing " ". If the original
$DOMAIN is empty, the new one becomes "host. ".

printf -v, as Chris said is bash only.

In zsh which has a real support for lists, you can do:

DOMAINS=(abc.tld def.tld ghi.tld)
DOMAINS=(host.$^DOMAINS)

In rc/es which is the shell with the most consistant variable
typing scheme:

DOMAINS = (abc.tld def.tld ghi.tld)
DOMAINS = (host. ^ $DOMAINS)

In zsh's completion, there's a "host" completion function shared
by all the commands that need to complete a host (ssh, rlogin,
ftp, finger @...). You can tweak its behavior with zstyles or
you can adapt it if for instance you want to support the
completion of domains in /etc/resolv.conf. Zsh has a built
support for completing arguments in several parts (like for the
autoconf configure options where you complete --pr to
complete options and --prefix=/us to complete the remaining
of the argument).

--
Stéphane