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

Re: read with redirected stdin



Roman Perepelitsa wrote on Sat, Jan 07, 2023 at 18:31:30 +0100:
> On Sat, Jan 7, 2023 at 6:22 PM Pier Paolo Grassi <pierpaolog@xxxxxxxxx> wrote:
> >
> > Thanks, but i don't _always_ redirect stdin.
> 
> The code will still work (except for the corner case I mentioned).
> 
> Upon further thinking, the following should work in all cases:
> 
>     if [[ -n $TTY ]]; then
>       # There is a terminal. Read from it.
>       read -k1
>     else
>       # There is no terminal. Read from stdin.
>       read -k1 -u0
>     fi

Would it be useful to provide a ctermid(3) wrapper in zsh/system?  I'm
aware of tty(1), but that returns the name of the terminal _on stdin_,
which isn't necessarily the same thing as the controlling terminal.




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