0

I have a Python executable, written and compiled by somebody else, that I simply need to run once halfway down my own bash script. The program uses a text-based UI, therefore waits for input before proceeding, but the key operations it performs when starting are required in my bash script. A messy (and strange) procedure I know, but unfortunately I haven't got any other options.

I've gotten around forcefully closing the program with a kill signal, but the program's TUI insists on outputting to wherever it's run. I've tried redirecting both stdout and stderr to /dev/null and running the program in the background by suffixing an ampersand, but simply can't get it to play ball. I believe the cause is the program spawns other processes, and the output redirection of the parent process doesn't affect them. Is there any trick I can utilise to redirect all output from child processes too?

3
  • Python is typically not compiled. Are you sure you don't have the source code?
    – Zoredache
    Jun 8, 2010 at 16:19
  • I thought it was unusual, but the file is definitely binary - nor can I find source code anywhere on the machine. Two child processes that the program runs are pyo2.2 and python2.2, which was how I assumed I was dealing with Python.
    – tjbp
    Jun 9, 2010 at 9:15
  • What does file /path/to/progname tell you? Jun 9, 2010 at 10:20

1 Answer 1

3

The program may be writing to the tty instead of stdout. You may be able to use the unbuffer script that comes with expect or some other technique using expect or something similar.

Here's a demonstration. Create this script and call it "writetty". Then do chmod u+x writetty:

#!/bin/bash
date > /dev/tty

Now try it out:

$ ./writetty
Tue Jun  8 12:24:36 CDT 2010
$ ./writetty > /dev/null
Tue Jun  8 12:24:46 CDT 2010
$ var=$(./writetty)
Tue Jun  8 12:24:56 CDT 2010
$ unbuffer ./writetty > /tmp/writetty.out
$ cat /tmp/writetty.out
Tue Jun  8 12:25:06 CDT 2010
$ var=$(unbuffer ./writetty)
$ echo $var
Tue Jun  8 12:25:16 CDT 2010

You could use expect to respond to the program's request for input instead of killing it.

3
  • Thanks for the response! Isn't the line "./writetty > /dev/null" equivalent to "date > /dev/tty > /dev/null" in your example? That line successfully suppresses output - so can we assume the Python prog isn't outputting to /dev/tty? I'd love to be able to use expect, but I'm not authorised to install any additional packages :(
    – tjbp
    Jun 9, 2010 at 9:29
  • @STM: No, they're not equivalent. As you can see from the demo (try it yourself), redirecting the script to /dev/null still outputs to the terminal. Jun 9, 2010 at 10:24
  • @STM: You might also try: yourprog < /dev/null > /dev/null or yourprog < /dev/tty > /dev/null or yourprog < /dev/stdin > /dev/null to see if that will get its attention. Jun 9, 2010 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .