如高级 bash 脚本指南中所述,exec 可用于重定向 I/O。所以我只是在我的shell中写了一些案例。重定向 stdout 或 stderr 效果很好,但重定向 stdin 会使 shell 注销。有什么解释吗?
命令:
exec < file
当外壳在其标准输入上达到 EOF 时退出(这就是您键入Control-D注销的原因)。当它完成从 读取时file
,它将退出,因为没有更多的输入。
来自 bash 的 BASH_BUILTINS 手册页 ( man exec
)
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process
is created. The arguments become the arguments to command. If
the -l option is supplied, the shell places a dash at the begin-
ning of the zeroth argument passed to command. This is what
login(1) does. The -c option causes command to be executed with
an empty environment. If -a is supplied, the shell passes name
as the zeroth argument to the executed command. If command can-
not be executed for some reason, a non-interactive shell exits,
unless the shell option execfail is enabled, in which case it
returns failure. An interactive shell returns failure if the
file cannot be executed. If command is not specified, any redi-
rections take effect in the current shell, and the return status
is 0. If there is a redirection error, the return status is 1.
因此,如您所见,如果命令完成 -> 退出,并且如果命令失败 -> 退出...
将文件重定向到 exec 将失败...
除非它包含的行是有效代码,但也没有'退出直到你退出它。
(否则它将运行并退出......)