bash shell 脚本编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1297318/
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-17 21:08:28  来源:igfitidea点击:

Compilers for shell scripts

bashcompiler-construction

提问by mfriedman

Do you know if there's any tool for compiling bash scripts?

你知道有没有编译bash脚本的工具?

It doesn't matter if that tool is just a translator (for example, something that converts a bash script to a C program), as long as the translated result can be compiled.

该工具是否只是一个翻译器(例如,将 bash 脚本转换为 C 程序的东西)并不重要,只要可以编译翻译的结果即可。

I'm looking for something like shc(it's just an example -- I know that shc doesn't work as a compiler). Are there any other similar tools?

我正在寻找类似shc 的东西(这只是一个例子——我知道 shc 不能作为编译器工作)。还有其他类似的工具吗?

回答by Stephen C

A Google search brings up CCsh, but it will set you back $50 per machine for a license.

Google 搜索会显示CCsh,但每台机器的许可证费用为 50 美元。

The documentation says that CCsh compiles Bourne Shell (not bash ...) scripts to C code and that it understands how to replicate the functionality of 50 odd standard commands avoiding the need to forkthem.

文档说 CCsh 将 Bourne Shell(而不是 bash ...)脚本编译为 C 代码,并且它了解如何复制 50 多个标准命令的功能,而无需使用fork它们。

But CCsh is not open source, so if it doesn't do what you need (or expect) you won't be able to look at the source code to figure out why.

但是 CCsh 不是开源的,因此如果它不能满足您的需要(或期望),您将无法查看源代码以找出原因。

回答by Chris Lutz

I don't think you're going to find anything, because you can't really "compile" a shell script. You could write a simple script that converts all lines to calls to system(3), then "compile" that as a C program, but this wouldn't have a major performance boost over anything you're currently using, and might not handle variables correctly. Don't do this.

我认为您不会找到任何东西,因为您无法真正“编译”shell 脚本。您可以编写一个简单的脚本,将所有行转换为对 的调用system(3),然后将其“编译”为 C 程序,但这不会对您当前使用的任何程序产生重大的性能提升,并且可能无法正确处理变量。不要这样做。

The problem with "compiling" a shell script is that shell scripts just call external programs.

“编译”shell 脚本的问题在于,shell 脚本只是调用外部程序。

回答by asdfkjaskj

In theory you could actually get a good performance boost.

理论上,您实际上可以获得良好的性能提升。

Think of all the

想想所有的

if [ x"$MYVAR" == x"TheResult" ]; then echo "TheResult Happened" fi

(note invocation of test, then echo, as well as the interpreting needed to be done.)

(注意 test 的调用,然后是 echo,以及需要完成的解释。)

which could be replaced by

可以用

if ( !strcmp(myvar, "TheResult") ) printf("TheResult Happened");

In C: no process launching, no having to do path searching. Lots of goodness.

在 C 中:无需启动进程,无需进行路径搜索。很多善。