1

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?

6
  • Why not write everything in python? Commented Feb 10, 2018 at 12:29
  • backticks aren't recommended, by the way, $( cmd ) is. Its easier for nesting etc. Commented Feb 10, 2018 at 12:38
  • @backticks, why backticks not recommended? Commented Feb 10, 2018 at 12:53
  • 1
    It's the same character to start and end makes them more difficult to use correctly. The way I suggested does the same thing, but with less escaping issues. stackoverflow.com/q/9405478 Commented Feb 10, 2018 at 13:03
  • 1
    @Guy, i knew both of them, but did not realize the difference between them, really a windfall, thanks. Commented Feb 10, 2018 at 13:08

2 Answers 2

5

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"
Sign up to request clarification or add additional context in comments.

Comments

2

a.sh

echo "start"

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

echo "end"

1 Comment

you dont need cat there, you can just use python <<- EOF ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.