在stdin的exec重定向之后Shell注销

时间:2013-05-19 03:16:37

标签: bash shell exec

如高级bash脚本指南中所述,exec可用于重定向I / O. 所以我只是在shell中写一些案例。重定向stdout或stderr效果很好,但重定向stdin会使shell注销。有什么解释吗?

命令:

exec < file

2 个答案:

答案 0 :(得分:3)

shell在标准输入上达到EOF时退出(这就是你键入 Control-D 注销的原因)。当它从file读完后,它将退出,因为没有更多的输入。

答案 1 :(得分:0)

来自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.

因此,正如您所看到的,如果命令完成 - >退出,如果命令失败 - &gt;出口,
将文件重定向到exec将失败...
除非它包含一行有效代码,在您退出之前不会退出 (否则它会运行,然后退出......)