From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 30521 invoked from network); 7 Jan 2023 17:36:32 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 7 Jan 2023 17:36:32 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1673112992; b=QqGsI1nD+C+26+EpFbYxF+KAr/GkrkMsR6Z7SBWKU6y+QFBKg5u6PoX+wn4qqsKZOPOQIK6hlH mhal7ZKPYXoOHTsNk3vrcp8OIfNKXo6gRYE81vIol1tbNaYSpL1R4ETVXgneukswO0nLcjet1z W6TVY480xSM4oljjgcV4jou93cQmYbgK4r3x0eNToYAjYO60pxDgjvzZeWkMB6vCfQQuMtkECw t/NP1a66CzXRrjl/gSPAH/AFZoJ8dIm8v737uAr1v4zPDX7txlJ6e/vB/9QHoZvck2N7oArG+b IZp7LfUNnvmwjsEa+NoGQ3M/v3q5XeiI5SrFNUSEM2i0cg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f51.google.com) smtp.remote-ip=209.85.218.51; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1673112992; bh=3YdeJEj56NWa9MiMJRrPqFyzxetrPXcVna1xrUkjW/A=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=X3A7aqfQxrjRffKA4crfngtKE5G3bwuOZ81yRDJrTI/z8fIqYeLZTPyLdMjv7CRDGLEbvI03r1 WPHyrqFW72yyxRvRibF1udJYrC4OyXKrODBn2WwBqb9s6c4nraPCkqyNQgFvkBd3c9s+hJF003 PCuiE22oPtkypH+f8x43I0eTMamy8THLSIqQbId76CE4i1/taew15s0ozGgyMcAtcqIq+rgokD vOqT0fklB8lCbZORZ1/nkzH11WW5dWD1xz3d4LEG/ER1ke2MfAMzv6EkmEEW2WzKM3zscMgDrL ZlM0nbwLbZyx4J6qxVP7ANJBu7lfiGt/MMHFUgfwvviu6w==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=juj+W45IF45FMljQanA0t8bG3IdWY6G4mH9bW/x9yUo=; b=Su2Dn5lZl0Xd+gLgaGZDNhhs0v WSBOsfVbTA3wzlT4S1FcanMt95xK1YeYfRH/Wfa4/n3p5i66nzeETvJc/94ZKSKUSjvklcbEM7UhI CMHcx+kj3Zm3KR/quNF8aejW8P5wNDicVHO59wBDAHAL23n55NMQci0yzct+mHzC+JJAnRZNhE/Vi 6X8UxtTNPexbi0c61tDinhEwqS+/k/mcCsTa9ogqlhnqbDLhUoKI6G16jpo6zK3V+2uPJdpXFNITw W5H4+AV50LoArQ/QT91vaf35dRiMM8a378H2UeLHF8jyNoUt2rfVP+GcMGDAyx7anzg9UtwHZ1rXR yXRXcP6A==; Received: by zero.zsh.org with local id 1pED7Q-000H49-Mt; Sat, 07 Jan 2023 17:36:32 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f51.google.com) smtp.remote-ip=209.85.218.51; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-ej1-f51.google.com ([209.85.218.51]:37777) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1pED4i-000GCd-A0; Sat, 07 Jan 2023 17:33:45 +0000 Received: by mail-ej1-f51.google.com with SMTP id ud5so10297614ejc.4 for ; Sat, 07 Jan 2023 09:33:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=juj+W45IF45FMljQanA0t8bG3IdWY6G4mH9bW/x9yUo=; b=pd1s22KOa8qJp82Oz1cqHTJhg+SOOjObRjhrFfuPs7P1VBXbb3KtrF95w/5h4KsLmh vL0PBLl906VFvSgpZgZOnjdGIHKccaYzdZzxXm1u9CsAG7WaYTkBOoH5eg507Z/GVni+ LcgyG4NS/hrsPS01admGYKsBg+MxFL7Lfd954ffjXzRgN2WGGdLyW5Dv77cXDCyiUQ70 n6ziNB2BjXwb/jo/EAhE7aX4r6unA8YoAEW7adP384fOg9nZ2E+we3UVM25INgcmyhvw 0KkitaKywTjGv65lpzEnfbSGrX0NMTHbqaWVXy/g5XHABe9DaIBcHa86XqbtnomM4jtV 9X0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=juj+W45IF45FMljQanA0t8bG3IdWY6G4mH9bW/x9yUo=; b=2sp7rEavV8rW3/omU6JEG+5oUdm6TPo12YPZtSRMgdu4rK7Dj/0gx2B41knsX7Yhs6 CEUzVXUTrPJHBzb5mgLLuTBCeTbxMpjr1DhZoYWlU5dkcMWXJ46C/RWyoUshd7/WjFAF m7hqC5nAD736D3grP8pFHk56f62ZKOJyGTONHHXvkRnGBuOEaMDNgPFiZhrtuPC6pyaA Q4iaLgQWs+jTsOxezOOT5TcX5d19lRYd+XRsfedb8Jgcpvb4xtAm0CARIM/2V4kUeOW/ dpBUm3+MOGwSmUu/ercmrGfHUYpAONlwiSilBFptw+qdz1rhPn3Ov16CatxNGih2M3JK rZ+g== X-Gm-Message-State: AFqh2kqPkycrK/j2IbecbtQBRe0foOAUsFwa+acrAhtR5X/7ZvHgYI/r xOwgi3MAy+q5YbkTT9ot093CpUjBz7OXZSA6VJvu2E++S00HiA== X-Google-Smtp-Source: AMrXdXtVZpwy1Tu9Epm2us5Xoepv5x/YonZ5z3tkqab7LY0zivvp0JroU2YhtAhZMprIwLko18rNZmvoxEMtpZ04QJI= X-Received: by 2002:a17:907:a482:b0:7c1:60fe:9d4a with SMTP id vp2-20020a170907a48200b007c160fe9d4amr3361216ejc.33.1673112823897; Sat, 07 Jan 2023 09:33:43 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Pier Paolo Grassi Date: Sat, 7 Jan 2023 18:33:07 +0100 Message-ID: Subject: Re: read with redirected stdin To: Roman Perepelitsa Cc: Zsh-Users List Content-Type: multipart/alternative; boundary="00000000000089a17605f1afef5d" X-Seq: 28621 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: --00000000000089a17605f1afef5d Content-Type: text/plain; charset="UTF-8" I think I got something wrong in my code to reproduce the cron problem: true | setsid zsh -c 'ps -o tty $$; read -k1 && echo 1' ps does not shows a tty, but read works correctly... Pier Paolo Grassi Il giorno sab 7 gen 2023 alle ore 18:21 Pier Paolo Grassi < pierpaolog@gmail.com> ha scritto: > 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 >> 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. >> > --00000000000089a17605f1afef5d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I think I got something=C2=A0wrong=C2=A0in my code to repr= oduce the cron problem:

true | setsid zsh -c 'ps -o = tty $$; read -k1 && echo 1'

ps does not = shows a tty, but read works correctly...

<= div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signature= ">
Pier = Paolo Grassi

<= br>
Il gior= no sab 7 gen 2023 alle ore 18:21 Pier Paolo Grassi <pierpaolog@gmail.com> ha scritto:
Thanks, = but i don't=C2=A0_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 in= fo exploring the /proc fs by myself.
for now I am asking directly= ps:
[[ "$(ps h -o tty $$)" !=3D "?" ]] &= & ...

Pier Paol= o Grassi


Il giorno sab 7 g= en 2023 alle ore 14:44 Roman Perepelitsa <roman.perepelitsa@gmail.com> ha s= critto:
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= 9;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 c= onnected 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:

=C2=A0 if [[ -r $TTY ]]; then
=C2=A0 =C2=A0 read -k1
=C2=A0 else
=C2=A0 =C2=A0 read -k1 -u0
=C2=A0 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.
--00000000000089a17605f1afef5d--