1

I am querying an SQLite3 database like so:

input=$(-separator "," "SELECT field1,field2,field3 FROM table1")

and get this result:

Red,Yellow is a color,Blue

I need to insert this into an array, is there a way to set this result into an array in ksh without having the result return the field in quotation marks (which is not possible, as far as I know, from a query statement in sqlite)?

As in example:

#!/bin/ksh

IFS=','

input=(Red,Yellow is a color,Blue)

set -A array $input
print ${array[@]}
print ${array[0]}
print ${array[1]}
print ${array[2]}

If I run the above code I get this: $ Red Yellow Red Yellow

If you encapsulate the second element with quotation marks, like this:

#!/bin/ksh

IFS=','

input=(Red,"Yellow is a color",Blue)

set -A array $input
print ${array[@]}
print ${array[0]}
print ${array[1]}
print ${array[2]}

You get the proper result...

$
Red
Yellow is a color
Blue

The solution I need to know is, is there is a way I can set this array without the quotation marks in the second element. Or would a pre-process need to be done on the db result return to encapsulate the elements with quotation marks before inserting into the the array. If so, what would be a good starting place? I'd like to stay within ksh if I could.

  • Could you please edit and clarify what you get now and what you want to get? What quotation marks are you talking about? Where are they? Why not just parse them out before saving into the array? Also, please provide us with a minimal working example that we can test. Use an echo statement to print your data instead of the SQL command so we can also do it on our own systems. – terdon Mar 4 '15 at 19:06
  • In SQLite, it does not return the query statement in quotation marks separating the fields i.e - "Red" Yellow is a color" Blue, only commas, as in the example. – Carl Lindgren Mar 4 '15 at 19:12
  • Show us the exact script you're running, starting with the #!/bin/ksh line (but with input=… instead of the SQL command because we don't have your database). What you've posted does not produce the output that you claim so you must have run different code. – Gilles Mar 4 '15 at 23:18
  • Sorry I had the wrong name for the array (corrected)... The SQL statement is actually of little importance other than letting you know where and how I'm getting the data. – Carl Lindgren Mar 5 '15 at 0:08
2

The following works for me in mksh:

$ echo $KSH_VERSION
@(#)MIRBSD KSH R50 2014/10/07
$ x="Red,Yellow is a color,Blue"
$ oIFS=$IFS
$ IFS=,
$ y=($x)
$ IFS=$oIFS
$ echo ${y[1]}
Yellow is a color

I believe it should work the same way in all versions of ksh.

  • @CarlLindgren If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Mar 5 '15 at 14:56
  • My concern with this approach was that it wouldn't be inheritable, either forward (to another child scripts) or backward (to an invoking script). But it seems to be working. I learned Korn Shell with pdksh 15+ years ago using Cygwin and Mandrake 7.0, ksh93 is much better! – Carl Lindgren Mar 5 '15 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.