ksh: convert space delimited string to array, but with '\ ' option

ksh: convert space delimited string to array, but with '\ ' option

Post by Chris Site » Thu, 13 Jan 2000 04:00:00



I'm trying to convert a space delimeted string to an array, but allow for
spaces in array elements by escaping with '\ '.

It seems to work manually...
# set -A array b\ cd efg
# print ${array[0]}
b cd

...but not from a variable
# input='b\ cd efg'
# set -A array $input
# print ${array[0]}
b\

How can I get around this?  Thanks.

--
Regards,
Chris Sites

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Ken Pizzi » Thu, 13 Jan 2000 04:00:00



Quote:>I'm trying to convert a space delimeted string to an array, but allow for
>spaces in array elements by escaping with '\ '.

>It seems to work manually...
># set -A array b\ cd efg
># print ${array[0]}
>b cd

>...but not from a variable
># input='b\ cd efg'
># set -A array $input
># print ${array[0]}
>b\

>How can I get around this?  Thanks.

eval.

  input='b\ cd efg'
  eval "set -A array $input"
  print ${array[0]}

                --Ken Pizzini

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Cal Dunniga » Thu, 13 Jan 2000 04:00:00


: I'm trying to convert a space delimeted string to an array, but allow for
: spaces in array elements by escaping with '\ '.

: It seems to work manually...
: # set -A array b\ cd efg
: # print ${array[0]}
: b cd

: ...but not from a variable
: # input='b\ cd efg'
: # set -A array $input
: # print ${array[0]}
: b\

You need to eval the "set" command so the shell can expand the variable correctly.
Oh, and you have to quote the variable.
  input='b\ cd efg'
  eval set -A array "$input"
  print ${array[0]}
  b cd

--
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

      Consulting                   wrong with a world in which Ken
      Modeling                     Thompson lives in obscurity and
      Training                     Bill Gates is a famous billionaire.
//////////////////////////////////////////////////////////////////////

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by David Lod » Fri, 14 Jan 2000 04:00:00


On 12 Jan 2000 18:36:49 GMT, Chris Sites


>I'm trying to convert a space delimeted string to an array, but allow for
>spaces in array elements by escaping with '\ '.
>...but not from a variable
># input='b\ cd efg'
># set -A array $input
># print ${array[0]}
>b\

You're going to kick yourself :-)

This is due to quoting - a space is preserved within a string if it
has been properly quoted ie:

ksh> input="b\ cd efg"
ksh> set -A array "${input"
ksh> print ${array[0]}
b cd
ksh>

The effects are subtly different; with you example, what is parsed by
the shell and passed to set -A is:
   set -A array b\ cd efg

When it's quoted what is passed is:
   set -A array "b cd" efg

dave

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Chris Site » Fri, 14 Jan 2000 04:00:00





>>I'm trying to convert a space delimeted string to an array, but allow for
>>spaces in array elements by escaping with '\ '.

>>It seems to work manually...
>># set -A array b\ cd efg
>># print ${array[0]}
>>b cd

>>...but not from a variable
>># input='b\ cd efg'
>># set -A array $input
>># print ${array[0]}
>>b\

>>How can I get around this?  Thanks.
> eval.
>   input='b\ cd efg'
>   eval "set -A array $input"
>   print ${array[0]}
>            --Ken Pizzini

Thanks. This helps some, but it has a side effect that I don't want.
Anything in 'input' gets evaluated.

# input='b\ cd efg'
# eval "set -A array $input $(date)"
# print ${array[0]}; print ${array[*]}
b cd
b cd efg Wed Jan 12 19:10:48 EST 2000

but I needed:
b cd
b cd efg $(date)

Any other ideas?

--Chris Sites

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Chris Site » Fri, 14 Jan 2000 04:00:00



> On 12 Jan 2000 18:36:49 GMT, Chris Sites

