The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* Re: [TUHS] sh: cmd | >file
@ 2020-01-04  2:58 Doug McIlroy
  2020-01-04 10:07 ` markus schnalke
  0 siblings, 1 reply; 56+ messages in thread
From: Doug McIlroy @ 2020-01-04  2:58 UTC (permalink / raw)
  To: tuhs

> I'm interested in the possible motivations for a redirection to be
> a simple command.

I use it to truncate a file to zero length.
Or to create (an empty) file.

Doug

^ permalink raw reply	[flat|nested] 56+ messages in thread
* Re: [TUHS] sh: cmd | >file
@ 2020-01-07  5:03 Brian Walden
  0 siblings, 0 replies; 56+ messages in thread
From: Brian Walden @ 2020-01-07  5:03 UTC (permalink / raw)
  To: tuhs

Clem Cole wrote:
>A heretic!!  Believers all know '*Bourne to Program, Type with Joy' *and*
>'One true bracing style' *are the two most important commandments of UNIX
>programmer!
>
>Seriously, I still write my scripts as v7 and use (t)csh as my login shell
>on all my UNIX boxes ;-)
>
>Clem

You know what's amazing? that Bill Joy code to launch either
csh or bourne shell based on the first character of teh file is
still in tcsh codebase today. It even has #! support just in case
your kernel does not. However this code never gets run as
who write scripts without #! anymore .. but here's a little test ---

$ tcsh
You have 2 mail messages.
> cat x1.sh
PATH=/bin
echo $SHELL
> ./x1.sh
/bin/sh
> cat x2.csh
#
setenv path /bin
echo $shell
> ./x2.csh
/usr/local/bin/tcsh
> exit

you can see it in https://github.com/tcsh-org/tcsh/blob/master/sh.exec.c

-Brian

^ permalink raw reply	[flat|nested] 56+ messages in thread
* Re: [TUHS] sh: cmd | >file
@ 2020-01-07  4:49 Brian Walden
  0 siblings, 0 replies; 56+ messages in thread
From: Brian Walden @ 2020-01-07  4:49 UTC (permalink / raw)
  To: tuhs

Doug McIlroy wrote:
>Brian Walden's discussion of sh #, etc, is right on.
>However, his etymology for unary * in C can be
>pushed back at least to 1959. * was used for
>indirect addressing in SAP, the assembler for
>the IBM 7090.

Thank you for both the confirmation and also that history update.
-Brian

^ permalink raw reply	[flat|nested] 56+ messages in thread
* Re: [TUHS] sh: cmd | >file
@ 2020-01-06 19:47 Doug McIlroy
  0 siblings, 0 replies; 56+ messages in thread
From: Doug McIlroy @ 2020-01-06 19:47 UTC (permalink / raw)
  To: tuhs

Brian Walden's discussion of sh #, etc, is right on.
However, his etymology for unary * in C can be
pushed back at least to 1959. * was used for
indirect addressing in SAP, the assembler for
the IBM 7090.

^ permalink raw reply	[flat|nested] 56+ messages in thread
* Re: [TUHS] sh: cmd | >file
@ 2020-01-06 16:11 Brian Walden
  2020-01-06 16:33 ` Clem Cole
  0 siblings, 1 reply; 56+ messages in thread
From: Brian Walden @ 2020-01-06 16:11 UTC (permalink / raw)
  To: tuhs

Richard Salz wrote:
>> not the kernel. This had traditionally been done after the exec() failed
>> then shell ould run "sh argv[0]", but with two shells this was now a
>> problem.
>>
>
>It seems the kernel did that; http://man.cat-v.org/unix_7th/2/exec since
>argv[-1] was altered.

As a user of these systems, the offical 7th Edition kernel most certainly
could not execute a script, only binaries. It happend after the release
1979 and took time to make its way out, which it did via DSB before 8th Ed
was finalized in 1985.

The usenet announcement of this new functionality from Dennis is on
Jan 10, 1980. Is listed here https://en.wikipedia.org/wiki/Shebang_(Unix)

