sh: Do not abort on a redirection error if there is no command word.

Although simple commands without a command word (only assignments and/or
redirections) are much like special builtins, POSIX and most shells seem to
agree that redirection errors should not abort the shell in this case. Of
course, the assignments persist and assignment errors are fatal.

To get the old behaviour portably, use the ':' special builtin.
To get the new behaviour portably, given that there are no assignments, use
the 'true' regular builtin.
This commit is contained in:
Jilles Tjoelker 2010-03-13 22:53:17 +00:00
parent ac7fbc6abd
commit 3a64dbc20a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=205138
2 changed files with 14 additions and 1 deletions

View file

@ -680,7 +680,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
/* Variable assignment(s) without command */
cmdentry.cmdtype = CMDBUILTIN;
cmdentry.u.index = BLTINCMD;
cmdentry.special = 1;
cmdentry.special = 0;
} else {
static const char PATH[] = "PATH=";
int cmd_flags = 0, bltinonly = 0;
@ -891,6 +891,12 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
}
handler = &jmploc;
redirect(cmd->ncmd.redirect, mode);
/*
* If there is no command word, redirection errors should
* not be fatal but assignment errors should.
*/
if (argc == 0 && !(flags & EV_BACKCMD))
cmdentry.special = 1;
if (cmdentry.special)
listsetvar(cmdenviron);
commandname = argv[0];

View file

@ -0,0 +1,7 @@
# $FreeBSD$
# A redirection error should not abort the shell if there is no command word.
exec 2>/dev/null
</var/empty/x
</var/empty/x y=2
y=2 </var/empty/x
exit 0