Passing a bash array to a python list

I am trying to pass an array from bash to python using the old getenv method, but I keep getting this error:

./crcFiles.sh: line 7: export: `0021': not a valid identifier Traceback (most recent call last): File "/shares/web/vm3618/optiload/prog/legalLitres.py", line 30, in <module> for i in mdcArray.split(' '): AttributeError: 'NoneType' object has no attribute 'split' 

can someone explain why $ mdcNo did not switch from bash to python successfully?

Code .sh:

 #!/bin/bash mdcNo=('0021' '0022' '0036' '0055' '0057' '0059' '0061' '0062' '0063' '0065' '0066' '0086' '0095' '0098' '0106' '0110' '0113' '0114' '0115' '0121' '0126' '0128' '0135' '0141' '0143' '0153' '0155' '0158') localDIR=/shares/web/vm3618/optiload/prog export mdcNo $localDIR/legalLitres.py for i in "${mdcNo[@]}" do echo $i cp $localDIR/MDC$i/*/QqTrkRec.txt $localDIR/crccalc/. cd $localDIR/crccalc ./crccalc.py QqTrkRec.txt cp $localDIR/crccalc/QqTrkRec.txt $localDIR/MDC$i/. done 

code.py:

 #!/usr/bin/python import glob import os mdcArray = os.getenv('mdcNo') #Legal Litres that hex and decimal legalLitresHex = "47E0" legalLitresTxt = '18,400' # file name and Legal Litres header legalHeader = ":00F0:" hexFile = "QqTrkRec.txt" # insert comment to explain change comment = "#\n# 2015 Nov 20: Legal Litres changed to 18,400\n#\n" commentFlag0 = "# SetDATA" commentFlag1 = "# SetDATA" try: for i in mdcArray.split(' '): line = "" Qqfile = glob.glob("/shares/web/vm3618/optiload/prog/MDC"+i+"/*/"+hexFile) outFile = Qqfile[0]+".new" print i 
+5
source share
2 answers

When you export variable from the shell, what you really do is add it to the POSIX "environment" array, which inherits all the child processes. But the POSIX environment is a flat array of strings name = value; it itself cannot contain arrays. Therefore, Bash does not even try to put arrays. It will give you export array variable, and even set the "exported" flag for this variable, but the environment is not affected. You can verify this fact by running env or a new copy of bash and selecting the β€œexported” variable:

 $ export myArr=(this is an array) $ bash -c 'echo "${myArr[@]}"' $ 

(Some other shells with an array, in particular ksh, actually export the array variable to the environment, but the exported value will consist only of the first element of the array.)

If you want to pass a shell array to a Python script, it is best to do this as command line arguments. If you run the Python script as follows:

 python code.py "${mdcNo[@]}" 

... then Python code can just go through sys.argv , which is always a list. (In particular, the passed array will be a slice of sys.argv[1:] , since sys.argv[0] always specified by the name of the script itself.)

If this is not an option, you will have to set the environment variable to a string with some separator between the elements and split it inside the Python code. Something like that..

Bash:

 export mdcList='0021,0022,0036,0055,0057,0059,0061,0062,0063,0065,0066,0086,0095,0098,0106,0110,0113,0114,0115,0121,0126,0128,0135,0141,0143,0153,0155,0158' 

Or you can build a string from an array:

 export mdcList=${mdcNo[0]} for i in "${mdcNo[@]:1}"; do mdcList+=,$i done 

In any case, the Python script can restore the array as a list:

 mdc_no = os.getenv('mdcList').split(',') 

If your array elements are not just numbers, you can replace the comma with something less likely to be displayed in the element; the traditional choice would be the ASCII Unit Separator (U + 001F, $'\x1f' in Bash, '\x1f' in Python).

+9
source

I think Mark Reed has already given you a very good explanation and solution. However, have you considered using python argparse ?

 #!/usr/bin/env python import argparse def main(): parser = argparse.ArgumentParser() parser.add_argument('stuff', nargs='+') args = parser.parse_args() print args.stuff if __name__ == '__main__': main() 

Using:

 $ mdcNo=('0021' '0022' '0036' '0055' '0057' '0059' '0061' '0062' '0063' '0065' '0066' '0086' '0095' '0098' '0106' '0110' '0113' '0114' '0115' '0121' '0126' '0128' '0135' '0141' '0143' '0153' '0155' '0158') $ python argp.py "${mdcNo[@]}" ['0021', '0022', '0036', '0055', '0057', '0059', '0061', '0062', '0063', '0065', '0066', '0086', '0095', '0098', '0106', '0110', '0113', '0114', '0115', '0121', '0126', '0128', '0135', '0141', '0143', '0153', '0155', '0158'] 
+3
source

Source: https://habr.com/ru/post/1236439/


All Articles