0

I want my program to launch some other program and somehow receive all of it's printing output.

The bash/terminall/shell (not sure about my terminology here) does exactly that - you tell it what program to open and it acts as standard output for that program. How can that be implemented? (Asking for a Python version here, but of course C/C++ way is good to know!)

Thanks!

user3496846
  • 1,627
  • 3
  • 16
  • 28
  • No, bash *does not* retrieve stdout from programs it invokes. A program bash runs writes its stdout direct to the same terminal that bash writes its own stdout to. – Charles Duffy Dec 15 '17 at 02:54
  • @CharlesDuffy hm, what is its mechanism then? – user3496846 Dec 15 '17 at 02:54
  • What do you mean? There is no mechanism. File handles are inherited from your parent process, so if bash's stdout is to a TTY (a SSH session, anything else), then a program that bash starts will write its stdout to that same TTY automatically, without bash having to do anything at all. – Charles Duffy Dec 15 '17 at 02:55
  • We often get requests here to, say, retroactively redirect the stdout from the last command that was run to a file, or something, but bash has no way to do that -- it never even saw that output at all, because it went straight from the program bash started to the terminal through the inherited FD. – Charles Duffy Dec 15 '17 at 02:56
  • Anyhow, it's absolutely possible to start a program and read its stdout in Python -- it's just that doing so makes your program *unlike* what bash does when it starts a child process, unless your bash code is performing a process substitution, command substitution, or other redirection operation that requires that the program write to a FIFO. Consequently, "like bash does" is not a useful description of the exact behavior you want, making this question quite vague. – Charles Duffy Dec 15 '17 at 02:58
  • @CharlesDuffy thanks for the explanation, I did know how the bash functions and was not sure about terminology. I used that to explain what I am trying to accomplish. – user3496846 Dec 15 '17 at 03:01
  • Or do you just want it to print straight to the terminal? If so, that's what `subprocess.Popen()` will do by default if you don't give it *any* `stdout` or `stderr` argument at all... and I'd be glad to find a duplicate for that as well, if you clarify that that truly is what you're asking. – Charles Duffy Dec 15 '17 at 03:03
  • That explanation doesn't actually clarify anything about what you're trying to accomplish, though. Could you [edit] it to be meaningful to people who don't share the same misunderstanding about how bash operates? – Charles Duffy Dec 15 '17 at 03:07

1 Answers1

0

In Python, subprocess — Subprocess management is exact what you want.

A quick example subprocess.check_output(["echo", "Hello World!"]) will return 'Hello World!\n'.

Jks Liu
  • 457
  • 2
  • 12
  • 1
    There are many, *many* answers already in the knowledge base directing people to use the subprocess module for this purpose -- meaning that we could (and should -- see [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the section *Answer Well-Asked Questions*, particularly the bullet regarding questions which "have already been asked and answered many times before") close the answer as a duplicate if this is what the OP is asking. – Charles Duffy Dec 15 '17 at 03:00
  • Nice, I wonder if that could work asynchronously? That is, is that possible to capture the output in real-time while the subprocess is running? Should I edit my question for that? – user3496846 Dec 15 '17 at 03:06
  • If there weren't already duplicates telling you how to do that, yes -- but that's been asked and answered many times. – Charles Duffy Dec 15 '17 at 03:07
  • @CharlesDuffy awesome, thanks for your help! – user3496846 Dec 15 '17 at 03:09