ksh variable substitution quandry

ksh variable substitution quandry

am 20.10.2005 17:29:34 von Shell Ponderer

Maybe someone can help. Anyone know why this doesn't work in ksh
(mult. varieties it seems)?

$ var=heynow
$ echo ${heynow:3:2}

I expect "no" and get either:

ksh: "${var:3:3}": 0403-011 The specified substitution is not valid for
this command.

or

ksh: : bad substitution

depending on the ksh variety, but according to my O'Reilly ksh book
this is perfectly valid syntax.

Also I would expect that I could do:
$ echo ${heynow:3}

and get "now".

And yes, I realize I could just use cut or maybe a regex but it would
seem to me that if this is supposed to work it's the most elegant way
of handling the problem. It also works swimmingly in bash.

TIA -

The Ponderer

Re: ksh variable substitution quandry

am 20.10.2005 17:32:22 von Shell Ponderer

Excuse me - in the second example I meant:

$ echo ${var:3}

and get "now".

Re: ksh variable substitution quandry

am 20.10.2005 23:48:36 von base60

Shell Ponderer wrote:
> Maybe someone can help. Anyone know why this doesn't work in ksh
> (mult. varieties it seems)?
>
> $ var=heynow
> $ echo ${heynow:3:2}

This syntax isn't valid until you get to ksh93.

Re: ksh variable substitution quandry

am 21.10.2005 03:14:31 von Janis Papanagnou

Shell Ponderer wrote:
> Maybe someone can help. Anyone know why this doesn't work in ksh
> (mult. varieties it seems)?
>
> $ var=heynow
> $ echo ${heynow:3:2}
>
> I expect "no" and get either:
>
> ksh: "${var:3:3}": 0403-011 The specified substitution is not valid for
> this command.
>
> or
>
> ksh: : bad substitution
>
> depending on the ksh variety, but according to my O'Reilly ksh book
> this is perfectly valid syntax.
>
> Also I would expect that I could do:
> $ echo ${heynow:3}
>
> and get "now".
>
> And yes, I realize I could just use cut or maybe a regex but it would
> seem to me that if this is supposed to work it's the most elegant way
> of handling the problem. It also works swimmingly in bash.
>
> TIA -
>
> The Ponderer
>

On what platform are you working? Most likely you have either a ksh88
or a pdksh running; either won't support the substring substitution.
Type "set -o vi" and then "Ctrl-V" to obtain the version information.
You may want to download a ksh93 from www.kornshell.com

Janis

Re: ksh variable substitution quandry

am 21.10.2005 03:36:05 von cfajohnson

On 2005-10-20, Shell Ponderer wrote:
> Maybe someone can help. Anyone know why this doesn't work in ksh
> (mult. varieties it seems)?
>
> $ var=heynow
> $ echo ${heynow:3:2}
>
> I expect "no" and get either:
>
> ksh: "${var:3:3}": 0403-011 The specified substitution is not valid for
> this command.
>
> or
>
> ksh: : bad substitution
>
> depending on the ksh variety, but according to my O'Reilly ksh book
> this is perfectly valid syntax.
>
> Also I would expect that I could do:
> $ echo ${heynow:3}
>
> and get "now".
>
> And yes, I realize I could just use cut or maybe a regex but it would
> seem to me that if this is supposed to work it's the most elegant way
> of handling the problem. It also works swimmingly in bash.

The substring expansion is only available in ksh93 and bash; it's
not present in ksh88 or pdksh. Doesn't the book say that?

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: ksh variable substitution quandry

am 24.10.2005 22:42:23 von Shell Ponderer

No - I was looking for the subscript which would point me in that
direction and it wasn't there. Thanks for the responses to this one.

Re: ksh variable substitution quandry

am 24.10.2005 23:01:42 von cfajohnson

On 2005-10-24, Shell Ponderer wrote:
> No - I was looking for the subscript which would point me in that
> direction and it wasn't there.

What subscript wasn't where? Please read:



> Thanks for the responses to this one.



--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: ksh variable substitution quandry

am 25.10.2005 14:30:17 von Shell Ponderer

Chris F.A. Johnson wrote:
> On 2005-10-24, Shell Ponderer wrote:
> > No - I was looking for the subscript which would point me in that
> > direction and it wasn't there.
>
> What subscript wasn't where? Please read:

For Chris F.A. Johnson:

Subscript meaning subscript near/on the ${somevar:5} text to indicate a
footnote at the bottom of the page in the ksh O'Reilly book I was using
as a reference. I was expectng that this was a ksh93 feature and that
it might not be present in the versions of ksh I was using (pdksh and
AIX 5.2's ksh which appears to be antiquated) -- but I also expected as
is true in most cases within the book that a feature only available in
ksh93 would clearly be marked as such. Hopefully authors Rosenblatt
and Robbins are listening.