Unix + Python ... together

Discussion in 'Mac Programming' started by OneDreamCloser, Sep 15, 2009.

  1. OneDreamCloser macrumors member

    Jun 16, 2008
    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 :( )

  2. jpyc7 macrumors 6502

    Mar 8, 2009
    Denver, CO
    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.
  3. OneDreamCloser thread starter macrumors member

    Jun 16, 2008
    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 )
  4. fredthefool macrumors newbie

    Jun 4, 2008
    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!
  5. OneDreamCloser thread starter macrumors member

    Jun 16, 2008
    that is basic knowledge about the bash shell that, obviously, i am not aware of, thanx

Share This Page