Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bash Script to C. Is that possible?

Tags:

c

bash

converters

I found the following Bash -> C converter.

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

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

like image 735
Amanada Smith Avatar asked Oct 23 '12 16:10

Amanada Smith


People also ask

Are shell scripts written in C?

C shell's scripting syntax is modeled after the C language in some aspects. Small programs can be created by writing scripts using the C shell syntax.

Is shell script similar to C?

Shell script is not a subset of C at all. It is a very high level language.

Can you CD in a bash script?

Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change.


2 Answers

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

  1. Speed interpreted scripts can be slower
  2. Maintainability perhaps you have a time that has more experience with C
  3. Flexibility the script is showing limitations on what can be achieved with reasonable effort
  4. Integration perhaps you already have a code base that you're willing to tightly integrate with the scripts
  5. Portability

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.

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.

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.

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

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,

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 =)

like image 187
Janito Vaqueiro Ferreira Filho Avatar answered Oct 25 '22 17:10

Janito Vaqueiro Ferreira Filho


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:

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.

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

like image 45
Mike Avatar answered Oct 25 '22 19:10

Mike