1
votes

Is it possible to interact with an IPython interactive session (or with a kernel) from a Bash script? Ideally, I'd like to do something like this, within a shell script (I'm aware that the send subcommand probably doesn't exist like this):

# do stuff in Bash ...
# start a kernel and get its Id
KERNEL=`ipython init --command="print(__KERNELID__)"`
# do something inside the kernel
ipython send --kernel=KERNELID --command="mylist = [0,1,2]"

Then, ideally, the command

ipython send --kernel=KERNELID --command="print(mylist)"

would output

[0, 1, 2]

In the end, I would need to destroy the kernel somehow:

ipython --kernel=KERNELID --command="sys.exit()"

Probably, there is already a mechanism to do what I'd like, right? Unfortunately, I wasn't able to find it ...

1

1 Answers

0
votes

There are quite a number of ways around this problem. Since you are having to use python you might as well use python for the whole thing. Python programs can take command line arguments like mylist and do whatever you want with them.

Since you are sending commands to be evaluated make sure you are the one controlling the inputs. Don't let someone start typing "import os" and "os.unlink([your hard drive here])" for example.

For other options: Check out expect for your interactive needs http://expect.sourceforge.net/ or just the python version check out the pexpect module http://pexpect.sourceforge.net/pexpect.html