[SOLVED] Capturing output in a korn variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Capturing output in a korn variable
# 1  
Old 05-15-2012
[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:

Code:
$ 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:

Code:
$ 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:

Code:
$ set -A outarray $output

...and I get the following error:

Code:
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.

Last edited by Corona688; 05-15-2012 at 03:04 PM..
# 2  
Old 05-15-2012
You need to give it the -- option to tell it that negative numbers should be taken literally instead of being considered commandline arguments.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-15-2012
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:

Code:
#!/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:
Code:
-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:
Code:
$ 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

Moderator's Comments:
Mod Comment Code tags for code, please.
# 4  
Old 05-15-2012
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 . . .
# 5  
Old 05-15-2012
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!

Last edited by daurin; 05-15-2012 at 08:44 PM.. Reason: Code tags
This User Gave Thanks to daurin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Korn Shell execution

There are two Korn Shell scripts : script_1.ksh ( located in /home/dir1 ) script_2.ksh ( located in /home/dir2 ) Content of script_2.ksh is #!/usr/bin/ksh echo "Hello world.." The script_2.ksh is called from within script_1.ksh using the following command : ./home/dir2/script_2.ksh but... (7 Replies)
Discussion started by: kumarjt
7 Replies

2. Shell Programming and Scripting

[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays , The following code works for ctcol in 2 3 4 5 6; do set -A a$ctcol $(cut -d, -f $ctcol test_file) done how ever this does not work for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

3. Shell Programming and Scripting

Capturing Output?

Hello All, I'm writing a Bash Script and in it I execute a piped command within a Function I wrote and I can't seem to redirect the stderr from the 1st pipe to stdout..? I'm setting the output to an Array "COMMAND_OUTPUT" and splitting on newlines using this --> "( $(...) )". By putting... (6 Replies)
Discussion started by: mrm5102
6 Replies

4. UNIX for Dummies Questions & Answers

[solved]Korn, disabling * substitution

If I execute the following line of code: echo "*" I get a list of files in the current directory. What if all I wanted to do was display the asterisk itself? What does the code have to look like so all I get is an asterisk? Thanks ahead of time for your assistance ----------... (3 Replies)
Discussion started by: Wreckoning
3 Replies

5. UNIX for Dummies Questions & Answers

Solved: how to save an output to a variable

i want to save the output of /scripts/whoowns domain.com to a username like $user = /scripts/whoowns domain.com but I'm not sure how to do that This is inside a bash script how can I get the output of /scripts/whoowns then save that to a variable? thanks! ---------- Post updated at... (0 Replies)
Discussion started by: vanessafan99
0 Replies

6. Shell Programming and Scripting

[Solved] Output in bash script not captured in variable

I'm tring to write down a simple script that would execute a command and wait until it returns a specific result. This is what i did: bjobs_out=`bjobs` while ]; do bjobs_out=`bjobs` sleep 6 done It seems to work until the command 'jobs' return the list of jobs in execution, but... (4 Replies)
Discussion started by: lifedj
4 Replies

7. Shell Programming and Scripting

Capturing output of procedure in variable in shell script

Hi guys I am calling one DB2 stored proc through unix. It is giving me below output. I want to capture the value 150 in one UNIX variable in shell script. Please let me know how I can achieve this. Thanks in advance Value of output parameters -------------------------- Parameter Name :... (5 Replies)
Discussion started by: vnimavat
5 Replies

8. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

9. Shell Programming and Scripting

Losing new lines when capturing output to variable

Explain this? $ ls | grep -e "crd\|cs" crd cs $ CLONES=`ls | grep -e "crd\|cs"`;echo $CLONES; crd cs $ CLONES=`ls | grep -e "crd\|cs"`;echo "$CLONES"; crd cs (1 Reply)
Discussion started by: blasto333
1 Replies

10. UNIX for Dummies Questions & Answers

capturing the output of grep as integer variable

Hi, I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value! For example, ps -ef|grep $project | grep -v grep | nawk '{print $2}' This returns my number. How... (2 Replies)
Discussion started by: babariba
2 Replies
Login or Register to Ask a Question