From: Pier Paolo Grassi <pierpaolog@gmail.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: read with redirected stdin
Date: Sat, 7 Jan 2023 18:21:36 +0100 [thread overview]
Message-ID: <CAP+y1xBGBDhmf3uQmxoTrkakTc_1=n54OtH3MC6eTdtsACJFiA@mail.gmail.com> (raw)
In-Reply-To: <CAN=4vMo5EJTiExmGxt2cxD2=_5PC0b=NCSEdZQRda+kvshg2=Q@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1266 bytes --]
Thanks, but i don't _always_ redirect stdin.
i wonder how is ps getting the tty (it displays an ? when there is no tty
for the process)
I suppose it uses /proc, but couldn't find a reference or the info
exploring the /proc fs by myself.
for now I am asking directly ps:
[[ "$(ps h -o tty $$)" != "?" ]] && ...
Pier Paolo Grassi
Il giorno sab 7 gen 2023 alle ore 14:44 Roman Perepelitsa <
roman.perepelitsa@gmail.com> ha scritto:
> On Sat, Jan 7, 2023 at 2:32 PM Pier Paolo Grassi <pierpaolog@gmail.com>
> 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.
>
[-- Attachment #2: Type: text/html, Size: 2040 bytes --]
next prev parent reply other threads:[~2023-01-07 17:23 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-07 13:31 Pier Paolo Grassi
2023-01-07 13:44 ` Roman Perepelitsa
2023-01-07 17:21 ` Pier Paolo Grassi [this message]
2023-01-07 17:31 ` Roman Perepelitsa
2023-01-07 17:36 ` Pier Paolo Grassi
2023-01-07 17:52 ` Roman Perepelitsa
2023-01-08 13:23 ` Daniel Shahaf
2023-01-08 13:48 ` Roman Perepelitsa
2023-01-08 14:21 ` Daniel Shahaf
2023-01-08 14:42 ` Roman Perepelitsa
2023-01-07 17:33 ` Pier Paolo Grassi
2023-01-08 4:28 ` Jim
2023-01-08 4:44 ` Pier Paolo Grassi
2023-01-08 21:18 ` Jim
2023-01-09 16:01 ` Pier Paolo Grassi
2023-01-09 22:47 ` Jim
2023-01-10 2:46 ` Bart Schaefer
2023-01-10 6:03 ` Pier Paolo Grassi
2023-01-08 14:44 ` Ray Andrews
2023-01-08 15:06 ` Roman Perepelitsa
2023-01-08 20:23 ` Ray Andrews
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAP+y1xBGBDhmf3uQmxoTrkakTc_1=n54OtH3MC6eTdtsACJFiA@mail.gmail.com' \
--to=pierpaolog@gmail.com \
--cc=roman.perepelitsa@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).