将 Bash 脚本转换为 C。这可能吗?

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

Convert Bash Script to C. Is that possible?

cbashconverters

提问by Amanada Smith

I found the followingBash -> C converter.

我找到了以下Bash -> C 转换器。

Is such a way possible to convert from bash to c?

这种方法可以从 bash 转换为 c 吗?

Reason: Is C faster then BASH? I want to run something as a deamon instead of a cron job.

原因:C 比 BASH 快吗?我想将某些东西作为守护程序而不是 cron 作业运行。

回答by Janito Vaqueiro Ferreira Filho

It is possible, the question is what are the objectives of doing so. They could be a subset of:

有可能,问题是这样做的目的是什么。它们可能是以下各项的子集:

  1. Speedinterpreted scripts can be slower
  2. Maintainabilityperhaps you have a time that has more experience with C
  3. Flexibilitythe script is showing limitations on what can be achieved with reasonable effort
  4. Integrationperhaps you already have a code base that you're willing to tightly integrate with the scripts
  5. Portability
  1. 速度解释脚本可能会更慢
  2. 可维护性也许你有时间对 C 有更多的经验
  3. 脚本的灵活性显示出通过合理努力可以实现的目标的限制
  4. 集成或许您已经拥有一个愿意与脚本紧密集成的代码库
  5. 可移植性

There are also other reasons, like scalability, efficiency, and probably a lot more.

还有其他原因,比如可扩展性、效率,可能还有更多。

Based on the objectives of the "conversion", there are quite a few ways to achieve a C equivalent, varying the amount of code that will be "native". As an example we can consider two extremes.

基于“转换”的目标,有很多方法可以实现 C 等价物,改变将是“本机”的代码量。作为一个例子,我们可以考虑两个极端。

On one extreme, we have a compiled C code that executes mostly as bash would, so every line of the original script would produce code equivalent to a fork/exec/wait system calls, where the changes would mostly be performing equivalents to wildcard expansion, retrieving of values from environment variables, handling synchronization of the forked processes, and also handling piping with the appropriate system call.

在一个极端情况下,我们有一个编译后的 C 代码,它主要像 bash 一样执行,所以原始脚本的每一行都会产生等效于 fork/exec/wait 系统调用的代码,其中的更改主要是执行与通配符扩展等效的代码,从环境变量中检索值,处理分叉进程的同步,以及使用适当的系统调用处理管道。

Notice that this "simple" conversion is already tons of work, that would probably be worse than just writting another shell interpreter. Also, it doesn't meet many of the objectives above, since portability wise, it is still probably dependent on the operating system's syscalls, and performance wise, the only gain is from initially parsing the command line.

请注意,这种“简单”的转换已经是大量的工作,这可能比编写另一个 shell 解释器还要糟糕。此外,它不符合上述许多目标,因为在可移植性方面,它仍然可能依赖于操作系统的系统调用,而在性能方面,唯一的好处是最初解析命令行。

On the other extreme, we have a complete rewrite in a more C fashion. This will replace all conditionals with C conditionals, ls, cdand rmcommands into their respective system calls and possibly replacing string processing with appropriate libraries.

在另一个极端,我们以更 C 的方式完全重写。这将替换为C条件句,所有条件句lscdrm命令到它们各自的系统调用和可能与相应的库替换字符串处理。

This might be better in achieving some of the objectives, but the cost would probably be even greater than the other way, also removing a lot of code reuse, since you'd have to implement function equivalents to simple commands.

这在实现某些目标方面可能会更好,但成本可能比其他方式更大,同时还消除了大量代码重用,因为您必须实现与简单命令等效的功能。

As for a tool for automating this, I don' know of any, and if there are any they probably don't have widespread use because converting Bash to C or C to Bash isn't probably a good idea. If such need arises, it is probably a sympton of a design problem, and therefore a redesign is probably a better solution. Programming languages and Scripting Languages are different tools for different jobs, even though there are areas of intersection between what can be done with them. In general,

至于自动化的工具,我不知道,如果有的话,它们可能不会被广泛使用,因为将 Bash 转换为 C 或将 C 转换为 Bash 可能不是一个好主意。如果出现这种需求,这可能是设计问题的征兆,因此重新设计可能是更好的解决方案。编程语言和脚本语言是用于不同工作的不同工具,即使它们之间存在交叉领域。一般来说,

Don't script in C, and don't code in Bash

It is best to know how and when to use the tools you have, then to find a generic universal tool (aka. there are no such things as silver bullets).

最好知道如何以及何时使用您拥有的工具,然后找到一个通用的通用工具(也就是没有银弹之类的东西)。

I hope this helps a little =)

我希望这会有所帮助 =)

回答by Mike

I'm sure someone has made a tool, just because they could, but I haven't seen one. If you need to run a bash script from C code, it's possible to just directly execute it via (for example) a system call:

我敢肯定有人已经制作了一种工具,只是因为他们可以,但我还没有见过。如果您需要从 C 代码运行 bash 脚本,则可以通过(例如)系统调用直接执行它:

system("if [ -f /var/log/mail ]; then echo \"you've got mail! (file)\"; fi");

Other than that, I'm not aware of an easy way to "automatically" do it. As humans we can look at the above and equate that to:

除此之外,我不知道“自动”做到这一点的简单方法。作为人类,我们可以查看上述内容并将其等同于:

if( access( "/var/log/mail", F_OK ) != -1 )
    printf("you've got mail! (file)");

As one of a dozen ways that could be achieved. So it's pretty easy to do that by hand, obviously it's going to take a lot more effort to make, what can be thought of as a bash->C compiler to do it automatically.

作为可以实现的十几种方法之一。因此,手动执行此操作非常容易,显然需要付出更多努力,可以将其视为 bash->C 编译器来自动执行此操作。

So is it possible? Sure!
Example? Sorry, no.

那么有可能吗?当然!
例子?抱歉,没有。

回答by unifex

There's a program I use to obfuscate code when I need that. For some of the programs I've used it on, it does improve the speed, on others it slows the script down, but that's not why i use it. The main utility for me is that the binary is not capable of being changed or read by casual users.

当我需要时,我使用一个程序来混淆代码。对于我使用过的一些程序,它确实提高了速度,而在其他程序中它会减慢脚本速度,但这不是我使用它的原因。对我来说主要的实用程序是二进制文件不能被临时用户更改或读取。

article: here http://www.linux-magazine.com/Online/Features/SHC-Shell-Compiler

文章:这里 http://www.linux-magazine.com/Online/Features/SHC-Shell-Compiler

developer's site here: http://www.datsi.fi.upm.es/~frosal/sources/

开发者网站:http: //www.datsi.fi.upm.es/~frosal/sources/

As mentioned on one of those pages, it falls someplace between a gadget and an actual tool.

正如其中一页所述,它介于小工具和实际工具之间。