tags:

views:

687

answers:

6

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

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.

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?

A: 

It does not compile the bash script, it is just sort of obfuscator which executes the script hidden inside the program.

Havenard
That's right. I should have been more precise. Thanks for your comment anyway.
mfriedman
+1  A: 

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.

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

Chris Lutz
Shell scripts can do way more than just run external programs. You can do loops and assign and compare variables as well as other things. Also, many shells implement some more common commands internally so they don't have to call external programs to run them.
pib
+4  A: 

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

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 fork them.

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.

Stephen C
+1 Thanks! I didn't know about CCsh.
mfriedman
This program looks quite interesting, but it does bring up the question of "What are you trying to accomplish?", which I should have asked in my answer. If this looks like what you want, a better bet may be to rewrite the bash script in another language (C if you absolutely need speed, C++ if you need a good balance between speed and built-in functionality, Perl/Python if the only performance increase you really need is not forking for every command, etc.)
Chris Lutz
They don't have a price on the web site, but ask that they be contacted to request them to provide a quote. Also, distributing the resultant binary is free of cost.
Dennis Williamson
A: 

Chris Lutz makes a good point. What would you compile a shell script to?

And a utility which 'compiles' Bourne Shell to c code is not compiling - it's only translating - and a C compiler is necessary to finish the job.

pavium
Yes, I understand. But if I can get a shell script translated into C, then I can compile the resulting translation, and that's what I'm interested in. I'll try to clarify my question a bit more.
mfriedman
Saying "compiling to C" == "not compiling" is splitting hairs, IMO. Sort of like saying that `javac` is not a compiler because it doesn't emit native machine code.
Stephen C
A: 

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 interpeting needed to be done.)

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.

asdfkjaskj
A: 

Actually, at least in bash the above line

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

does not invoke any external command at all, as both test and echo are bash builtins. So no forking takes place, and consequently, the performance boost from compiling to C wouldn't be as large as suggested above.

Goldi