>>I'm trying to convert a space delimeted string to an array, but allow for
>>spaces in array elements by escaping with '\ '.
>>...but not from a variable
>># input='b\ cd efg'
>># set -A array $input
>># print ${array[0]}
>>b\
> You're going to kick yourself :-)
> This is due to quoting - a space is preserved within a string if it
> has been properly quoted ie:
> ksh> input="b\ cd efg"
> ksh> set -A array "${input"
> ksh> print ${array[0]}
> b cd
> ksh>
> The effects are subtly different; with you example, what is parsed by
> the shell and passed to set -A is:
>    set -A array b\ cd efg
> When it's quoted what is passed is:
>    set -A array "b cd" efg
> dave

Hmmm... This doesn't seem to work.

--Chris Sites

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Ken Pizzi » Fri, 14 Jan 2000 04:00:00



Quote:>>>It seems to work manually...
>>># set -A array b\ cd efg
>>># print ${array[0]}
>>>b cd

>>>...but not from a variable
>>># input='b\ cd efg'
>>># set -A array $input
>>># print ${array[0]}
>>>b\
>> eval.
>Thanks. This helps some, but it has a side effect that I don't want.
>Anything in 'input' gets evaluated.

># input='b\ cd efg'
># eval "set -A array $input $(date)"
># print ${array[0]}; print ${array[*]}
>b cd
>b cd efg Wed Jan 12 19:10:48 EST 2000

>but I needed:
>b cd
>b cd efg $(date)

You're mixing two different things up on the "eval" line --- a
variable which you do want eval'd and a command which you don't;
try:
   input='b\ cd efg'
   eval "set -A array $input '\$(date)'"
   print "${array[0]}"; print "${array[*]}"

It can sometimes be tricky to get an "eval" statement quoted
correctly, but the main thing is that after expansions, your
"eval" line should look just like it would if you were to enter
it manually.  If you want the three array elements "b cd",
"efg", "$(date)", then the above works because it expands to:
   set -A array b\ cd efg '$(date)'
whereas the one you were complaining about expanded to:
   set -A array b\ cd efg Thu Jan 13 01:08:10 UTC 2000
due to the command substitution.

To get an idea of what an "eval" is going to do, you might
replace the "eval" keyword with the "echo" command:
   echo "set -A array $input '\$(date)'"
   echo "set -A array $input $(date)"
will show the two expansions I placed in the previous paragraph.

                --Ken Pizzini

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Chris Site » Fri, 14 Jan 2000 04:00:00





>>>>It seems to work manually...
>>>># set -A array b\ cd efg
>>>># print ${array[0]}
>>>>b cd

>>>>...but not from a variable
>>>># input='b\ cd efg'
>>>># set -A array $input
>>>># print ${array[0]}
>>>>b\
>>> eval.
>>Thanks. This helps some, but it has a side effect that I don't want.
>>Anything in 'input' gets evaluated.

>># input='b\ cd efg'
>># eval "set -A array $input $(date)"
>># print ${array[0]}; print ${array[*]}
>>b cd
>>b cd efg Wed Jan 12 19:10:48 EST 2000

>>but I needed:
>>b cd
>>b cd efg $(date)
> You're mixing two different things up on the "eval" line --- a
> variable which you do want eval'd and a command which you don't;
> try:
>    input='b\ cd efg'
>    eval "set -A array $input '\$(date)'"
>    print "${array[0]}"; print "${array[*]}"
> It can sometimes be tricky to get an "eval" statement quoted
> correctly, but the main thing is that after expansions, your
> "eval" line should look just like it would if you were to enter
> it manually.  If you want the three array elements "b cd",
> "efg", "$(date)", then the above works because it expands to:
>    set -A array b\ cd efg '$(date)'
> whereas the one you were complaining about expanded to:
>    set -A array b\ cd efg Thu Jan 13 01:08:10 UTC 2000
> due to the command substitution.
> To get an idea of what an "eval" is going to do, you might
> replace the "eval" keyword with the "echo" command:
>    echo "set -A array $input '\$(date)'"
>    echo "set -A array $input $(date)"
> will show the two expansions I placed in the previous paragraph.
>            --Ken Pizzini

I'm sorry.  Now I feel stupid.  I put the $(date) in the wrong place in my
example.  Bottom line is that I don't know what might be in the variable,
and I just want the user to be able to escape spaces on input to the
variable so an array element is allowed to have a space in it.  And
the only thing I wanted to require escapes for is spaces, because a
space is the column (or array element) delimiter.  BTW, the data is coming
from a file via a 'while read -r input; do' loop.

