用于json数组的for循环中的python / bash变量扩展

时间:2013-07-09 00:11:31

标签: python json bash variables variable-expansion

这是我的问题。我有以下bash片段:

#!/bin/bash

R1=$(cat $HRESULTPATH/KEYLIST.OUT|grep "$KEYCHAIN")
H1=$(echo $R1|tr -d [[:space:]])
O1=$(echo $H1|tr -d '\"')
S1=$(echo $KEYCHAIN|tr -d [[:space:]])

export TESTCASE1=`echo "CLI - Test Case $tcindex - Was the Card read and listed as an active Keychain?"`
export OUTFILE1="KEYLIST.OUT"
if [[ $S1 =~ $O1 ]]; then
  export RESULT1=`echo -e "PASS"`
  export MESSAGE1=`echo -e "\"$KEYCHAIN\" was list as an active Keychain"`
else
  export RESULT1=`echo -e "FAIL"`

export MESSAGE1=`"\"$KEYCHAIN\" was not listed and not active"`
Rf=$(cat $HRESULTPATH/KEYLIST.OUT|grep "$KEYCHAIN")
Zf=$(echo $Rf|sed 's/~~*/~/g')
export ERRORDIFF1=`(echo -ne "\"$KEYCHAIN\" was expected");(echo " -- \"$Zf\" was captured from Guest")`
fi

tcindex=$((tcindex+1))

R2=$(cat $HRESULTPATH/DUMP.OUT|grep "$DUMP")
H2=$(echo $R2|tr -d [[:space:]])
O2=$(echo $H2|tr -d '\"')
S2=$(echo $DUMP|tr -d [[:space:]])

export TESTCASE2=`echo "CLI - Test Case $tcindex - Did the tool report the user in AD? and belonging to a Zone?"`
export OUTFILE2="DUMP.OUT"
if [[ $S2 =~ $O2 ]]; then
  export RESULT2=`echo -e "PASS"`
  export MESSAGE2=`echo -e "sctool -d reflected $SCUSER as \"in AD and zoned\""`
    else
  export RESULT2=`echo -e "FAIL"`
  export MESSAGE2=`echo -e "sctool -d did not reflect $SCUSER in AD or zoned or both"`
Rf=$(cat $HRESULTPATH/DUMP.OUT|grep -i "zone")
Zf=$(echo $Rf|sed 's/~~*/~/g')
export ERRORDIFF2=`(echo -ne "\"$DUMP\" was expected");(echo " -- \"$Zf\" was captured from Guest")`
fi

我对此做的是将“INDEXED”变量,例如“MESSAGE1,RESULT1或RESULT2 ... x 导出到json对象中然后呈现Web表单中的数据。这不是最优雅的解决方案,但它适用于我。我是“粗暴”强制创建python数组,这意味着,我将列出以下内容:

  results = [{'TCID':1, 'Date': '$DATESTAMP','Time': '$TIMESTAMP', 'Build':'$BUILD', 'TestCase': '$TESTCASE1', 'Result': '$RESULT1', 'Message':'$MESSAGE1', 'OutFile': '$OUTFILE1', 'ErrorDiff':'$ERRORDIFF1'}, {'TCID':2, 'Date': '$DATESTAMP','Time': '$TIMESTAMP', 'Build':'$BUILD','TestCase': '$TESTCASE2', 'Result': '$RESULT2', 'Message':'$MESSAGE2', 'OutFile': '$OUTFILE2', 'ErrorDiff':'$ERRORDIFF2'}, {'TCID':3, 'Date': '$DATESTAMP','Time': '$TIMESTAMP', 'Build':'$BUILD', 'TestCase': '$TESTCASE3', 'Result': '$RESULT3', 'Message':'$MESSAGE3', 'OutFile': '$OUTFILE3', 'ErrorDiff':'$ERRORDIFF3'}] …

我正在重新处理所有这些代码,以便1)使维护更容易,2)能够更轻松,更有效地将测试和结果添加到数组中。我想到的python代码是最简单的部分,因为我可以创建一个简单的for循环来处理来自bash代码的变量索引,然后动态创建python数组,但它没有成功好。

这是我的实验性python代码(首先在外面编写,用于测试理论),然后嵌入到bash脚本中:

 TFILE="`basename $0`.$$.py"
 cat <<END >$TFILE
 #!/usr/bin/env python
 import os
 results = []
 for i in range(1,2):
 a = "TCID"
 b = i
 c = 'Message'
 d = '$MESSAGE'
 e = str(i)
 f = 'Date'
 g = '$DATESTAMP'
 h = 'Time'
 j = '$TIMESTAMP'
 k = 'Build'
 l = '$BUILD'
 m = 'TestCase'
 n = '$TESTCASE'
 p = 'Result'
 q = '$RESULT'
 r = 'OutFile'
 s = '$OUTFILE'
 t = 'ErrorDiff'
 u = '$ERRORDIFF'

 results.append({a:b,c:d+e,f:g,h:j,k:l,m:n+e,p:q+e,r:s+e,t:u+e})

 with open('file.json', 'w') as f:
   json.dump(results, f, sort_keys=True, ensure_ascii=False)

 end

当运行bash的外部时,python创建了以下数组,这正是我想要的:

  [{"Build": "$BUILD", "Date": "$DATESTAMP", "ErrorDiff": "$ERRORDIFF1", "Message": "$MESSAGE1", "OutFile": "$OUTFILE1", "Result": "$RESULT1", "TCID": 1, "TestCase": "$TESTCASE1", "Time": "$TIMESTAMP"}]

(请记住,数组将继续使用46个不同的RESULT,MESSAGE等,每个都将被编入索引,就像上面的暴力示例一样)

但是,当在bash脚本中运行它时,我没有获得所有变量的变量扩展,只有那些没有添加索引的变量,所以$ BUILD被添加,但是“$ MESSAGE1”没有,只有索引得到补充:

 [{"Build": "831", "Date": "07-08-13", "ErrorDiff": "1", "Message": 1, "OutFile": "1", "Result": "1", "TCID": 1, "TestCase": "1", "Time": "16:45:27"}, {"Build": "831", "Date": "07-08-13", "ErrorDiff": "2", "Message": 2, "OutFile": "2", "Result": "2", "TCID": 2, "TestCase": "2", "Time": "16:45:27"}]]…

现在,如果我将以下(os.environ.get)添加到python:

 results = []
 for i in range(1, $tcindex):
   a = "TCID"
   b = i
   c = 'Message'

   ** d = os.environ.get(str('$MESSAGE'+str(i))) **

   e = str(i)

我得到以下内容:

  [{"Build": "831", "Date": "07-08-13", "ErrorDiff": "1", "Message": null, "OutFile": "1", "Result": "1", "TCID": 1, "TestCase": "1", "Time": "16:45:27"}, {"Build": "831", "Date": "07-08-13", "ErrorDiff": "2", "Message": null, "OutFile": "2", "Result": "2", "TCID": 2, "TestCase": "2", "Time": "16:45:27"}]]...

现在,而不是索引,我得到一个“空”值,这意味着我可能在正确的轨道上,但我仍然卡住了,我想在这里有人会很快看到我的方式错误让我走上正轨。请记住,我不是最精明的蟒蛇人,但我当然明白这一点,但是一点帮助总是一件好事。

问题:如何在bash脚本中为嵌入式python代码安排变量扩展,其中有问题的变量有索引,例如上面的代码。

1 个答案:

答案 0 :(得分:3)

调用os.environ.get时,不应包含$前缀,这是shell变量语法的一部分。你应该写os.environ.get('MESSAGE'+str(i)))