shell脚本中exec命令的作用是什么? [英] What are the uses of the exec command in shell scripts?

查看:43
本文介绍了shell脚本中exec命令的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能用简单的例子解释一下 exec 命令在 shell 脚本中的用途是什么?

Can anyone explain what are the uses of the exec command in shell scripting with simple examples?

推荐答案

exec 内置命令镜像内核中的函数,有一系列基于execve,通常从 C 调用.

The exec built-in command mirrors functions in the kernel, there are a family of them based on execve, which is usually called from C.

exec 替换当前进程中的当前程序,无需fork新进程.您不会在编写的每个脚本中使用它,但有时它会派上用场.下面是我用过的一些场景;

exec replaces the current program in the current process, without forking a new process. It is not something you would use in every script you write, but it comes in handy on occasion. Here are some scenarios I have used it;

  1. 我们希望用户在不访问 shell 的情况下运行特定的应用程序.我们可以更改/etc/passwd 中的登录程序,但也许我们希望从启动文件中使用环境设置.所以,在(比如说).profile 中,最后一个语句是这样的:

  1. We want the user to run a specific application program without access to the shell. We could change the sign-in program in /etc/passwd, but maybe we want environment setting to be used from start-up files. So, in (say) .profile, the last statement says something like:

 exec appln-program

所以现在没有可以返回的外壳.即使 appln-program 崩溃,最终用户也无法访问 shell,因为它不存在 - exec 替换了它.

so now there is no shell to go back to. Even if appln-program crashes, the end-user cannot get to a shell, because it is not there - the exec replaced it.

我们想使用与/etc/passwd 不同的 shell.尽管看起来很愚蠢,但某些站点不允许用户更改其登录外壳.我知道的一个网站让每个人都从 csh 开始,每个人都只是在他们的 .login(csh 启动文件)中调用了 ksh.虽然这有效,但它留下了一个运行的 csh 进程,并且注销是两个阶段,这可能会让人感到困惑.因此,我们将其更改为 exec ksh,它只是用 korn shell 替换了 c-shell 程序,并使一切变得更简单(这还有其他问题,例如 ksh 不是登录外壳).

We want to use a different shell to the one in /etc/passwd. Stupid as it may seem, some sites do not allow users to alter their sign-in shell. One site I know had everyone start with csh, and everyone just put into their .login (csh start-up file) a call to ksh. While that worked, it left a stray csh process running, and the logout was two stage which could get confusing. So we changed it to exec ksh which just replaced the c-shell program with the korn shell, and made everything simpler (there are other issues with this, such as the fact that the ksh is not a login-shell).

只是为了节省进程.如果我们调用 prog1 ->prog2 ->prog3 ->prog4 等,永不返回,然后使每次调用都成为 exec.它节省了资源(不可否认,除非重复)并使关闭更简单.

Just to save processes. If we call prog1 -> prog2 -> prog3 -> prog4 etc. and never go back, then make each call an exec. It saves resources (not much, admittedly, unless repeated) and makes shutdown simplier.

您显然已经看到在某处使用了 exec,也许如果您展示了困扰您的代码,我们就可以证明它的使用是合理的.

You have obviously seen exec used somewhere, perhaps if you showed the code that's bugging you we could justify its use.

编辑:我意识到我上面的回答不完整.在kshbash 等shell 中,exec两种 用途 - 用于打开文件描述符.以下是一些示例:

Edit: I realised that my answer above is incomplete. There are two uses of exec in shells like ksh and bash - used for opening file descriptors. Here are some examples:

exec 3< thisfile          # open "thisfile" for reading on file descriptor 3
exec 4> thatfile          # open "thatfile" for writing on file descriptor 4
exec 8<> tother           # open "tother" for reading and writing on fd 8
exec 6>> other            # open "other" for appending on file descriptor 6
exec 5<&0                 # copy read file descriptor 0 onto file descriptor 5
exec 7>&4                 # copy write file descriptor 4 onto 7
exec 3<&-                 # close the read file descriptor 3
exec 6>&-                 # close the write file descriptor 6

请注意,这里的间距非常重要.如果在 fd 编号和重定向符号之间放置一个空格,则 exec 将恢复为原始含义:

Note that spacing is very important here. If you place a space between the fd number and the redirection symbol then exec reverts to the original meaning:

  exec 3 < thisfile       # oops, overwrite the current program with command "3"

有几种方法可以使用这些,在 ksh 上使用 read -uprint -u,在 bash 上,例如:

There are several ways you can use these, on ksh use read -u or print -u, on bash, for example:

read <&3
echo stuff >&4

这篇关于shell脚本中exec命令的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