$ input='french\ horn flute $(date) tuba'
$ eval "set -A array $input"
$ print ${array[0]}; print ${array[*]}
french horn
french horn flute Wed Jan 12 23:15:55 EST 2000 tuba

but I wanted...
french horn
french horn flute $(date) tuba

...perhaps this is just a case of wanting my cake and eating it too,
unless maybe there's some other solution besides eval.  Thanks.

--Chris Sites

 
 
 

ksh: convert space delimited string to array, but with '\ ' option

Post by Ken Pizzi » Fri, 14 Jan 2000 04:00:00



Quote:>  Bottom line is that I don't know what might be in the variable,
>and I just want the user to be able to escape spaces on input to the
>variable so an array element is allowed to have a space in it.  And
>the only thing I wanted to require escapes for is spaces, because a
>space is the column (or array element) delimiter.  BTW, the data is coming
>from a file via a 'while read -r input; do' loop.

>$ input='french\ horn flute $(date) tuba'
>$ eval "set -A array $input"
>$ print ${array[0]}; print ${array[*]}
>french horn
>french horn flute Wed Jan 12 23:15:55 EST 2000 tuba

>but I wanted...
>french horn
>french horn flute $(date) tuba

>...perhaps this is just a case of wanting my cake and eating it too,
>unless maybe there's some other solution besides eval.  Thanks.

eval is still appropriate, but you've introduced a new layer of
complexity --- you need to protect things other than escaped
spaces from being interpreted during the eval while leaving
escaped spaces as escaped spaces.  Here's one way:
  eval "set -A array $(print -- "$input" |



Note that with:
  input='french\\ horn flute $(date) tuba'
this will make ${array[0]} be:
  french\ horn
which may or may not be the desired result (the other likely
possibility being:
  french\
with ${array[1]} being "horn"; i.e., \\ sequences should be
interpreted as an escaped \, not as two independent \s.)

                --Ken Pizzini

 
 
 

1. in 'getopt()', how to handle multiple options in the 'optarg' string?

Hi All,

Pardon the possible idiocy of this question:

I'm trying to use the 'getopt' routine for handling command-line flags and
while most of it works like a charm, I need to have 1 flag that has 2
arguments:

progname -g 10 filename  yadda yadda yadda

optarg is declared as an 'extern char *', as per the man page for this,
and after the call to getopt, is supposed to point to the beginning of the
variable string.  I read this to mean that after the call, optarg should
point to the string "10 filename".

However, using an intermediate pointer 'tempstr' (because I can't examine
'optarg' directly for some reason (Is that a clue?)), I see with my
debugger that after:
   .
   .
while ((c = getopt(argc, argv, "Lsvqhf:n:o:m:M:b:e:R:g:l:t:T:C:F:w:")) != EOF)
   .
   .
tempstr = optarg;

<now look at tempstr in memory, from the above example>

1 0 \0 f i l e n a m e \0

it looks like it's there but as 2 null-terminated strings instead of one,
so I can't read it in with a 'sscanf()' or suchlike.

Two questions:
1) Is this the correct action of getopt? (almost certainly 'yes' - I'm a
novice at this)

2) What is the usual way of extracting multiple options per flag using getopt?

Thanks in advance for any enlightenment.
Cheers
harry
--
Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC
Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598
--
Harry J Mangalam, MolBio+Biochem / Dev+Cell Bio, Rm 4201, BioSciII  UC Irvine, Irvine, CA, 92717, (714) 824-4824, fax (714) 824 8598

2. tools for cleaning log files of "robot" entries?

3. Convert String to Array (ksh)

4. Netscape ?

5. How to convert a 'variable name' to a string in C++

6. 24/8 bit problem, red is blue

7. why does 'join' convert my tabs to spaces?

8. HELP: UMSDOS

9. ksh: parsing a string for 'invalid' chars

10. ksh question: Looking for an '@' in string

11. dip 3.3.7n-uri: how to insert space in string for 'wait' command?

12. '-i' option for 'cp' and 'mv'

13. malloc, array's, pointer array's AGHHHH!!!