Unix Shell - Variable Substitution Question (KSH88)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Variable Substitution Question (KSH88)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Variable Substitution Question (KSH88)
Sparky

2004-10-02, 9:12 pm

Anyone know why this fails to display anything:

#!/usr/bin/ksh

x=
y="abc.123"

echo "${x##*.:-${y##*.}}"

exit 0

I would have expected a display of 123
Chris F.A. Johnson

2004-10-02, 9:12 pm

On 2004-10-01, Sparky wrote:
> Anyone know why this fails to display anything:
>
> #!/usr/bin/ksh
>
> x=
> y="abc.123"
>
> echo "${x##*.:-${y##*.}}"
>
> exit 0
>
> I would have expected a display of 123


You cannot combine different types of parameter expansion in one
operation:

$ x='qwer.:-123456'
$ echo "${x##*.:-${y##*.}}"
456

":-" is part of the pattern you are removing from the value.

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Stephane CHAZELAS

2004-10-02, 9:12 pm

2004-10-1, 04:52(+00), Chris F.A. Johnson:
[...]

It's ${x##<pattern>} where <pattern> is *.:-${y##*.} of course.
[vbcol=seagreen]
> You cannot combine different types of parameter expansion in one
> operation:


YMMV. You can try with bash and ksh93, but it's often bogus (not
in that case of course).

zsh explicitely allows it and it's documented.

$ x=abc.efg y=hij.
$ echo ${${x##*.}:-foo}
efg
$ echo ${${y##*.}:-foo}
foo

--
Stephane
Dana French

2004-10-02, 9:12 pm

Sparky <tyates@newsguy.com> wrote in message news:<7o9pl0tu8nnsopmuoaggmdefcc2gqjv83h@4ax.com>...
> Anyone know why this fails to display anything:
>
> #!/usr/bin/ksh
>
> x=
> y="abc.123"
>
> echo "${x##*.:-${y##*.}}"
>
> exit 0
>
> I would have expected a display of 123


It's because you are performing multiple variable operations at the
same time on variable "x". Just do them one at a time or change it as
follows:

x=
y="abc.123"
echo "${x:-${y##*.}}"


Dana French
Stephane CHAZELAS

2004-10-03, 5:55 pm

2004-10-1, 04:52(+00), Chris F.A. Johnson:
[...]

It's ${x##<pattern>} where <pattern> is *.:-${y##*.} of course.
[vbcol=seagreen]
> You cannot combine different types of parameter expansion in one
> operation:


YMMV. You can try with bash and ksh93, but it's often bogus (not
in that case of course).

zsh explicitely allows it and it's documented.

$ x=abc.efg y=hij.
$ echo ${${x##*.}:-foo}
efg
$ echo ${${y##*.}:-foo}
foo

--
Stephane
Dana French

2004-10-04, 6:01 pm

Sparky <tyates@newsguy.com> wrote in message news:<7o9pl0tu8nnsopmuoaggmdefcc2gqjv83h@4ax.com>...
> Anyone know why this fails to display anything:
>
> #!/usr/bin/ksh
>
> x=
> y="abc.123"
>
> echo "${x##*.:-${y##*.}}"
>
> exit 0
>
> I would have expected a display of 123


It's because you are performing multiple variable operations at the
same time on variable "x". Just do them one at a time or change it as
follows:

x=
y="abc.123"
echo "${x:-${y##*.}}"


Dana French
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com