0

I have a korn shell script that calls in a Python script.
The Python script should return a variable length list of strings.
The ksh should capture those strings and do some more processing.

How do I return the list and capture it??

My current code:

Python test.py:

#!/usr/bin/python
import sys

list = [ 'the quick brown fox', 'jumped over the lazy', 'dogs' ]
for s in list:
        print s

Korn script test.ksh:

#!/bin/ksh
IFS=$'\n'
echo $IFS

for line in $(test.py)
do
echo "\nline:"
echo "$line"
done

Output:

test.ksh

 \

line:
the quick brow

line:
 fox
jumped over the lazy
dogs
Radamand
  • 175
  • 1
  • 12

3 Answers3

0

Try this:

for l in $list; do
    echo "$l
done

More specifically:

for l in "${list[@]}"; do
    echo "$l
done
Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51
0

The python script will just print stuff to stdout.

The ksh part will read each line into an array:

typeset -a values
python scrypt.py | while IFS= read -r line; do
    values+=( "$line" )
done

echo the first value is: "${value[0]}"
echo there are ${#value[@]} values.

Here's a different technique

output=$( python scrypt.py )
oldIFS=$IFS
IFS=$'\n'
lines=( $output )
IFS=$oldIFS

ksh88:

typeset -i i=0
python scrypt.py | while IFS= read -r line; do
    lines[i]="$line"
    i=i+1
done

echo the first line is: "${lines[0]}"
echo there are ${#lines[@]} lines
echo the lines:
printf "%s\n" "${lines[@]}"

When a variable has the "integer" attribute (variable i), assignments to it are implicitly done in arithmetic context, so the magical i=i+1 works.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

Short answer: use a for loop, subshell invocation (see assigning value to shell variable using a function return value from Python), and set the IFS to just newline.

See the following demonstrations:

First, create a python program that prints a variable-length list of strings:

$ cat > stringy.py
list = [ 'the quick brown fox', 'jumped over the lazy', 'dogs' ]
for s in list:
    print s
import sys
sys.exit(0)
<ctrl-D>

Demonstrate that it works:

$ python stringy.py
the quick brown fox
jumped over the lazy
dogs

Launch ksh:

$ ksh

Example #1: for loop, invoking subshell, standard IFS, no quoting:

$ for line in $(python stringy.py) ; do echo "$line" ; done # Edited: added double-quotes
the
quick
brown
fox
jumped
over
the
lazy
dogs

Example #2: for loop, invoking subshell, standard IFS, quoting:

$ for line in "$(python stringy.py)" ; do echo "$line" ; done # Edited: added double-quotes
the quick brown fox jumped over the lazy dogs

Example #3: for loop, invoking subshell, no quoting, IFS set to just newline:

$ IFS=$'\n'
$ echo $IFS

$ for line in $(python stringy.py) ; do echo $line ; done # Edited: added double-quotes
the quick brown fox
jumped over the lazy
dogs

Example #3 demonstrates how to solve the problem.

Community
  • 1
  • 1
Flortify
  • 619
  • 5
  • 15
  • 1
    inside the loop, quote `"$line"` -- sequences of whitespace and tabs will get collapsed to a single space otherwise. – glenn jackman May 08 '14 at 21:19
  • @glenn Technically, that is not correct in this **particular** instance, since `echo` handles a variable number of arguments, echoing them all. **But**, you make an **important** point: I agree 100% that if you were passing the args to a non-varargs function/executable then you would want to put double-quotes around the variable. I.e., if you were invoking `foo` instead of `echo`, then you would write `foo "$line"` instead of `foo $line`. Let me reiterate: it is better to quote it. I will edit the answer to show `echo "$line"` instead of `echo $line`. – Flortify Jun 05 '14 at 15:51
  • 1
    I mean this: `x=" foo bar baz "` -- then `echo $x` and `echo "$x"` give you different results. In many situations that's significant. – glenn jackman Jun 05 '14 at 19:14
  • @glennjackman YES, YOU ARE CORRECT, MY MISTAKE: the shell word-breaks the string and then the `echo` echoes each of the resulting "words" with a space between them. – Flortify Jun 06 '14 at 15:51