After years of bash scripting I've been given the task of modifying a python script to have it call a shell script when certain conditions are met. That part wasn't too bad, but now I'm trying to also send this shell script a variable from within the python script and I'm getting lost.
The shell script takes stdin, which is where I'm trying to place the value of the python variable, which is just an interger.
my code (so far) looks like this:
if VAR > NUMBER:
import os
bashCommand = "echo something | /path/to/script --args"
os.system(bashCommand)
and it works just fine. But what I am looking to do is make os.system(bashCommand) an equivalent of:
echo $VAR | /path/to/script --args
or even
echo 'some text' $VAR | /path/to/script --args
given the way os.system appears to work though it seems I'm approaching this completely wrong.
So I guess my question is how can I pass the value of VAR to bashCommand, preferably as stdin?
Best Answer
Python doesn't expand variables in strings in the same way as bash. If you have
VAR
in python and want to pass that to bash you could doif
VAR
in python holds the name of a bash variable you want to expand you could do similar: