0

Alright. So I need a one-line linux command line command that needs to automatically boot up a python environment and execute a command. So the functionality I want is:

bash><some command here telling it to boot up python and print blah>
<enters python environment>
>>> print blah
blah

So basically, I am using xterm -e /bin/bash python And I want to do something like python;print blah However, it won't execute the second statement in the python environment in the new terminal. It waits until I quit() the other shell then it executes print blah. How can I make it do this?

Benjamin W.
  • 131
  • 1
  • 7
  • Do you want it to `print blah` _and then exit_, or `print blah` _and then let the user take control_? The first is much easier (and also very duplicative of other knowledgebase entries; in short, pass a herestring as input to the interpreter). – Charles Duffy Aug 04 '21 at 16:53
  • ...and the latter basically means you want the `interact` feature of expect, or to write a Python script that first prints whatever you want and then hands control to a REPL (which is honestly what I'd do). – Charles Duffy Aug 04 '21 at 16:54
  • @ Charles Duffy i want it to ```print blah``` then exit. – Benjamin W. Aug 04 '21 at 16:55
  • Then you want `python < – Charles Duffy Aug 04 '21 at 16:56
  • Easiest thing to do is probably to put that in a script, and tell your xterm command to run _that script_. – Charles Duffy Aug 04 '21 at 16:56
  • Anyhow -- there are really two completely separate questions here. One is how to run a command inside an xterm (which you already seem to know how to answer), and the other is how to pass your own chosen stdin to a program you're running. Make the command you run in the xterm be a script, and you can use any canned answer for how to pass stdin to a program started by a script. – Charles Duffy Aug 04 '21 at 16:57
  • Yeah the second part of that is my main question. – Benjamin W. Aug 04 '21 at 16:58
  • BTW, for Python, consider also `python -c 'your python code here'` instead of passing the code _as input_ at all. – Charles Duffy Aug 04 '21 at 16:59
  • So that can be `xterm -e python -c 'your python code here'` -- no reason to involve bash at all. – Charles Duffy Aug 04 '21 at 17:00
  • 1
    @BenjaminW. You might want to look at [this](https://unix.stackexchange.com/questions/385771/writing-to-stdin-of-a-process) question from elsewhere in the network. I think both the question and the accepted solution there are much closer to what you're trying to do. IMO the duplicate marked here could be better. – Jon Reeves Aug 04 '21 at 17:19

0 Answers0