将bash数组传递给python列表 [英] passing bash array to python list

查看:431
本文介绍了将bash数组传递给python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用旧的getenv方法将数组从bash传递到python,但是我一直收到此错误:

I'm trying to pass an array from bash to python using the old getenv method however 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'

有人可以解释一下为什么$ mdcNo不能从bash成功传递到python吗?

could someone please explain why the $mdcNo isn't passing from bash to python successfully?

代码.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

代码.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

推荐答案

当您从shell中export变量时,您真正要做的就是将其添加到所有子进程都继承的POSIX环境"数组中.但是POSIX环境是name = value字符串的平面数组;它本身不能包含数组.因此,Bash甚至都没有尝试将数组放置在那里.它可以让您export一个数组变量,甚至可以在该变量上设置"exported"标志,但不会影响环境.您可以通过运行envbash的新副本并寻找已导出"变量来验证这一事实:

When you export a variable from the shell, what you are really doing is adding it to the POSIX "environment" array that all child processes inherit. But the POSIX environment is a flat array of name=value strings; it cannot itself contain arrays. So Bash doesn't even attempt to put arrays there. It will let you export an array variable, and doing so even sets the "exported" flag on that variable, but the environment is not touched. You can verify this fact by running env or a new copy of bash and looking for the "exported" variable:

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

$

(其他一些带有数组的shell,尤其是ksh,实际上会将数组变量导出到环境中,但是导出的值将仅包含数组的第一个元素.)

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

如果要将shell数组传递给Python脚本,最好的选择是作为命令行参数.如果您像这样运行Python脚本:

If you want to pass a shell array to the Python script, your best bet is to do so as command line arguments. If you run the Python script like this:

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

...,那么Python代码可以遍历sys.argv,它始终是一个列表. (特别是,传入的数组将是切片sys.argv[1:],因为sys.argv[0]始终设置为脚本本身的名称.)

... then the Python code can just loop over sys.argv, which is always a list. (Specifically, the passed-in array will be the slice sys.argv[1:], since sys.argv[0] is always set to the name of the script itself.)

如果这不是一个选项,那么您必须将环境变量设置为在元素之间带有一些定界符的字符串,并将其在Python代码中拆分.像这样的东西.

If that's not an option, then you'll have to set the environment variable to a string with some delimiter between elements and split it inside the Python code. Something like this..

重击:

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 the string up from the array:

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

无论哪种方式,Python脚本都可以将数组恢复为列表,如下所示:

Either way, the Python script can recover the array as a list like this:

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

如果数组元素不仅仅是数字,则可以用不太可能出现在元素中的内容替换逗号;传统选择是ASCII单位分隔符(U + 001F,Bash中为$'\x1f',Python中为'\x1f').

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

这篇关于将bash数组传递给python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