Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: read with redirected stdin
On Sat, Jan 7, 2023 at 2:32 PM Pier Paolo Grassi <pierpaolog@xxxxxxxxx> wrote:
>
> Hello, I have a script that asks to create a target dir if it doesn't exist.
> to that extend I inserted an
> read -k1
> in the script.
> To be able to ask the user for confirm even when the script stdin is connected to a pipe I did
> read -k1 < /dev/tty
Note that this redirect doesn't affect `read -k1`. It'll read from the
terminal either way.
You can do something like this in your script:
if [[ -r $TTY ]]; then
read -k1
else
read -k1 -u0
fi
There is a corner case when you log in as root and then su to a
non-privileged user. $TTY won't be readable even though the process
has a TTY. Since you are redirecting stdin anyway, this corner case
shouldn't affect you.
Roman.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author