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

Re: read with redirected stdin



oh ok, that seems what I want (except I don't want to read from stdin when there is no tty, just assume a negative answer, but that's trivial).
I am still wondering,  what can be a way to simulate the situation of a script launched by cron, where there is no tty and no other mean for read to connect to a terminal?
thanks!

Pier Paolo Grassi


Il giorno sab 7 gen 2023 alle ore 18:31 Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx> ha scritto:
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

Roman.


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