Arya Arya - 6 months ago 19
Python Question

Passing multiple variables from python script to shell script

I am trying to run a shell script from python by exporting the python variable to the shell script instead of directly reading them from the user. A question regarding passing array values as variable to shell script was answered successfully earlier and helped me to pass the values in an array as a input variable to the shell script. I want to export multiple variables such as

FLUID
,
TTYPE
and
FLIBRARY
from the following python script:

FLUID="MDM"
TTYPE=0
FLIBRARY="RefProp"
HSPACE=[0.01, 0.009, 0.008, 0.007]
subprocess.call(['./testfile1'] + [str(n) for n in HSPACE])


to the following shell script named testfile1:

#!/bin/bash
echo "$FLUID, $FLIBRARY" | ./vls.exe
for i; do
awk 'NR==8 {$1=" " a }1' a=$i spacingcontrol.vls > tmp.vls && mv tmp.vls spacingcontrol.vls
awk 'NR==8 {$2=" " b " "}1' b=$i spacingcontrol.vls > tmp.vls && mv tmp.vls spacingcontrol.vls
done

Answer

You could set them as environment variables within the Python script:

import os 
import subprocess

os.environ['FLUID'] ="MDM"
os.environ['TTYPE'] = str(0)
os.environ['FLIBRARY'] = "RefProp"
HSPACE=[0.01, 0.009, 0.008, 0.007]
subprocess.call(['./testfile1'] + [str(n) for n in HSPACE])
Comments