Dennis stated the idea was not his, it came up during csonverastions at
a conference.
-Brian

^ permalink raw reply	[flat|nested] 56+ messages in thread
* Re: [TUHS] sh: cmd | >file
@ 2020-01-06  3:24 Brian Walden
  2020-01-06 15:42 ` Richard Salz
  0 siblings, 1 reply; 56+ messages in thread
From: Brian Walden @ 2020-01-06  3:24 UTC (permalink / raw)
  To: tuhs

More than you ever wanted to know about #
The first shell to use it as a comment was csh(1), Bill Joy did this.
This was also pre #! in the kernel so the shell had to exec scripts,
not the kernel. This had traditionally been done after the exec() failed
then shell ould run "sh argv[0]", but with two shells this was now a problem.
So csh would look at the first line of the script and if it was a #\n
it would exec csh on it if not it would exec sh(1) on it. This was check
was also placed into to BSD's (not v7 nor att's) bourne shell so it could
run csh scripts as well.

However this was not the first use of # as a comment character. That award
goes to Brian Kernighan's ratfor(1) (rational fortran) compiler in 1974-75.
Then Feldman used in make(1) in 1976, followed by Kernighan's m4(1), learn(1)
and most famously awk(1) in 1977

Bourne shell, written around 1976, eventualy picked this up later on but after
the initial v7 release.  And as some noted the : was kind of a comment, it
was a command that did an exit(0) orginally for labels for Thompson's
shell's goto command. The : command was eventually hard linked to the
true(1) command

Remember # was hard to type on teletypes as that was the erase character, so
to enter it, you needed to type \#
(# as erase and @ as line kill came from multics btw)
It was so hard to type that the orignal assember based on DEC PAL-11R,
that addressing syntax changed @ to * and # to $.
In DEC it would be--
MOV @X, R0;
In UNIX asm it became --
mov *x, r0
So this is also why C pointers use * notation.

-Brian

> From: Dave Horsfall dave at horsfall.org
>
>On Sat, 4 Jan 2020, Chet Ramey wrote:
>
>>> Which reminds me: which Shell introduced "#" as a true comment?
>>
>> Define "true comment." The v7 shell had `#' as the comment character, but
>> it only worked when in non-interactive shells. I think it was the Sys III
>> shell that made it work when the shell was interactive.
>
>Yes, that's what I meant.
>
>> This is, incidentally, why bash has the `interactive_comments' option,
>> which I saw in another message. BSD, which most of the GNU developers were
>> using at the (pre-POSIX) time, used the v7 shell and didn't have
>> interactive comments. When a sufficiently-advanced POSIX draft required
>> them, we added it.
>
>I never did catch up with all the options on the various shells; I just
>stick with the defaults in general.  Eg:
>
>     aneurin% man bash | wc -l
>       5947
>
>Life's too short...
>
>-- Dave

^ permalink raw reply	[flat|nested] 56+ messages in thread
* [TUHS] sh:  cmd | >file
@ 2020-01-03 12:45 markus schnalke
  2020-01-03 14:00 ` Steffen Nurpmeso
  2020-01-03 19:38 ` markus schnalke
  0 siblings, 2 replies; 56+ messages in thread
From: markus schnalke @ 2020-01-03 12:45 UTC (permalink / raw)
  To: tuhs

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

^ permalink raw reply	[flat|nested] 56+ messages in thread

end of thread, other threads:[~2020-01-07  5:05 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-04  2:58 [TUHS] sh: cmd | >file 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
  -- strict thread matches above, loose matches on Subject: below --
2020-01-07  5:03 Brian Walden
2020-01-07  4:49 Brian Walden
2020-01-06 19:47 Doug McIlroy
2020-01-06 16:11 Brian Walden
2020-01-06 16:33 ` Clem Cole
2020-01-06  3:24 Brian Walden
2020-01-06 15:42 ` Richard Salz
2020-01-06 15:45   ` Brantley Coile
2020-01-03 12:45 markus schnalke
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

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