The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] Shell builtin exec
@ 2019-08-20 21:10 Adam Thornton
  2019-08-20 22:00 ` Warren Toomey
  2019-08-21  0:38 ` Chet Ramey
  0 siblings, 2 replies; 3+ messages in thread
From: Adam Thornton @ 2019-08-20 21:10 UTC (permalink / raw)
  To: The Eunuchs Hysterical Society

[-- Attachment #1: Type: text/plain, Size: 306 bytes --]

This is probably the place to ask:

I understand why the shell builtin "exec" is the same as the syscall exec()
in the sense of "replace this process with that one."  But why is it also
the way to redirect filehandles in the current shell?  (That is, why isn't
the redirection named something else?)

Adam

[-- Attachment #2: Type: text/html, Size: 421 bytes --]

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

* Re: [TUHS] Shell builtin exec
  2019-08-20 21:10 [TUHS] Shell builtin exec Adam Thornton
@ 2019-08-20 22:00 ` Warren Toomey
  2019-08-21  0:38 ` Chet Ramey
  1 sibling, 0 replies; 3+ messages in thread
From: Warren Toomey @ 2019-08-20 22:00 UTC (permalink / raw)
  To: Adam Thornton; +Cc: The Eunuchs Hysterical Society

On Tue, Aug 20, 2019 at 02:10:55PM -0700, Adam Thornton wrote:
>    This is probably the place to ask:
>    I understand why the shell builtin "exec" is the same as the syscall
>    exec() in the sense of "replace this process with that one."  But why
>    is it also the way to redirect filehandles in the current shell?  (That
>    is, why isn't the redirection named something else?)

exec() doesn't do anything with file handles; it simply replaces the
executable code in the current process. Here's the pseudo-code for a
shell where redirection is done.

  int pid;                      /* Holds the process-id of the child */

  switch (pid = fork())         /* Call fork, and test what it returns */
  {
    case -1: printf("The fork failed\n"); return (-1);

    case 0:              /* The child is given a value of 0 from fork() */
                           /* Do special things like closing files etc. */

    if (redirection required) {
      close my standard output;
      open the named output file as my standard output;
    }

                                /* Replace ourselves with the real child */
                                /* Call execvp */
      execlp(command, arg1, arg2, ..., NULL);
      exit(1);                  /* Exec failed, indicate error */

    default:          /* The parent receives the new process-id from fork() */
      wait();                   /* Wait for child to exit */
}

So the child shell itself does the redirection after the fork(), but before
it replaces itself with the new executable code.

Cheers, Warren



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

* Re: [TUHS] Shell builtin exec
  2019-08-20 21:10 [TUHS] Shell builtin exec Adam Thornton
  2019-08-20 22:00 ` Warren Toomey
@ 2019-08-21  0:38 ` Chet Ramey
  1 sibling, 0 replies; 3+ messages in thread
From: Chet Ramey @ 2019-08-21  0:38 UTC (permalink / raw)
  To: Adam Thornton, The Eunuchs Hysterical Society

On 8/20/19 5:10 PM, Adam Thornton wrote:
> This is probably the place to ask:
> 
> I understand why the shell builtin "exec" is the same as the syscall exec()
> in the sense of "replace this process with that one."  But why is it also
> the way to redirect filehandles in the current shell?  (That is, why isn't
> the redirection named something else?)

It was probably a side effect that turned out to be useful. The `exec'
builtin performs redirections without a way to undo them, because why
bother, and executes its arguments as a command with arguments, as if it
were in a child process.

The original Bourne shell just performed the redirections and returned
if there weren't any arguments, leaving the non-undoable redirections
in place.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

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

end of thread, other threads:[~2019-08-21  0:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-20 21:10 [TUHS] Shell builtin exec Adam Thornton
2019-08-20 22:00 ` Warren Toomey
2019-08-21  0:38 ` Chet Ramey

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