質問

Similar to redirect COPY of stdout to log file from within bash script itself, but I'd also like to preserve stdout as a TTY device.

For example, I have the following scripts:

/tmp/teed-off$ cat some-script
#!/usr/bin/env ruby

if $stdout.tty?
  puts "stdout is a TTY"
else
  puts "stdout is NOT a TTY"
end
/tmp/teed-off$ cat wrapper 
#!/usr/bin/env bash

exec > >(tee some-script.log)

./some-script

When I run them, the wrapper eats stdout as a TTY device:

/tmp/teed-off$ ./some-script 
stdout is a TTY
/tmp/teed-off$ ./wrapper 
stdout is NOT a TTY

How can I flip that behavior around so that the script believes that its in a TTY even when executed via the wrapper?

役に立ちましたか?

解決

It won't be trivial, but I think you can do it via pseudo-ttys. I'm not sure that there's any standard tool, other than perhaps expect, that would do it for you.

It takes a bit of thinking about. You'd have a control program that would open the pseudo-tty master, then the slave. The slave would be connected to the output of ./some-script. The master would be read by the control program, which would copy the data it reads from the master to the file and to standard output.

I've not tried coding that up. I'm not sure whether you could do it with standard shell commands; I can't think of any way. So, I think there will be some C coding to be done.

他のヒント

look for dup2 it duplicates a file descriptor

int dup2(int oldfd, int newfd);

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top