Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

OneDreamCloser

macrumors member
Original poster
Jun 16, 2008
30
0
hi all,

when i am running a Python script that i made, i get a list as a result
( i do not know the number of items stored in that list )

i am using a unix script to control various pieces of code,
sometime i call this python script,
HOW can i store the result of the python script
INTO an array defined in my unix script ?

( i have tried also to define an array in the unix script, for example set -a colors BLUE YELLOW GREY but when i do echo $colors or echo ${colors[1]},
it does not display anything :( )

thanx
 
Assuming the output of your python script is solely the array contents, something like this might work in the bash shell language:

colorarray=(`python script.py`)

The array elements should not contain any spaces, but if they do I think using quotation characters in the python script output might be sufficient.

I've never used arrays in bash, but apparently it is somewhat whitespace dependent. I couldn't put a space character before the opening parenthesis character.
 
jpyc7 what you said it was the most helpful comment i read today on this problem, thanx again
( it works when i have strings but not with numbers )
 
as bash interprets all command-line params as strings, your script sees _only_ strings in your array.
to handle the individual values, you can use the expr command, which converts the string (e.g. "1234.56") to the equivalent number (1234.56) you can assign to a variable.
to validate, if a string contains only digits or the decimal point, you probably should use a regexp.
and in case you want to "use" the values in your shell script: as far as i remember, bash can't deal with floats, just integers!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.