0

这是我的问题。我有以下 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

然后,我将“索引”变量(例如“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"}]]...

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

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

4

1 回答 1

3

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

于 2013-07-09T00:45:11.967 回答