Unix Shell - ksh variable substitution quandry

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2005 > ksh variable substitution quandry





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 ksh variable substitution quandry
Shell Ponderer

2005-10-24, 3:45 pm

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

Shell Ponderer

2005-10-24, 3:45 pm

Excuse me - in the second example I meant:

$ echo ${var:3}

and get "now".

base60

2005-10-24, 3:45 pm

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.

Janis Papanagnou

2005-10-24, 3:45 pm

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
Chris F.A. Johnson

2005-10-24, 3:45 pm

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 <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Chris F.A. Johnson

2005-10-24, 5:48 pm

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:

<http://cfaj.freeshell.org/google>

> Thanks for the responses to this one.




--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Shell Ponderer

2005-10-25, 7:48 am

Chris F.A. Johnson wrote:
> On 2005-10-24, Shell Ponderer wrote:
>
> 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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com