Can anyone explain what are the uses of the exec command in shell scripting with simple examples?
相关问题
- How to get the return code of a shell script in lu
- Invoking Mirth Connect CLI with Powershell script
- Why should we check WIFEXITED after wait in order
- Emacs shell: save commit message
- “command not found” errors in expect script execut
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- What is the difference between execl and execv?
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
The
exec
built-in command mirrors functions in the kernel, there are a family of them based onexecve
, which is usually called from C.exec
replaces the current program in the current process, withoutfork
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;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: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 - theexec
replaced it.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 toksh
. While that worked, it left a straycsh
process running, and the logout was two stage which could get confusing. So we changed it toexec 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 theksh
is not a login-shell).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.You have obviously seen
exec
used somewhere, perhaps if you showed the code that's bugging you we could justify its use.Edit: I realised that my answer above is incomplete. There are two uses of
exec
in shells likeksh
andbash
- used for opening file descriptors. Here are some examples: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:There are several ways you can use these, on ksh use
read -u
orprint -u
, onbash
, for example:Just to augment the accepted answer with a brief newbie-friendly short answer, you probably don't need
exec
.If you're still here, the following discussion should hopefully reveal why. When you run, say,
you run a
sh
instance, then startcommand
as a child of thatsh
instance. Whencommand
finishes, thesh
instance also finishes.runs a
sh
instance, then replaces thatsh
instance with thecommand
binary, and runs that instead.Of course, both of these are useless in this limited context; you simply want
There are some fringe situations where you want the shell to read its configuration file or somehow otherwise set up the environment as a preparation for running
command
. This is pretty much the sole situation whereexec command
is useful.This does some stuff to prepare the environment so that it contains what is needed. Once that's done, the
sh
instance is no longer necessary, and so it's a (minor) optimization to simply replace thesh
instance with thecommand
process, rather than havesh
run it as a child process and wait for it, then exit as soon as it finishes.Similarly, if you want to free up as much resources as possible for a heavyish command at the end of a shell script, you might want to
exec
that command as an optimization.If something forces you to run
sh
but you really wanted to run something else,exec something else
is of course a workaround to replace the undesiredsh
instance (like for example if you really wanted to run your own spiffygosh
instead ofsh
but yours isn't listed in/etc/shells
so you can't specify it as your login shell).The second use of
exec
to manipulate file descriptors is a separate topic. The accepted answer covers that nicely; to keep this self-contained, I'll just defer to the manual for anything whereexec
is followed by a redirect instead of a command name.