从 bash 执行 ksh 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18678886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:29:40  来源:igfitidea点击:

Executing ksh script from bash

bashshell

提问by itsh

During one of my interviews I came across a very basic question about shell scripting and I could not find the answer for it. Question was: If first line of the script is "#!/bin/ksh" can I execute it on bash?

在我的一次采访中,我遇到了一个关于 shell 脚本的非常基本的问题,但我找不到答案。问题是:如果脚本的第一行是“#!/bin/ksh”,我可以在 bash 上执行它吗?

My answer was No. And the reason to include a directive as the first line of script is to tell which interpreter needs to be used. But the interviewer's answer to this question was Yes. Can anybody explain me the reason?

我的回答是否定的。在脚本的第一行包含一个指令的原因是为了告诉需要使用哪个解释器。但是面试官对这个问题的回答是肯定的。任何人都可以向我解释原因吗?

回答by Aleks-Daniel Jakimenko-A.

Yeah, you can:

是的,你可以:

bash yourscript

Of course it will throw some errors if there are language specific features. But the question did not specify if it should work correctly or not.

当然,如果有语言特定的功能,它会抛出一些错误。但问题没有具体说明它是否应该正常工作。

From man bash:

来自man bash

the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash's exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script

假定第一个参数是包含 shell 命令的文件的名称。如果以这种方式调用 bash,则将 $0 设置为文件名,并将位置参数设置为其余参数。Bash 从这个文件中读取并执行命令,然后退出。Bash 的退出状态是脚本中执行的最后一个命令的退出状态。如果没有执行任何命令,退出状态为 0。首先尝试打开当前目录中的文件,如果没有找到文件,则 shell 在 PATH 中的目录中搜索脚本

In fact, that's exactly what program loader is going to do. Here's a quote from wikipedia shebang article:

事实上,这正是程序加载器要做的。这是维基百科shebang文章的引述:

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script. For example, if a script is named with the path "path/to/script", and it starts with the following line:

#!/bin/sh

then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible shell), passing "path/to/script" as the first argument.

在类 Unix 操作系统下,当带有 shebang 的脚本作为程序运行时,程序加载器将脚本初始行的其余部分解析为解释器指令;而是运行指定的解释器程序,将尝试运行脚本时最初使用的路径作为参数传递给它。例如,如果脚本以路径“path/to/script”命名,并且它以以下行开头:

#!/bin/sh

然后指示程序加载器运行程序“/bin/sh”(通常这是 Bourne shell 或兼容的 shell),将“path/to/script”作为第一个参数传递。

Edit:

编辑:

If they say Oh, but can you execute it in bash just by using ./script ?then the answer is still yes. Remove /bin/kshand copy(or link) bash to the same path. Taa-daa! Might require root privileges though.

如果他们说Oh, but can you execute it in bash just by using ./script ?那么答案仍然是肯定的。删除/bin/ksh并复制(或链接)bash 到同一路径。哒哒!不过可能需要root权限。