atline
atline

Reputation: 31674

Combine shell and python code

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?

Upvotes: 1

Views: 1883

Answers (2)

atline
atline

Reputation: 31674

a.sh

echo "start"

export abc="hello"
a=`python <<- EOF
import os
print os.getenv('abc')*2
EOF`
echo $a

echo "end"

Upvotes: 2

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

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"

Upvotes: 5

Related Questions