Convert Bash Script to C. Is it possible?

I found the following: http://www.basic-converter.org/stable/bacon.bash

Is such a conversion from bash to c possible?

Reason: C is faster than BASH? I want to run something like a deamon instead of a cron job.

+4
source share
2 answers

I am sure that someone made the tool, simply because I could, but I did not see it. If you need to run a bash script from C code, you can simply execute it through (for example) a system call:

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

Other than that, I don’t know a simple way to "automatically" do this. As humans, we can consider the above and equate this to:

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

As one of a dozen ways that can be achieved. So it is quite easy to do it manually, it is obvious that it will take much more effort to do what can be considered a bash β†’ C compiler to do this automatically.

So maybe? Sure!
Example? Sorry, but no.

+4
source

Perhaps the question is what are the goals of this. They can be a subset of:

  • The speed of interpreted scripts may be slower
  • Maintaining health , maybe you have time that has more experience with C
  • The flexibility of the script shows the limitations of what can be achieved with reasonable effort.
  • Integration , perhaps you already have a code base that you are ready to integrate closely with scripts
  • Portability

There are other reasons, such as scalability, efficiency, and possibly much more.

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

On the one hand, we have compiled C code that runs mostly like bash, so each line of the source script will generate code equivalent to the fork / exec / wait system calls, where the changes will basically do the equivalents of the wildcard extension, extract the values from environment variables, handle the synchronization of branched processes, as well as process pipelines with the corresponding system call.

Note that this β€œsimple” conversion is already a lot of work, which is likely to be worse than just writing another shell interpreter. In addition, it does not satisfy many of the above goals, since portability is wise, it still probably depends on system calls of the operating system and performance, only a gain from the initial command line parsing.

On the other hand, we have a complete census in more mode C. This will replace all conditional expressions with conditional expressions C, ls , cd and rm in the corresponding system calls and, possibly, replace string processing with the corresponding libraries.

It may be better in achieving some goals, but the cost is likely to be even greater than in the other case, also eliminating a lot of code reuse, as you will have to implement function equivalents to simple commands.

As for the tool to automate this, I don't know anyone, and if they are, they probably don't have widespread use, because converting bash to C or C to bash is probably not a good idea. If such a need arises, this is probably a symptom of a design problem, and therefore redesign is probably the best solution. Programming and scripting languages ​​are different tools for different tasks, although there are areas of intersection between what can be done with them. Generally,

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

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

Hope this helps a bit =)

+3
source

Source: https://habr.com/ru/post/1441602/


All Articles