The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: markus schnalke <meillo@marmaro.de>
To: tuhs@tuhs.org
Subject: [TUHS] sh:  cmd | >file
Date: Fri, 03 Jan 2020 13:45:53 +0100	[thread overview]
Message-ID: <1inMKv-0Km-00@marmaro.de> (raw)

Hoi,

in a computer forum I came across a very long command line,
including `xargs' and `sh -c'. Anyways, throughout the thread
it was modified several times, when accidently a pipe symbol
appeared between the command and the output redirection. The
command line did nothing; it ran successful. I was confused,
because I expected to see a syntax error in case of
``cmd|>file''. This made me wonder ...


With help of Sven Mascheck, I was able to clear my understanding.
The POSIX shell grammer provided the answer:

pipeline         :      pipe_sequence 
                 ... 
 
pipe_sequence    :                             command 
                 | pipe_sequence '|' linebreak command 
                 ; 
command          : simple_command 
                 ... 
 
simple_command   : cmd_prefix cmd_word cmd_suffix 
                 | cmd_prefix cmd_word 
                 | cmd_prefix   <--- HIER! 
                 | cmd_name cmd_suffix 
                 | cmd_name 
                 ; 
 
cmd_prefix       :            io_redirect 
                 ... 
 
io_redirect      :           io_file 
                 ... 
 
io_file          : '<'       filename 
                 | LESSAND   filename 
                 | '>'       filename 
                 ... 
 
A redirection is a (full) simple_command ... and because
``simple_command | simple_command'' is allowed, so is
``io_file | io_file''. This can lead to such strange (but
valid) command lines like:
 
	<a | >b 
	>b | <a 
 
Sven liked this one:

	:|>: 

Here some further fun variants:
 
	:|:>: 

	<:|:>: 

They would provide nice puzzles. ;-)


My understanding was helped most by detaching from the
semantics and focussing on syntax. This one is obviously
valid, no matter it has no effect:
 
	:|:|: 

From there it was easier to grasp:
 
	>a | >a | >a 
 
Which is valid, because ``>a'' is a (complete) simple_command.
 
Thus, no bug but consistent grammer. ;-) 


If one would have liked to forbid such a corner case,
additional special case handling would have been necessary
... which is in contrast to the Unix way.
 

Sven checked the syntax against various shells with these
results:

- Syntax ok in these shells:

SVR2 sh (Ultrix), SVR4 sh (Heirloom)
ksh93
bash-1.05, bash-aktuell
pdksh-5.2.14
ash-0.4.26, dash-0.5.6.1
posh-0.3.7, posh-0.12.3
mksh-R24, mksh-R52b
yash-2.29
zsh-3.0.8, zsh-4.3.17

- Exception to the rule:

7thEd sh:

    # pwd|>>file
    # echo $?
    141

On first sight ok, but with a silent error ... SIGPIPE (128+13).


I'd be interested in any stories and information around this
topic.

What about 7thEd sh?


meillo

             reply	other threads:[~2020-01-03 12:59 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-03 12:45 markus schnalke [this message]
2020-01-03 14:00 ` Steffen Nurpmeso
2020-01-03 17:03   ` Brian Zick
2020-01-03 17:18     ` markus schnalke
2020-01-04  0:53       ` Sven Mascheck via TUHS
2020-01-04 20:41         ` Steffen Nurpmeso
2020-01-03 19:38 ` markus schnalke
2020-01-03 19:44   ` Warner Losh
2020-01-03 22:49     ` Michael Parson
2020-01-03 23:32   ` Dave Horsfall
2020-01-04  2:58 Doug McIlroy
2020-01-04 10:07 ` markus schnalke
2020-01-04 11:47   ` Robert Clausecker
2020-01-04 22:31     ` Chet Ramey
2020-01-04 21:02   ` Dave Horsfall
2020-01-04 21:06     ` Jon Steinhart
2020-01-05  0:03       ` Eric Allman
2020-01-05  1:49         ` Adam Thornton
2020-01-05  2:44           ` Chet Ramey
2020-01-05  8:15             ` Brantley Coile
2020-01-05 15:16               ` Chet Ramey
2020-01-04 21:11     ` Dave Horsfall
2020-01-04 21:39       ` Warner Losh
2020-01-04 22:19         ` Terry Jones
2020-01-04 22:33           ` Chet Ramey
2020-01-04 23:53             ` Dave Horsfall
2020-01-05  0:04               ` Andreas Kusalananda Kähäri
2020-01-05  2:41               ` Chet Ramey
2020-01-05 13:45                 ` Sven Mascheck via TUHS
2020-01-05 15:18                   ` Chet Ramey
2020-01-05 21:21                 ` Dave Horsfall
2020-01-06 13:53                   ` Chet Ramey
2020-01-06 15:42                     ` Brantley Coile
2020-01-06 15:46                       ` arnold
2020-01-06 16:13                         ` Clem Cole
2020-01-06 20:44                           ` arnold
2020-01-06 20:51                             ` Steve Nickolas
2020-01-06 21:32                             ` Clem Cole
2020-01-06 21:39                               ` Brad Spencer
2020-01-06 21:29                         ` Dave Horsfall
2020-01-06 21:55                           ` Chet Ramey
2020-01-06 22:22                             ` Dave Horsfall
2020-01-06 22:52                             ` Dan Cross
2020-01-07  0:50                             ` Adam Thornton
2020-01-06 22:10                           ` Bakul Shah
2020-01-04 22:44           ` markus schnalke
2020-01-04 23:01             ` Terry Jones
2020-01-04 22:22         ` Dave Horsfall
2020-01-06  3:24 Brian Walden
2020-01-06 15:42 ` Richard Salz
2020-01-06 15:45   ` Brantley Coile
2020-01-06 16:11 Brian Walden
2020-01-06 16:33 ` Clem Cole
2020-01-06 19:47 Doug McIlroy
2020-01-07  4:49 Brian Walden
2020-01-07  5:03 Brian Walden

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=1inMKv-0Km-00@marmaro.de \
    --to=meillo@marmaro.de \
    --cc=tuhs@tuhs.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.
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).