Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Testing if a file is a terminal?



On Dec 9, 11:47am, Rocky Bernstein wrote:
}
} isatty()  works on a file descriptor, but we don't have that yet. I
} suppose one could try to open that and then test but then care needs
} to be made to use that opened file descriptor and not say close and
} reopen that.

Hence you think [[ -t 0 ]] < "$1" is going to cause a problem because
it opens and closes the device.

If that's so, you do this:

  exec {ttyfd}<>"$1"
  [[ -t $ttyfd ]]

Now $ttyfd remains open for read and write, and future redirections
can use it, e.g.

    print -u $ttyfd "This is $1"
    cat < /etc/termcap >&$ttyfd

I'm not sure of the context, so somehow you have to get $ttyfd to be
visible both where you're testing the tty-ness of $1 and wherever it
is you wanted to use $1 for I/O.



Messages sorted by: Reverse Date, Date, Thread, Author