shell脚本中exec命令的用途是什么? [英] What are the uses of the exec command in shell scripts?
问题描述
任何人都可以通过简单的示例来解释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 fork
ing 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;
-
我们希望用户在不访问Shell的情况下运行特定的应用程序.我们可以在/etc/passwd中更改登录程序,但是也许我们希望从启动文件中使用环境设置.因此,在(说)
.profile
中,最后一条语句表示如下内容:
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
崩溃,最终用户也无法进入外壳,因为它不存在-用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
开头,每个人都将对ksh
的调用放入其.login
(csh启动文件)中.在此过程中,它留下了一个迷路的csh
进程,并且注销是两个阶段,可能会引起混淆.因此,我们将其更改为exec ksh
,仅用korn shell替换了c-shell程序,并使一切变得更简单(与此相关的还有其他问题,例如ksh
不是登录shell).
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
等并且从不回退,则使每个调用都执行一次.这样可以节省资源(当然,除非重复,否则可以节省很多资源),并且使关机更加简单.
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.
编辑:我意识到上面的答案不完整.在像ksh
和bash
这样的shell中,有两种用法,用于打开文件描述符.以下是一些示例:
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 -u
或print -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屋!