Here is my problem. I have the following bash snippet:
#!/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
What I do with this is then export out the "INDEXED" variables, such as "MESSAGE1, RESULT1, or RESULT2…x into a json object and then present the data inside a web form. It's not the most elegant solution, but it works for me. I was "brute" forcing the creation of the python array, meaning, I would list out the following:
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'}] …
I was re-working all of this code to 1) make it easier to maintain, and 2) have the ability to add tests and results into the array a little easier and most efficiently. The python code I figured would be the easiest part, in that, I could just create a simple for loop to tack on an index to the variables from the bash code and then create the python array dynamically, but it hasn't worked out so good.
Here was my experimental python code (written outside at first, to test the theory), and then embedded inside the bash script:
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
When running this outside of bash, the python created the following array, which is exactly what I wanted:
[{"Build": "$BUILD", "Date": "$DATESTAMP", "ErrorDiff": "$ERRORDIFF1", "Message": "$MESSAGE1", "OutFile": "$OUTFILE1", "Result": "$RESULT1", "TCID": 1, "TestCase": "$TESTCASE1", "Time": "$TIMESTAMP"}]
(keep in mind, the array will continue on for 46 different RESULT, MESSAGE, etc, each will be indexed, like the brute force example above)
However, when running this inside the bash script, I don't get the variable expansion for all the variables, only those without an index added, so $BUILD gets added, but "$MESSAGE1" does not, only the index gets added:
[{"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"}]]…
Now, if I add the following (os.environ.get) to the 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)
I get the following:
[{"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"}]]...
now, instead of the index, I get a "null" value, which implies maybe I'm on the right track, but I'm still stuck and I figured someone on here would quickly see the error of my ways and put me on to the right track. Keep in mind, I'm not the most savvy python person, but I certainly understand it, but a little help is always a nice thing.
QUESTION: How does one arrange a variable expansion for embedded python code inside a bash script where the variable in question has an index, such as the code above.