Currently, I have following code works:
a.sh
echo "start"
export abc="hello"
a=`python a.py`
echo $a
echo "end"
a.py
import os
print os.getenv('abc')*2
Above, my shell script need one python script' help to handle something then back the answer to shell script.
Although it works, we need to write another python file, the requirement is to afford single file to users, so how it makes, I remember I have once saw some kind of realize which combine shell and python code, could anyone also know that & give me some clue?
asked Feb 10, 2018 at 12:25
atline
32.1k19 gold badges102 silver badges134 bronze badges
2 Answers 2
You could use python's -c option in your a.sh file:
echo "start"
export abc="hello"
a=$(python -c "import os ; print os.getenv('abc') * 2")
echo $a
echo "end"
answered Feb 10, 2018 at 12:30
Ronan Boiteau
10.3k7 gold badges40 silver badges62 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
a.sh
echo "start"
export abc="hello"
a=`python <<- EOF
import os
print os.getenv('abc')*2
EOF`
echo $a
echo "end"
answered Feb 10, 2018 at 12:29
atline
32.1k19 gold badges102 silver badges134 bronze badges
1 Comment
Guy
you dont need
cat there, you can just use python <<- EOF ...default
$( cmd )is. Its easier for nesting etc.