dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Parke <parke.nexus@gmail.com>
To: dash@vger.kernel.org
Subject: Dash man page error regarding: background commands and the redirection of standard input to /dev/null
Date: Sat, 29 Jan 2022 16:10:52 -0800	[thread overview]
Message-ID: <CAPxz1+bq_JM4OmgPNh+Hgkx=eDrdiXrFFxi3BscKv3F1VJk2xQ@mail.gmail.com> (raw)

While writing a script, I wanted a background command to be able to
read from standard input.  (Specifically, from the terminal.)

While trying to achieve this result, I believe I found an error in the
dash man page.

The dash man page says:

   Background Commands -- &
     If the shell is not interactive, the standard input of an asynchronous
     command is set to /dev/null.

Source:
https://git.kernel.org/pub/scm/utils/dash/dash.git/tree/src/dash.1#n583
(Specifically, see line 592.)

I believe the above is incorrect.  I believe it is the 'monitor'
option flag that determines whether or not standard input is
redirected.

Below is a script that exposes the actual (and correct?) behavior.

I have tested the script with:
dash 0.5.11.4-1 on Arch
dash 0.5.10.2-6 on Ubuntu

----

PS1='$ '

probe  ()  {
  [ -t 0 ]  &&  s0='0'  ||  s0='1'
  [ -t 1 ]  &&  s1='0'  ||  s1='1'
  [ -t 2 ]  &&  s2='0'  ||  s2='1'
  echo  "probe.$1  $s0 $s1 $s2"  ;  }

echo  ;  echo  'plain'
probe  1
probe  2  &  wait
sleep  1

echo  ;  echo  'set  -o monitor'
set  -o monitor
probe  3
probe  4  &  wait
sleep  1
set  +o monitor

echo  ;  echo  'set  -o interactive'
set  -o interactive
probe  5
probe  6  &  wait
sleep  1
sleep  1

----

Actual output:

plain
probe.1  0 0 0
probe.2  1 0 0

set  -o monitor
probe.3  0 0 0
probe.4  0 0 0
[1] + Done                       probe 4

set  -o interactive
$ probe.5  0 0 0
$ probe.6  1 0 0
$ $ $

----

In the above, probe.4 and probe.6 do not behave as described by the
dash man page.

If the dash man page were correct, probes 4 and 6 would echo the
following output:
probe.4  1 0 0
probe.6  0 0 0

----

To compare with bash, the bash man page says:

       If a command is followed by a & and job control is not active, the  de‐
       fault standard input for the command is the empty file /dev/null.  Oth‐
       erwise, the invoked command inherits the file descriptors of the  call‐
       ing shell as modified by redirections.

----

When run with bash, the above test script behaves as described in the
bash man page.

----

plain
probe.1  0 0 0
probe.2  1 0 0

set  -o monitor
probe.3  0 0 0
probe.4  0 0 0

set  -o interactive
test.sh: line 23: set: interactive: invalid option name
probe.5  0 0 0
probe.6  1 0 0

----

Observations:

1)  It appears that the 'monitor' option flag controls the redirection
of standard input for background commands.  This is true in both dash
and bash.

2)  Whereas: The dash man page (incorrectly?) says that only the
'interactive' option flag controls redirection of standard input for
background commands.

3)  dash (surprisingly?) allows run-time toggling the interactive
option flag.  (bash does not.)

4)  (Surprisingly?) 'set -o monitor' causes dash to print out '[1] +
Done probe 4' even when the interactive action flag is not set.  (bash
does not print out this 'Done' message.)

5)  If the interactive option flag is toggled on at run-time, dash
echos PS1 after every command.  (Whereas bash does not allow run-time
toggling of the interactive option flag.)

6)  Assuming dash's current behavior is correct (i.e. redirecting
standard input based solely on the monitor option flag), then I
recommend updating the dash man page so it accurately describes the
current behavior.

                 reply	other threads:[~2022-01-30  0:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CAPxz1+bq_JM4OmgPNh+Hgkx=eDrdiXrFFxi3BscKv3F1VJk2xQ@mail.gmail.com' \
    --to=parke.nexus@gmail.com \
    --cc=dash@vger.kernel.org \
    --subject='Re: Dash man page error regarding: background commands and the redirection of standard input to /dev/null' \
    /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

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).