为什么要执行“sh -c a.out"?而不是 a.out 本身? [英] Why should one exec "sh -c a.out" instead of a.out itself?
问题描述
我正在 https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/popen.c.auto.html 并注意到他们做 execl(_PATH_BSHELL, "sh", "-c", command, NULL)
而不是 execl(_PATH_BSHELL, command, NULL)
.
I'm studying Apple's implementation of popen()
at https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/popen.c.auto.html and noticed that they do execl(_PATH_BSHELL, "sh", "-c", command, NULL)
instead of execl(_PATH_BSHELL, command, NULL)
.
你为什么要(或应该)执行一个可执行文件,例如a.out
通过 sh -c
而不是可执行文件本身?
Why would you want to (or should you) exec an executable, e.g. a.out
via sh -c
instead of just the executable itself?
如果你 exec sh -c a.out
而不是 a.out
本身,实际的 a.out
过程是否会结束成为孙子"进程而不是子进程?
If you exec sh -c a.out
instead of just a.out
itself, does the actual a.out
process end up being a "grandchild" process and not a child process?
推荐答案
你为什么要(或应该)执行一个可执行文件,例如
a.out
通过sh -c
而不是可执行文件本身?
Why would you want to (or should you) exec an executable, e.g.
a.out
viash -c
instead of just the executable itself?
popen()
旨在运行包含 shell 语法的 shell 命令,例如 >
重定向、|
管道和 &&
命令链.它需要通过 sh -c
传递字符串以支持这些结构.如果没有,这些语法特征将作为参数逐字传递给相关程序.
popen()
is designed to run shell commands that include shell syntax like >
redirection, |
pipes, and &&
command chaining. It needs to pass the string through sh -c
in order to support those constructs. If it didn't those syntactical features would be passed verbatim to the program in question as arguments.
例如,popen("make clean && make")
应该触发两个 make
调用.如果没有 sh -c
,它会用三个参数调用 make
一次,就好像已经输入了
For example, popen("make clean && make")
should trigger two make
invocations. Without sh -c
it would call make
once with three arguments, as if one had typed
$ make clean '&&' make
在终端.
如果你 exec sh -c a.out
而不是 a.out
本身,实际的 a.out
过程是否会结束成为孙子"进程而不是子进程?
If you exec
sh -c a.out
instead of justa.out
itself, does the actuala.out
process end up being a "grandchild" process and not a child process?
是的,没错.当前进程和a.out
之间会有一个sh
进程.
Yes, that is correct. There will be a sh
process in between the current process and a.out
.
这篇关于为什么要执行“sh -c a.out"?而不是 a.out 本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!