1

I am writing a python program which is runs some BASH scripts in sub-processes. One might say that I am using BASH as a scripting language for my python program.

Is there a way to inject python functions into my bash script without having to reload the python code from bash?

strawman example: If this were Python running Javascript, then I could bind my Python functions into the js2py VM and call them from within javascript.

I thought of calling: python some_file_of_mine.py from bash , but this would be launching a new python process, without access to my python program's data.

Also thought of calling: python -c $SOME_INJECTED_PYTHON_CODE. This could use Python's inspect.source() to pre-inject some simple python code along with some bound data from the parent process into the child bash shell. However this will be very quote(`/") sensetive, and will cause some problems with $import.

What I would really like, is a simple way of calling the parent process the bash subprocess, and getting some data back (short of using a Flask + CURL combination)

asked Jul 12, 2021 at 6:41
4
  • I don't understand your problem. Maybe you should run all in one Python script and skip all bash code. Or maybe you should write data in file and read it in other script. Or send as parameter python script.py arg1 arg2 ... Commented Jul 12, 2021 at 6:56
  • You could use a fifo file for communication, or have the Python process create a pair of fds to read/write. But why not use Python to script Python..? Commented Jul 12, 2021 at 7:01
  • @AKX the file descriptors sound like a good idea. I will try it Commented Jul 12, 2021 at 21:56
  • This seems like an example of the XY Problem. Commented Jul 13, 2021 at 1:48

1 Answer 1

2

You can send the result of your functions to the standard output by asking the Python interpreter to print the result:

python -c 'import test; print test.get_foo()'

The -c option simply asks Python to execute some Python commands.

In order to store the result in a variable, you can therefore do:

RESULT_FOO=`python -c 'import test; print test.get_foo()'`

or, equivalently

RESULT=$(python -c 'import test; print test.get_foo()')

since backticks and $(...) evaluate a command and replace it by its output.

PS: Getting the result of each function requires parsing the configuration file each time, with this approach. This can be optimized by returning all the results in one go, with something like:

ALL_RESULTS=$(python -c 'import test; print test.get_foo(), test.get_bar()')

The results can then be split and put in different variables with

RESULT_BAR=$(echo $ALL_RESULTS | cut -d' ' -f2)

which takes the second result and puts it in RESULT_BAR for example (and similarly: -fn for result #n).

PS2: it would probably be easier to do everything in a single interpreter (Python, but maybe also the shell), if possible, instead of calculating variables in one program and using them in another one.

answered Jul 12, 2021 at 6:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.