[SOLVED] Capturing output in a korn variable

Hi, I'm new to korn and having trouble capturing the text output from one program in an array that I can then feed into another program. Direct approaches didn't work, so I've tried to break it down thus:

The program lonlat2pixline gives the values I need in the second column, so I print that column with awk:

$ lonlat2pixline -x 2 -y 2 $afile $lon $lat | awk -F= '{print $2}'
-159.814606
24.181808
474
478
342
346

Now I try to capture those numbers in a variable:

$ output=$(lonlat2pixline -x 2 -y 2 $afile $lon $lat | awk -F= '{print $2}')
$ echo $output
-159.814606 24.181808 474 478 342 346

Unfortunately, this is not an array yet, but a scalar. I therefore try to make it into an array using set -A:

$ set -A outarray $output

...and I get the following error:

ksh: set: -1: unknown option
ksh: set: -5: unknown option
ksh: set: -9: unknown option
ksh: set: -.: unknown option
ksh: set: -8: unknown option
ksh: set: -1: unknown option
ksh: set: -4: unknown option
ksh: set: -6: unknown option
ksh: set: -0: unknown option
ksh: set: -6: unknown option
Usage: set [-sabefhkmnprtuvxBCGH] [-A name] [-o[option]] [arg ...]
$ 

Thanks in advance.

You need to give it the -- option to tell it that negative numbers should be taken literally instead of being considered commandline arguments.

1 Like

Wow, you're fast. Perfect. Thanks again!

---------- Post updated at 02:41 PM ---------- Previous update was at 01:59 PM ----------

Hold the phone. That worked brilliantly on the command line (in ksh of course), but in my script, it behaved differently. Here are the important bits:

#!/bin/ksh
.
.
.
output=$(lonlat2pixline -x 2 -y 2 $afile $lon $lat | awk -F= '{print $2}')
        set -A outarr -- $output
echo $output
echo $outarr
        sline=${outarr[2]}
        .
.
echo ${outarr[2]}
echo $sline

...which comes up with:

-159.814606
24.181808
474
478
342
346
-159.814606
24.181808
474
478
342
346


$

That's two blank lines at the end where the array elements should be. So outarr is still a single element variable.

When I run the lines on the ksh command line, I get:

$ output=$(lonlat2pixline -x 2 -y 2 $afile $lon $lat | awk -F= '{print $2}')
$ set -A outarr -- $output
$ echo $output
-159.814606 24.181808 474 478 342 346
$ echo $outarr
-159.814606
$ sline=${outarr[2]}      
$ eline=${outarr[3]}
$ spixl=${outarr[4]}
$ epixl=${outarr[5]}
$ echo "l2extract $afile $spixl $epixl $sline $eline 1 1 ${afile}_SUB"
l2extract /data/Imagery/GLOBAL/modisa/L2/A2003061001000.L2_LAC_OC 342 346 474 478 1 1 /data/Imagery/GLOBAL/modisa/L2/A2003061001000.L2_LAC_OC_SUB

Have you altered the value of IFS anywhere? That will alter what all splitting splits upon, including ordinary unquoted $variable splitting.

Post the whole script. not . . .

Yes indeed. Was embarrassed to post all my spaghetti, but here it is:
(PS. was forced to chop up the urls since this is a new account)

PPS. Got it squared from your comments re IFS this afternoon, cut my code back out of the post.
Thanks again!

1 Like