Linux API 中的哪个函数使用 shebang 执行脚本文件?
what function in Linux API implements execution of a script file with a shebang?
来自https://unix.stackexchange.com/a/2910/674
... the way shebang (#!) is typically implemented:
- The kernel opens the executable, and finds that it starts with #!.
- The kernel closes the executable and opens the interpreter instead.
- The kernel inserts the path to the script to the argument list (as argv[1]), and executes the interpreter.
我想知道 Linux API 中的哪个函数实现了上述步骤以使用 shebang 执行脚本文件?
我考虑了以下可能性,但 none 似乎匹配:
execve()
将无法执行脚本。
execlp()
或 execvp()
似乎只是为了执行一个
没有任何 shebang 的脚本,默认为 /bin/sh
,根据
APUE:
If either execlp()
or execvp()
finds an executable file using one of
the path prefixes, but the file isn’t a machine executable that was
generated by the link editor, the function assumes that the file is a
shell script and tries to invoke /bin/sh
with the filename as input to
the shell.
可以 execlp()
或 execvp()
使用 shebang 执行脚本
对于任何语言的解释器(Python、Perl、Bash、...)。
谢谢。
应该由execve()
实施。 exec
家族中的所有其他函数都只是围绕它的包装(以 p
结尾的函数执行 $PATH
搜索以查找可执行参数,以 l
结尾的函数构建argv
数组通过遍历可变参数列表)。
它对任何语言的解释器都是一样的——该机制并不真正关心 shebang 行中的程序做什么,它只是使用脚本路径名作为参数来执行它。你甚至可以这样做:
#!/bin/cat
创建一个在您执行时只打印自身的文件。
来自https://unix.stackexchange.com/a/2910/674
... the way shebang (#!) is typically implemented:
- The kernel opens the executable, and finds that it starts with #!.
- The kernel closes the executable and opens the interpreter instead.
- The kernel inserts the path to the script to the argument list (as argv[1]), and executes the interpreter.
我想知道 Linux API 中的哪个函数实现了上述步骤以使用 shebang 执行脚本文件?
我考虑了以下可能性,但 none 似乎匹配:
execve()
将无法执行脚本。execlp()
或execvp()
似乎只是为了执行一个 没有任何 shebang 的脚本,默认为/bin/sh
,根据 APUE:If either
execlp()
orexecvp()
finds an executable file using one of the path prefixes, but the file isn’t a machine executable that was generated by the link editor, the function assumes that the file is a shell script and tries to invoke/bin/sh
with the filename as input to the shell.可以
execlp()
或execvp()
使用 shebang 执行脚本 对于任何语言的解释器(Python、Perl、Bash、...)。
谢谢。
应该由execve()
实施。 exec
家族中的所有其他函数都只是围绕它的包装(以 p
结尾的函数执行 $PATH
搜索以查找可执行参数,以 l
结尾的函数构建argv
数组通过遍历可变参数列表)。
它对任何语言的解释器都是一样的——该机制并不真正关心 shebang 行中的程序做什么,它只是使用脚本路径名作为参数来执行它。你甚至可以这样做:
#!/bin/cat
创建一个在您执行时只打印自身的文件。